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.3.tar.gz (31.6 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.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: blank_line_after_blocks-0.1.3.tar.gz
  • Upload date:
  • Size: 31.6 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.3.tar.gz
Algorithm Hash digest
SHA256 fd1748f2ea8783791b111707d7b62359c323081e6940ee2a93cb6272ddcbbf52
MD5 1f4b17ff24e0a6a792a44a8245b0897f
BLAKE2b-256 1564ba44353151b7ef63138c8b37ea7d1fb180a010801ae7b6f606a5e590a7dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for blank_line_after_blocks-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 65597676d240f329abfd1d92eacfa87b28d31ab8cc7887134bafad808f2917ef
MD5 d54deb663d1deeba80e73007fe544ee1
BLAKE2b-256 df6862aa777173351fe9002aabc94b051831391d869baaf089556a4ed163cd9b

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