Exclude some files on running black using pre-commit
Asked Answered
N

2

61

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

Noble answered 4/4, 2020 at 17:48 Comment(0)
D
125

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

Disparage answered 5/4, 2020 at 17:54 Comment(13)
dumb question but, how do I exclude multiple directoriesArtis
@JamM.HernandezQuiceno exclude is a regular expression, so you'd use | -- for example: exclude: ^(migrations/|other/folder/) -- more on this in the docsDisparage
Thanks! This works also well for pydocstyleRenascence
@AnthonySottile request you add this example to the docs. I tried the regex approach (?x)^(...) for a mix of files and folders and couldn't get it working. I found your answer here and it worked like a charm.Lehmann
it is in the docs: pre-commit.com/#regular-expressionsDisparage
I'm getting "did not find expected '-' indicator" when trying to do exactly as you did in my .pre-commit-config.yamlTier
@Tier sounds like you made invalid yaml -- I can't help without seeing the full file though and could only guess at the many ways you can make invalid yamlDisparage
It would be a helluva lot nicer if exclude allowed globs or a YAML list of paths.Sybarite
globs are less powerful, and lists are ambiguous -- so no it would not be "nicer" -- it would be more complex, and more confusingDisparage
No, lists are not "ambiguous". And no it would not be confusing. The actual thing that is more confusing is requiring multi-lined regexes to specify multiple, well-defined paths.Sybarite
I'm in favor of simplicity over complexity--requiring someone to know special non-Yaml syntax inside of a Yaml file for something that could be very easily accomplished with a Yaml list, is the very definition of complexity.Sybarite
It looks like you had an opportunity to improve the excludes syntax with pre-commit/pre-commit#591, but sadly you rejected it in favor of more complex syntax.Sybarite
lists are ambigious -- is it "and" or "or" -- as someone who has spent literal years thinking about these problems please trust that you do not know what you're talking about. also there is no "non-yaml" syntax -- there is no extension of the parser whatsoever. there is one way to do things and that is best.Disparage
T
12

Currently, you can use black option --force-exclude. As indicated in the documentation:

--force-exclude TEXT Like --exclude, but files and directories matching this regex will be excluded even when they are passed explicitly as arguments.

So, in your pyproject.toml change exclude to force-exclude.

Thermostat answered 15/3, 2022 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.