Skip to main content

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

MIT

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

rattle_blank_lines-0.1.5.tar.gz (49.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rattle_blank_lines-0.1.5-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file rattle_blank_lines-0.1.5.tar.gz.

File metadata

  • Download URL: rattle_blank_lines-0.1.5.tar.gz
  • Upload date:
  • Size: 49.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for rattle_blank_lines-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f9bb24b3f600419d37efce5609be4379bf71451ab83d50f9cc5fd44c9e2bf728
MD5 16d4ba7b886fe4933a88235f20b8dc04
BLAKE2b-256 1f16bdbf7b8fe30b3aa784eaa532d9b2a82f914b1f2cd6330acfe89ae63be962

See more details on using hashes here.

File details

Details for the file rattle_blank_lines-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for rattle_blank_lines-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 26860f7688d1fe1e7b2f1e8bc25b4742c029b553c5b00f5069287f4f17cacac7
MD5 6d27854f9dbb587a5e43390c7400338d
BLAKE2b-256 53270b57c4f49b8629a70bcbabad8b310864a57abb87526fd5694b7c1afc394c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page