Black --exclude argument not excluding desired file(s)
Asked Answered
N

2

18

Let's say I have the following python file exclude_from_black.py in the root of my project:

print('I want single quotes')

I'm trying to exclude this file from black reformatting but none of the following commands seem to work:

black --exclude="exclude_from_black.py"
black --exclude="exclude_from_black\.py"
black --exclude="exclude\_from\_black\.py"

According to the Documentation the exclude argument takes a regex. What am I doing something wrong here?

Nyx answered 15/11, 2019 at 17:50 Comment(1)
Well, try r"exclude_from_black\.py", but the first should also work. It seems the issue is not with the regexManger
N
9

Just tried these examples on black==20.8b1 and everything works as expected:

black --check --verbose --exclude="exclude_from_black.py" .
# exclude_from_black.py ignored: matches the --exclude regular expression

black --check --verbose --exclude="exclude_from_black\.py" .
# exclude_from_black.py ignored: matches the --exclude regular expression
Nyx answered 16/3, 2021 at 5:22 Comment(1)
Really should use --exclude="exclude_from_black\.py" since it is a regex.Subirrigate
C
11

I think you should omit the space after --exclude and wrap the regex in /…/.

Try black --exclude '/exclude_from_black\.py/'

Also, for debugging, -v should give some info about which files were excluded.

Cloudless answered 11/12, 2019 at 14:22 Comment(0)
N
9

Just tried these examples on black==20.8b1 and everything works as expected:

black --check --verbose --exclude="exclude_from_black.py" .
# exclude_from_black.py ignored: matches the --exclude regular expression

black --check --verbose --exclude="exclude_from_black\.py" .
# exclude_from_black.py ignored: matches the --exclude regular expression
Nyx answered 16/3, 2021 at 5:22 Comment(1)
Really should use --exclude="exclude_from_black\.py" since it is a regex.Subirrigate

© 2022 - 2024 — McMap. All rights reserved.