Home

Awesome

brunette

🟤 A best practice Python code formatter

PyPI version

This is the "black" formatter but with some improvements:

  1. --config option supports setup.cfg format.
    • Where a single-quotes option enables single quotes as the preferred.
  2. --single-quotes option to make single quotes the preferred.

Installation

pip install brunette

Usage

Use in the same way you would the 'black' formatter.

brunette **/*.py
brunette *.py --config=setup.cfg
brunette *.py --line-length=79 --single-quotes

Example setup.cfg:

[tool:brunette]
line-length = 79
verbose = true
single-quotes = false
# etc, etc...

This can also be combined with Flake8's configuration:

[flake8]
# This section configures `flake8`, the python linting utility.
# See also https://flake8.pycqa.org/en/latest/user/configuration.html
ignore = E201,E202,E203
# E201 - whitespace after ‘(‘
# E202 - whitespace before ‘)’
# E203 - whitespace before ‘:’

# Exclude the git directory and virtualenv directory (as `.env`)
exclude = .git,.env

[tool:brunette]
line-length = 79
# etc, etc...

Why does this need to exist?

How to configure in VSCode

  1. Get the full path to your brunette installation. In your terminal type:

In my case this looks like /home/work/.pyenv/shims/brunette. On Windows thats more like C:\Python39\Scripts\brunette.exe. Now copy whatever that value is.\

  1. Open the setttings UI.

  2. Search for black.

https://i.imgur.com/6EXoamM.png

  1. That's it! Now whenever you format your Python code brunette will be used.

How to configure with Pre-Commit (https://pre-commit.com)

  1. Run pip install pre-commit to install

  2. Add a local repo option for brunette in .pre-commit-config.yaml

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/odwyersoftware/brunette
    rev: 0.2.7
    hooks:
      - id: brunette
  # Drop-in replacement for black with brunette
  # - repo: https://github.com/psf/black
  #   rev: stable
  #   hooks:
  #     - id: black
  #       language_version: python3.6
  - repo: https://gitlab.com/pycqa/flake8
    rev: 3.8.1
    hooks:
      - id: flake8
  1. Run pre-commit install to install the Git pre-commit hook

  2. Run pre-commit run to validate all files