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

Black pre-commit hook not respecting pyproject.toml exclude config #1985

Closed
jidicula opened this issue Feb 13, 2021 · 5 comments
Closed

Black pre-commit hook not respecting pyproject.toml exclude config #1985

jidicula opened this issue Feb 13, 2021 · 5 comments
Labels
C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black.

Comments

@jidicula
Copy link

Describe the bug
The Black pre-commit hook blocks commits containing staged Python files not respecting Black formatting when those files are in the tool.black exclude path in pyproject.toml. Those files should not be causing the pre-commit hook to fail. When running black --check . at the repo root, the check passes, indicating the issue is specifically with the pre-commit hook.

To Reproduce Steps to reproduce the behavior:

  1. Clone this repo containing a minimal reproducible example of this bug.
  2. Follow the directions in its README to reproduce the bug.

Expected behavior
Staged changes that are non-compliant with Black in files that are supposed to be ignored by Black should be able to pass the pre-commit check hook.

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: macOS 11.2.1 Python 3.7.1

Does this bug also happen on master? To answer this, you have two options:

Yes, see here. However, I could only use the commit at the HEAD of master on the day I tried this out (today, 2021-02-13), since pre-commit does not allow branchnames for the value of rev.

Additional context Add any other context about the problem here.
None

@jidicula jidicula added the T: bug Something isn't working label Feb 13, 2021
@ichard26
Copy link
Collaborator

ichard26 commented Feb 13, 2021

See #438. The problem is that pre-commit passes the files it wants the Black hook to check directly via the CLI instead of letting Black find them by itself. Files directly passed through the CLI aren't subject to the ---include/--exclude regexes since they only apply to recursively found files and directories. Try using --force-exclude which behaves like --exclude but also applies to files and directories directly given through the CLI.

I hope that helps!

@ichard26 ichard26 added C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black. and removed T: bug Something isn't working labels Feb 13, 2021
@jidicula
Copy link
Author

Thanks @ichard26! I'm still struggling to get this set up though. How should I be passing the path to the --force-exclude option? This is what I currently have and it's still failing the pre-commit hook when I change any Python file in the ignored/ path.

repos:
  - repo: https://github.com/psf/black
    rev: b8c1020b526b488a1a216d9a4d58fc8616b61a99
    hooks:
      - id: black
        language_version: python3
        args:
          - --force-exclude='ignored/'

@ichard26
Copy link
Collaborator

ichard26 commented Feb 13, 2021

repos:
  - repo: https://github.com/psf/black
    rev: b8c1020b526b488a1a216d9a4d58fc8616b61a99
    hooks:
      - id: black
        language_version: python3
        args: [--force-exclude, ignored/]

maybe??? (I don't use pre-commit or YAML syntax enough to be 100% sure)

You could also just use the --force-exclude option via your pyproject.toml config file.

jidicula added a commit to jidicula/pre-commit-black that referenced this issue Feb 13, 2021
jidicula added a commit to jidicula/pre-commit-black that referenced this issue Feb 13, 2021
@jidicula
Copy link
Author

jidicula commented Feb 13, 2021

You could also just use the --force-exclude option via your pyproject.toml config file.

AHA, yes that would be perfect - I much prefer having a SSOT for these configs 😁. Thank you so much, that worked nicely!

For anyone coming across this issue later, this is my tool.black entry in my pyproject.toml that is respected by pre-commit:

exclude = '''
/(
    \.eggs
  | \.git
  | \.venv
  | \.env
  | build
  | dist
  | \.mypy_cache
)\
'''
force-exclude = '''
/(
   ignored                     # files in this dir are ignored by pre-commit
)\
'''

The example is also visible here.

@maxbane
Copy link

maxbane commented Jun 27, 2022

Hi, I'm sure I'm missing something, but quick question... My pyproject.toml looks like this:

[tool.black]
extend-exclude = '''
^/codebases/external
'''
force-exclude = '''
^/codebases/external
'''

I find that running black . from my shell ignores the external codebases as desired, but when it is run by pre-commit run --all-files, black still modifies the external codebases. This is surprising to me -- do I really need to use two different regexes in these two options for them to have the same effect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: integrations Editor plugins and other integrations R: not a bug This is deliberate behavior of Black.
Projects
None yet
Development

No branches or pull requests

3 participants