Skip to content

Black

Black is a style guide enforcement tool.

You can use this cookiecutter template to create a python project with black already configured.

Installation

pip install black

Configuration

Its configuration is stored in pyproject.toml.

File: pyproject.toml

# Example configuration for Black.

# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python.  Multiline strings are treated as
# verbose regular expressions by Black.  Use [ ] to denote a significant space
# character.

[tool.black]
line-length = 88
target-version = ['py36', 'py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
    \.eggs
  | \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
  # The following are specific to Black, you probably don't want those.
  | blib2to3
  | tests/data
  | profiling
)/
'''

You can use it both with:

  • The Vim plugin

  • Pre-commit:

    File: .pre-commit-config.yaml

    repos:
    - repo: https://github.com/ambv/black
      rev: stable
      hooks:
        - id: black
          language_version: python3.7
    
  • Github Actions:

    File: .github/workflows/lint.yml

    ---
    name: Lint
    
    on: [push, pull_request]
    
    jobs:
      Black:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-python@v2
          - name: Black
            uses: psf/black@stable
    

Split long lines

If you want to split long lines, you need to use the --experimental-string-processing flag. I haven't found how to set that option in the config file.

Disable the formatting of some lines

You can use the comments # fmt: off and # fmt: on

References