Skip to main content

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:

  • if statements
  • for loops
  • while loops
  • with statements
  • try/except/finally blocks

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()

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')

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

blank_line_after_blocks-0.1.2.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

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

blank_line_after_blocks-0.1.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file blank_line_after_blocks-0.1.2.tar.gz.

File metadata

  • Download URL: blank_line_after_blocks-0.1.2.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for blank_line_after_blocks-0.1.2.tar.gz
Algorithm Hash digest
SHA256 32458576094d0712feb6560853dc0f5d9060bf839479ff9e5c0d2c0a1bb1c798
MD5 61aff8733eed4725194d3a5dc092c242
BLAKE2b-256 d940414e61904cc290664a9318eb7902803a1173d1a2d5364558a3597101937e

See more details on using hashes here.

File details

Details for the file blank_line_after_blocks-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for blank_line_after_blocks-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 576dccaae7c42f175caf4c4d4f065ec552f706de81e92abc253104bde51e6f03
MD5 22959a76bd1c23d845ccaf2516975be5
BLAKE2b-256 5cb8c0d3e46365197d565892a7a31a438f60b5ef80c94902f5844f31a3abfcaa

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