question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation example `--extend-exclude ^/foo.py` does not exclude root file `foo.py`

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ketozhangcommented, Jan 7, 2022

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 like foobar/ will be excluded.
  • foo\/ exclude any directory if its path has this substring

Largely needed since everyone makes the mistake of attempting to use glob patterns (e.g., \.\/foo and ^foo\/ doesn’t work as intended)

0reactions
tomschrcommented, Dec 17, 2022

I had the same problem and circumvent it by using Git’s .gitignore and adding my excludes:

# .gitignore file
/foo.py

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:

/*.py
!/setup.py

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:

$ black --verbose --check .
Identified `/tmp/gitblack` as project root containing a .git directory.
Sources to be formatted: "."
.git ignored: matches the --exclude regular expression
foo.py ignored: matches the .gitignore file content
setup.py already well formatted, good job.
src/foo/foo.py already well formatted, good job.

All done! ✨ 🍰 ✨
2 files would be left unchanged.

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
$ mkdir -p /tmp/gitblack && cd /tmp/gitblack
$ git init .
$ mkdir -p src/foo/
$ touch foo.py src/foo/foo.py setup.py
$ cat << EOF > .gitignore
/*.py
!/setup.py
EOF
$ git add .gitignore setup.py src/foo/foo.py
$ git commit -m "Initial import"
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found