Reusable Rattle rules for blank-line and statement cuddling policies.
Project description
rattle-blank-lines
Rattle rules for blank-line and statement-cuddling policy checks in Python.
The distribution and repository name are rattle-blank-lines.
The project keeps the historical fixit_blank_lines package path for compatibility.
Installation
pip install rattle-blank-lines
uv add rattle-blank-lines
Quick Start
Add the rule pack to your project configuration:
[tool.rattle]
root = true
enable = ["fixit_blank_lines.rules"]
This enables the default rule pack. Rattle keeps its built-in rattle.rules enabled by default; add disable = ["rattle.rules"] if you want to run only this rule pack.
Run linting and autofix:
rattle lint <path>
rattle lint --diff <path>
rattle fix --automatic <path>
For in-file suppressions, use Rattle comments:
# lint-ignore: RuleName# lint-fixme: RuleName
Configurable Rules
Rattle supports per-rule settings under [tool.rattle.options]. This package exposes short rule selectors via codes and class-name aliases.
[tool.rattle.options]
BL200 = { max_suite_non_empty_lines = 2 }
BL210 = { short_control_flow_max_statements = 3 }
BL400 = { max_case_non_empty_lines = 2 }
These values match the default behavior, so you only need to set them when you want to override the defaults.
Rules
NoSuiteLeadingTrailingBlankLines (BL100, BL101)
Removes leading and trailing blank lines at suite boundaries.
Before:
def f() -> int:
value = 1
return value
After:
def f() -> int:
value = 1
return value
BlankLineBeforeBranchInLargeSuite (BL200)
In suites larger than max_suite_non_empty_lines non-empty lines, requires a blank line before return/raise/break/continue. The default is 2.
Before:
def f(value: int) -> int:
x = value + 1
y = x + 1
z = y + 1
return z
After:
def f(value: int) -> int:
x = value + 1
y = x + 1
z = y + 1
return z
BlockHeaderCuddleRelaxed (BL300)
Allows assignment-before-block cuddling only when the immediately previous assignment feeds the block header or first body statement.
The first statement after a suite docstring is exempt (Ruff D202 compatibility).
Before:
def f(value: int) -> int:
prepared = value + 1
if value > 0:
return value
return 0
After:
def f(value: int) -> int:
prepared = value + 1
if value > 0:
return value
return 0
BlockHeaderCuddleStrict (BL301)
Stricter cuddle mode. Like BL300, the first statement after a suite docstring is exempt.
Opt in with fixit_blank_lines.rules.block_header_cuddle_strict, and disable BL300 if you want BL301 instead of BL300.
[tool.rattle]
root = true
enable = [
"fixit_blank_lines.rules",
"fixit_blank_lines.rules.block_header_cuddle_strict",
]
disable = [
"BL300",
]
Before:
def f(value: int) -> int:
header_value = value + 1
trailing = value + 2
if header_value > 0:
return header_value
return 0
After:
def f(value: int) -> int:
header_value = value + 1
trailing = value + 2
if header_value > 0:
return header_value
return 0
BlankLineAfterControlBlock (BL350)
Requires a separator after multiline control-flow blocks.
Consecutive simple if blocks without else stay together when they test the same subject expression, so compact dispatch ladders are preserved.
Before:
def f(value: int) -> int:
if value > 0:
value += 1
return value
After:
def f(value: int) -> int:
if value > 0:
value += 1
return value
BlankLineBeforeAssignment (BL210)
Requires a separator before an assignment when it follows a non-assignment statement,
except when the assignment directly follows a suite docstring.
Short control-flow suites (if/for/while/with/try/match) with at most short_control_flow_max_statements statements are exempt. The default is 3.
Before:
def f() -> int:
log_start()
value = compute()
return value
After:
def f() -> int:
log_start()
value = compute()
return value
MatchCaseSeparation (BL400)
Requires a separator before the next case when a case body is larger than max_case_non_empty_lines non-empty lines. The default is 2.
This rule is opt-in and is not included by enable = ["fixit_blank_lines.rules"].
You can enable it with enable = ["fixit_blank_lines.rules.match_case_separation"].
Before:
def f(value: int) -> int:
match value:
case 1:
a = 1
b = 2
c = 3
case _:
return 0
After:
def f(value: int) -> int:
match value:
case 1:
a = 1
b = 2
c = 3
case _:
return 0
License
Project details
Release history Release notifications | RSS feed
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 rattle_blank_lines-0.1.6.tar.gz.
File metadata
- Download URL: rattle_blank_lines-0.1.6.tar.gz
- Upload date:
- Size: 50.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81c46d2f02dc4615fad7d15464445c12ab8207c639f54385fc806ea41ccc5259
|
|
| MD5 |
9dc668eae4667dd34a931a37f55b972c
|
|
| BLAKE2b-256 |
2615ea83555dfef49fb1e3b39f4a4c957a7a66e2e19ac13234e73a843d994e1e
|
File details
Details for the file rattle_blank_lines-0.1.6-py3-none-any.whl.
File metadata
- Download URL: rattle_blank_lines-0.1.6-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dd5f6c84723b843da936a9fb8afd7e7b07135cf1742256a9481470fd6dd8cae
|
|
| MD5 |
27802412f628fde4912c9401523ee5b5
|
|
| BLAKE2b-256 |
68d8577f108eee74dc5d3c20c36e15a04624c1d4b2e6358f7ce990e0c1091205
|