Ignoring Django Migrations in pyproject.toml file for Black formatter
Asked Answered
S

5

22

I just got Black and Pre-Commit set up for my Django repository.

I used the default config for Black from the tutorial I followed and it's been working great, but I am having trouble excluding my migrations files from it.

Here is the default configuration I've been using:

pyproject.toml

[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

I used Regex101.com to make sure that ^.*\b(migrations)\b.*$ matched apps/examples/migrations/test.py.

[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
  | ^.*\b(migrations)\b.*$
)/
'''

When I add that regex line to my config file, and run pre-commit run --all-files, it ignores the .git folder but still formats the migrations files.

Sandblind answered 24/2, 2020 at 17:36 Comment(3)
Did you find a solution for this?Alejandroalejo
@ClarkSandholtz why are you not accepting any of the answer, or explain why they don't work for you?Bobsleigh
@Bobsleigh the formatting of the migrations ended up not being that big of a deal, so I just didn't worry about fixing it for awhile. Sorry for being slow on this.Sandblind
K
19

Add the migration exclusion to your .pre-commit-config.yaml file

- id: black
  exclude: ^.*\b(migrations)\b.*$
Kamikamikaze answered 11/9, 2020 at 5:38 Comment(1)
There is a reason to configure your tools in pyproject.toml (and others) instead of pre-commit's config: you wan't to configure them in one place and have it working in your IDE/CLI/whatever and not just in pre-commit.Discordancy
L
9

That's the solution to the problem: pyproject.toml

[tool.black]
exclude = '''
/(
  | migrations
)/

'''
Loud answered 20/9, 2020 at 18:39 Comment(2)
Can you pass the documentation on where you take this syntax under the exclude option? Is this part of TOML language or black? Couldn't find it in Black docs...Averroism
Sorry, I don't remember where I got this, it was already in my project :(Loud
R
3

Try this (note last line):

[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
  | migrations
)/
'''
Ranita answered 24/3, 2020 at 22:57 Comment(0)
L
2

Maintaining two different places for exclude config doesn't look good if avoidable and will not work well for the CI either (should you want to dry run black in the PR checks). Adding the following works for the pyproject.toml and then you can run the same in the pre-commit hook and CI:

[tool.black]
...
exclude = '''

(
  /(
    ...
    | .+/migrations
  )/
)
'''
Libriform answered 15/3, 2021 at 21:8 Comment(0)
A
1

to just extend the default exlusions (without adding the whole list), you can just do:

[tool.black]
extend-exclude = '''
/(
  | migrations
)/
'''
Achromat answered 11/8, 2022 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.