Documentation example `--extend-exclude ^/foo.py` does not exclude root file `foo.py`
See original GitHub issueDescribe the bug
Black does not exclude the filefoo.py
root when asked to exclude it with ^/foo.py
. This particular example was used in the documentation:
extend-exclude = ''' # A regex preceded with ^/ will apply only to files and directories # in the root of the project. ^/foo.py # exclude a file named foo.py in the root of the project (in addition to the defaults) '''
To Reproduce Start with an empty directory
$ echo "s='test'" > foo.py
$ black --extend-exclude "^/foo.py" --check .
would reformat foo.py
Oh no! 💥 💔 💥
1 file would be reformatted.
Same behavior using --exclude
flag
Environment
- Black’s version: 21.12b0
- OS and Python version: OSX 11.6, Python 3.9.6
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
The basics - Black 22.12.0 documentation
Foundational knowledge on using and configuring Black. Black is a well-behaved Unix-style command-line tool: it does nothing if it finds no sources to...
Read more >Cannot get Black to ignore a file - Stack Overflow
I have Black 22.8.0 set as Formatting Provider and IDE is set to format the code on save. This is the first time...
Read more >black · PyPI
Black can be installed by running pip install black . It requires Python 3.7+ to run. If you want to format Jupyter Notebooks,...
Read more >The Internal Structure of Python Eggs - Setuptools
Note that this does not imply that the lines within the sections follow an .ini format, however. Please see an individual metadata file's...
Read more >setuptools.txt - Collection of Repositories
Transparent Pyrex support, so that your setup.py can list ``.pyx`` files and ... root, so to list a resource ``foo.png`` in package ``bar.baz``,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think that particular modification would help since for the case of pyproject file, the root is always the path of the pyproject file.
A section on general examples of exclusion patterns will help:
foo
exclude any file or directory if its path has this substring^/foo$
, exclude file or directory on project root (see Where Black looks for the file) Make sure to end with$
; without it, directories likefoobar/
will be excluded.foo\/
exclude any directory if its path has this substringLargely needed since everyone makes the mistake of attempting to use glob patterns (e.g.,
\.\/foo
and^foo\/
doesn’t work as intended)I had the same problem and circumvent it by using Git’s
.gitignore
and adding my excludes:If you want to ignore all Python files in the project’s root dir (but not
setup.py
, for example) you could add these lines:I’ve prepared a small environment to show how it looks like (see below for details). When I run
black
in verbose mode I get:Maybe this is not intended, but perhaps it’s even better to have all the ignores in the
.gitignore
file anyway. If I don’t want to reformat or check the source code of a Python file in the project’s root directory, it shouldn’t be included in the Git repo anyway.Details about the test Git repo