Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--extend-exclude not working when in pre-commit #3767

Closed
jsh9 opened this issue Jul 5, 2023 · 2 comments
Closed

--extend-exclude not working when in pre-commit #3767

jsh9 opened this issue Jul 5, 2023 · 2 comments
Labels
T: bug Something isn't working

Comments

@jsh9
Copy link

jsh9 commented Jul 5, 2023

Describe the bug

When Black is used as a pre-commit hook, the --extend-exclude option has no effect.

To Reproduce

  • Take a piece of Python code that you know will be formatted by Black
  • Add the path of this file to --extend-exclude
  • Run Black directly in the command line (like black /folder/that/contains/this/file)
  • Run Black in pre-commit

Expected behavior

When Black is run directly in the command line, that file is not formatted.

When Black is run via pre-commit, that file should also not be formatted.

Actual behavior

When Black is run directly in the command line, that file is not formatted.

When Black is run via pre-commit, that file is formatted.

Environment

  • Black's version: the main branch
  • OS and Python version: Python 3.9, macOS

Root cause

I did some debugging found the root cause: when Black is run directly in the command line, this branch is entered:

elif p.is_dir():

But when Black is run via pre-commit, this branch is entered because p.is_file() is True:

if is_stdin or p.is_file():

In the if is_stdin or p.is_file() branch, there's no checking of extend_exclude or exclude patterns, only force_exclude. That's why --extend-exclude has no effect.

Proposed solution

Given the root cause above, the solution should be straightforward.

@jsh9 jsh9 added the T: bug Something isn't working label Jul 5, 2023
@hauntsaninja
Copy link
Collaborator

I think you're looking for --force-exclude https://github.com/psf/black/blob/main/docs/usage_and_configuration/the_basics.md#--force-exclude
You can also use the pre-commit exclude options.

See #395 and #438

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
@jsh9
Copy link
Author

jsh9 commented Jul 5, 2023

I would argue that the documentation around exclude, extend-exclude, and force-exclude is very confusing.

Does force-exclude replace exclude? Or does it add on top of exclude?

And if users have to use force-exclude in pre-commit and force-exclude replaces exclude, does it mean that we now have to look into Black's code base to find out what the default exclude pattern is?

The code logic may be fine, but I would appreciate additional clarity in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants