A Python formatter to automatically add blank lines after if/for/while/with/try blocks
Project description
blank-line-after-blocks
A Python formatter to automatically add blank lines after if/for/while/with/try blocks to improve code readability.
Installation
pip install blank-line-after-blocks
Usage
Command Line
# Format Python files
blank-line-after-blocks file1.py file2.py
# Format with exclude patterns (regex - use | for multiple patterns)
blank-line-after-blocks --exclude "tests/|_generated\.py$" src/
# Format Jupyter notebooks
blank-line-after-blocks-jupyter notebook1.ipynb notebook2.ipynb
# Format notebooks with exclude patterns (regex)
blank-line-after-blocks-jupyter --exclude "notebooks/generated/" notebooks/
Pre-commit Hook
Add this to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/jsh9/blank-line-after-blocks
rev: <LATEST_TAG>
hooks:
- id: blank-line-after-blocks
- id: blank-line-after-blocks-jupyter
Pre-commit with exclude patterns
repos:
- repo: https://github.com/jsh9/blank-line-after-blocks
rev: <LATEST_TAG>
hooks:
- id: blank-line-after-blocks
args: ["--exclude", "tests/|_generated\.py$"]
- id: blank-line-after-blocks-jupyter
args: ["--exclude", "notebooks/generated/"]
Configuration File
You can also configure exclude patterns in pyproject.toml:
[tool.blank-line-after-blocks]
exclude = [
"tests/", # Exclude all files in tests directory
"_generated\.py$", # Exclude files ending with _generated.py
"vendor/", # Exclude all files in vendor directory
"build/", # Exclude build directory
]
Note: CLI --exclude options take precedence over configuration file
settings.
What it does
This tool automatically adds one blank line after the end of:
ifstatementsforloopswhileloopswithstatementstry/except/finallyblocks
This improves code readability by providing visual separation between blocks and subsequent code.
Examples
Basic if and for blocks
if condition:
do_something()
+
next_statement()
for item in items:
process(item)
+
final_step()
if a > 3:
print('a > 3')
# Already a comment; no new line added
a += 2
Try/except blocks with context managers
def process_files(filenames):
results = []
for filename in filenames:
try:
with open(filename) as f:
data = json.load(f)
+
results.append(data)
except FileNotFoundError:
print(f'File {filename} not found')
except json.JSONDecodeError:
print(f'Invalid JSON in {filename}')
+
return results
Nested blocks in class methods
class TestClass:
def method(self):
try:
if self.condition():
with self.get_context():
self.do_work()
+
self.cleanup()
+
except Exception as e:
self.handle_error(e)
+
print('method complete')
Compound blocks stay tight (no blank line before else/elif/except/finally)
If a block ends right before the second part of compound blocks (if/else, try/except, etc.), no blank line is added:
for item in items:
if found(item):
break
else:
not_found()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blank_line_after_blocks-0.1.4.tar.gz.
File metadata
- Download URL: blank_line_after_blocks-0.1.4.tar.gz
- Upload date:
- Size: 32.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b7b9ef3a3c298c52d718c87f99eada30a75d1792150d8bacf924436c5ffe884
|
|
| MD5 |
3679bd231e4a15e9ecf3ca4d4f869e69
|
|
| BLAKE2b-256 |
1c11188cc43fff820b6292db86bbd796fd9079dd4caca6af86ac11f508858985
|
File details
Details for the file blank_line_after_blocks-0.1.4-py3-none-any.whl.
File metadata
- Download URL: blank_line_after_blocks-0.1.4-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a920744aefb0969155074a5bff6db183fa76271f385a2245ed21ab362761f615
|
|
| MD5 |
309ef19c0c9cd67ac662033f9e81df0b
|
|
| BLAKE2b-256 |
79deb25edbae61a06e28440f39194541430dcd5d7ab194e13844f305a2425284
|