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

Exclude property is not working in black #1584

Closed
kunal5 opened this issue Aug 11, 2020 · 8 comments
Closed

Exclude property is not working in black #1584

kunal5 opened this issue Aug 11, 2020 · 8 comments
Labels
C: configuration CLI and configuration T: bug Something isn't working

Comments

@kunal5
Copy link

kunal5 commented Aug 11, 2020

Describe the bug A clear and concise description of what the bug is.
Giving a file name to be excluded while running black via pre-commit is not excluding that file and formatting it.

To Reproduce Steps to reproduce the behavior:
.pre-commit-config.yml file

repos:
-   repo: https://github.com/ambv/black
    rev: stable # Replace by any tag/version: https://github.com/psf/black/tags
    hooks:
    -   id: black
        language_version: python3 # Should be a command that runs python3.6+
        args: ['--config=pyproject.toml', '-v']
-   repo: git@github.com:pre-commit/pre-commit-hooks
    rev: v2.0.0
    hooks:
    -   id: trailing-whitespace
    -   id: flake8
        args: ['--config=.flake8']
-   repo: git@github.com:pre-commit/mirrors-jshint
    rev: v2.12.0
    hooks:
    -   id: jshint
        language_version: 4.2.1
        entry: jshint squadrun/static/js/app/*
        args:
        - --exclude-path=squadrun/static/js/app/.jshintignore
        exclude: squadrun/static/js/libs/*
-   repo: git@github.com:SquadRun/mirrors-jscs
    sha: c7807cc8634375013f90942c5d927e3f7c55ab6b
    hooks:
    -   id: jscs
        language_version: 4.2.1
        entry: jscs squadrun/static/js/app/
        exclude: squadrun/static/js/libs/*
-   repo: local
    hooks:
    -   id: squadrun_linter
        entry: ./shell_scripts/squadrun_linter.sh
        name: squadrun_linter
        language: script
        files: ‘’

My pyproject.toml file

[tool.black]
target-version = ['py27']
line-length = 120
include = '\.pyi?$'
exclude = '''
(
  /(
      \.eggs         # exclude a few common directories in the
    | \.git          # root of the project
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
    | migrations
  )/
  | foo.py           # also separately exclude a file named foo.py in
                     # the root of the project
  | apps/core/migrations/0002_auto_20200707_1828.py
)
'''

Here I have explicitly given the name of the file to be excluded i.e. apps/core/migrations/0002_auto_20200707_1828.py
3. See error
This file is not excluded. Here is the snapshot for running black command. apps/core/migrations/0002_auto_20200707_1828.py got successfully formatted.
Screenshot 2020-08-11 at 7 14 49 PM

How can I fix this?
Please help!
Thanks!

  • Version: stable version
  • OS and Python version: [e.g. MacOS/Python 2.7.]
@kunal5 kunal5 added the T: bug Something isn't working label Aug 11, 2020
@hugovk
Copy link
Contributor

hugovk commented Aug 11, 2020

I'd guess it's a problem with the exclude regex.

Try changing it to something of this form:

# ...
    -   id: my-hook
        exclude: >
            (?x)^(
                path/to/file1.py|
                path/to/file2.py|
                path/to/file3.py
            )$ 

See https://pre-commit.com/#regular-expressions

@kunal5
Copy link
Author

kunal5 commented Aug 11, 2020

@hugovk , yes this is working but in this I have to explicitly name each and every file... there are 100s of migration files which I want to exclude.... how can I do that... something like apps/core/migrations/* is not working

@ichard26 ichard26 added the C: configuration CLI and configuration label Aug 11, 2020
@hugovk
Copy link
Contributor

hugovk commented Aug 11, 2020

Ah yes, you're already excluding from Black's config in pyproject.toml, so ideally there should be no need to duplicate that exclude for pre-commit's config.

But the Black exclude list is ignored when run from pre-commit. This sounds like a duplicate of #438.

There's a few suggestions in there.

  1. add --force-exclude argument #1032 added a new --force-exclude option to Black, you can change your Black exclude to force-exclude so it will always exclude despite whatever it gets from pre-commit/the CLI. However, this was only added to Black in March and hasn't been released yet (see Release new version onto PyPI #1583/Non-prerelease black release #517 for the next release).

  2. I think the best for now is, if you're only calling Black through pre-commit, then move your exclude list to .pre-commit-config.yaml

  3. (There's another suggestion with args: [.] and some other options, but it's not recommended.)

@kunal5
Copy link
Author

kunal5 commented Aug 11, 2020

@hugovk But exclude is working fine in python3 project but not in python2. Can this be related to python version as well ?

@hugovk
Copy link
Contributor

hugovk commented Aug 11, 2020

Black only works with Python 3.

@kunal5
Copy link
Author

kunal5 commented Aug 11, 2020

No... it only wants python3 environment and it works for python 2 files as well. It is working in my case

@hugovk
Copy link
Contributor

hugovk commented Aug 11, 2020

Okay, it shouldn't matter which sort of files you're processing with Black.

I suggest you do number 2 above, or if that doesn't work, create a minimal Git repo of your setup to demonstrate.

@ichard26
Copy link
Collaborator

The solutions provided by @ hugovk are correct (20.8b1 has been released so you can use force-exclude). Given that there is no response from OP and nothing actionable for us, I'll be closing this issue.

To OP: please reply with more information (a reproducible case would be ideal!) if you are still hitting your issue

adam-grant-hendry pushed a commit to adam-grant-hendry/pyvistaqt that referenced this issue Jun 28, 2022
`pre-commit` is known to override behavior from config files in
certain instances. This commit fixes known issues for:

- `isort`
- `black`
- `mypy`

For relevant details, see:

isort:
  + https://jugmac00.github.io/blog/isort-and-pre-commit-a-friendship-with-obstacles/
  + PyCQA/isort#885

black:
  + psf/black#1584

mypy:
  + python/mypy#4008 (comment)
  + https://pre-commit.com/#hooks-pass_filenames
adam-grant-hendry pushed a commit to adam-grant-hendry/pyvistaqt that referenced this issue Jun 28, 2022
`pre-commit` is known to override behavior from config files in
certain instances. This commit fixes known issues for:

- `isort`
- `black`
- `mypy`
- `pydocstyle`

For relevant details, see:

isort:
  + https://jugmac00.github.io/blog/isort-and-pre-commit-a-friendship-with-obstacles/
  + PyCQA/isort#885

black:
  + psf/black#1584

mypy/pydocstyle:
  + python/mypy#4008 (comment)
  + https://pre-commit.com/#hooks-pass_filenames
akaszynski pushed a commit to pyvista/pyvistaqt that referenced this issue Jun 29, 2022
* refactor(trove-classifiers): update to python 3.7-3.9

* refactor(linters): add `.flake8` and `.codespellrc` configuration files

Also add `PullRequest` to `ignore_words.txt`

* feat(pyproject): add `pyproject.toml`

Currently, this is only use for configuring linters and formatters (no
build file specifications are set). Configuration options for `black`
and `pydocstyle` are added here in this commit.

* feat(pre-commit): add pre-commit

* fix(mypy): add `mypy` dependency to `environment.yml`

This supports conda test/build environments.

* feat(pylint): Update `.pylintrc`

Make this match settings specififed in `Makefile`.

* feat(test): align checks

All checks are placed in appropriate config files. Test runner scripts
point to these so that there exists a single source of truth for all
configurations and that the tests performed across systems is the same.
This may be adjusted, of course, if different settings must be used on
a per-system basis.

* feat(requirements): add `toml`

This package supports reading `pyproject.toml`, which is required for
some of our tests.

* fix(pre-commit): fix file passing errors

`pre-commit` is known to override behavior from config files in
certain instances. This commit fixes known issues for:

- `isort`
- `black`
- `mypy`
- `pydocstyle`

For relevant details, see:

isort:
  + https://jugmac00.github.io/blog/isort-and-pre-commit-a-friendship-with-obstacles/
  + PyCQA/isort#885

black:
  + psf/black#1584

mypy/pydocstyle:
  + python/mypy#4008 (comment)
  + https://pre-commit.com/#hooks-pass_filenames

* feat(ignores): ignore specific linting errors

Ignore linting errors in source code in this PR. The purpose of this PR
is to update linting tools. A future PR will correct the errors. This is
done to separate concerns.

Co-authored-by: Hendry, Adam <adam.hendry@medtronic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants