Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude some files on running black using pre-commit

I want to configure black in pre-commit and exclude precommit from checking any migrations folder.

My pyproject.toml looks like this

[tool.black]
line-length = 79
target-version = ['py37']
include = '\.pyi?$'
exclude = '''

(
  /(
      \.eggs
    | \.git
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
  )/
  | ^migrations/
'''

I have also configured precommit. But on running pre-commit run --all-files Black formats the migration folders also how can I configure black

like image 371
Suyash Salampuria Avatar asked Apr 04 '20 17:04

Suyash Salampuria


People also ask

How do you skip a pre-commit hook?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.

What does pre-commit do?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

Why use pre-commit hook?

The goal of pre-commit hooks is to improve the quality of commits. This is achieved by making sure your commits meet some (formal) requirements, e.g: that they comply to a certain coding style (with the hook style-files ). that you commit derivatives such as README.md or .


1 Answers

this issue on the black issue tracker outlines your particular problem

pre-commit finds all the python files, then applies pre-commit's exclusion, and then passes that list of files to the underlying tools (in this case black)

black currently (at the time of writing) will format all files listed on the command line -- independent of black's exclude pattern

the suggestion is to use pre-commit's exclusion (via your .pre-commit-config.yaml) such that those files are never passed to black at all:

-   id: black
    exclude: ^migrations/

note: unlike black, pre-commit will only ever lint files which are checked into your git repository, so the laundry list of exclusion is unnecessary here (.git / .mypy_cache / etc.)


disclaimer: I'm the author of pre-commit

like image 74
Anthony Sottile Avatar answered Sep 24 '22 22:09

Anthony Sottile