Black: --exclude <file> --check <file> doesn't exclude the file

Created on 29 Jul 2019  路  8Comments  路  Source: psf/black

Operating system: OSX
Python version: 3.6.8
Black version: 19.3b0

me:~ me$ echo "a = '123'" > test.py
me:~ me$ cat test.py
a = '123'
me:~ me$ black -l 2 --exclude test.py --check test.py
would reformat test.py
All done! 馃挜 馃挃 馃挜
1 file would be reformatted.

Understandably the use case is a bit odd, but in this toy example I would expect test.py to be excluded from the check and a message that no files needs to be reformatted.

command line

Most helpful comment

I could see myself doing something like black --exclude=some_dir/ $(find . -name '*.py'), and there I'd want it to obey the exclude rule.

This has also come up in the past in the context of tools like pre-commit, which want to run Black on all changed files in git without having to re-implement Black's exclude file parsing.

All 8 comments

I agree that it makes more sense to exclude the file in this case. In general it's safer for Black to exclude more files instead of reformatting files that shouldn't be reformatted.

Interesting, I actually expect all files given on the command line to be formatted, because I associate include/exclude with source collecting. Don't have a strong opinion though. I'll meditate on it :)

I could see myself doing something like black --exclude=some_dir/ $(find . -name '*.py'), and there I'd want it to obey the exclude rule.

This has also come up in the past in the context of tools like pre-commit, which want to run Black on all changed files in git without having to re-implement Black's exclude file parsing.

@JelleZijlstra example is exactly the use case where I discovered this behavior; when using Black in a pre-commit hook context.

is already there any workaround for this problem?

probably also related to #986

As I wrote in the now closed issue #1025, it also useful when used in an editor integration (for example PyCharm/IntelliJ IDEA), where the arguments can just be specified as: "--exclude file.py $FilePath$" to ensure that file.py is never reformatted, even if edited and saved.

I just realized there's already an issue open for this problem. Let's move the discussion over to #438.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devxpy picture devxpy  路  70Comments

underyx picture underyx  路  22Comments

Lukas0907 picture Lukas0907  路  66Comments

dusty-phillips picture dusty-phillips  路  21Comments

adamehirsch picture adamehirsch  路  48Comments