-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 does not honor exclude regex when files explicitly listed on the command line #438
Comments
From a prior art perspective, |
I'll wait for the black maintainers to indicate whether they think it's a good idea; no sense writing a PR for something that'll be rejected on concept. |
How is the decision of flake8 inconsistent here? The rationale is rather simple: by default, we exclude some paths and only include some file extensions in recursive search. But if you specifically give us a file path on the command line which doesn't match the file extension or would otherwise belong to the exclusion list, you probably know what you're doing. I do agree that interfacing with CI and editors is surprising in this case so I'm not downright rejecting changing this. But I need to carefully consider whether there are backwards compatibility disasters hiding in changing this. And even if we agree to do this, what does it mean for defaults in This is not going to be straight-forward to change but let's try to figure something out. Anthony, what would you suggest? |
ah let me clarify -- Maybe it's a bit snowflakey but intuitively it makes a lot of sense for me to apply That said, there's certainly arguments in both directions -- it may very well be simpler to only apply it during the recursive routines. the one thing I usually point at here is "pre-commit is better at running your linter than your linter is" because it can take advantage of a few things:
Though for tools that often means you have to configure both pre-commit exclusion and tool exclusion and keep them in sync (or a superset / subset of each other). It would be nice to only configure this in one place and for most that usually means "configure it using the tool's configuration". But then you run into OP's issue :) |
My confusion stemmed from the following line from black's own readme on pre-commit:
I know this specifically says "args", but I took this to mean "pyproject.toml is preferable to pre-commit's yaml when both have a similar config." It made sense at the moment (without having fully grasped the concept of how everything worked), that since If the answer is "exclude will only ever exclude on recursive searches" that's fine, but the readme should note that in the pre-commit section ("pre-commit passes specific files, not a recursive search, so you should exclude files in pre-commit as well"). But I would fully support some sort of "--hard-exclude" that is a definitive "black will not run on these files, no matter what" |
We'll definitely hard-exclude things that are .gitignore'd (see #475). Other than that, I don't think there's anything actionable here. If I pass a file explicitly to the tool, I expect it to be acted on. What I think we could do instead is to convince @asottile to let pre-commit run |
Would a PR with an update to the readme be warranted then?
This heavily implies that pre-commit should not have any config (although it admittedly just calls out |
I'd like to wait for what Anthony thinks about it. If he agrees that we should change the hook setup so that Black is ran on the entire repo every commit, then we will do just that and the README then remains fine. Otherwise, yeah, we will have to explicitly call out in the README that --exclude= in pyproject.toml is not used by pre-commit. I would like to avoid this. |
first, it's possible to enable this behaviour but I don't think it's desirable (as it sidesteps the benefits of the framework): - id: black
args: [.]
pass_filenames: false
always_run: true pre-commit is (generally) better at running linters than linters themselves are, here's a couple of reasons why:
An example of a case where always running black on all the files is (very) wrong is during a merge conflict resolution. pre-commit will only execute a linter on files which conflict or are manually changed avoiding the headache of waiting for (gotta run, hope this succinct reply is enough, if not I can elaborate / link some more prose on this -- hope it helps!) |
When using an editor integration that calls black with a changed file's path (which it could do automatically, such as on a post save action), this behaviour also means that it would reformat the file even if it's excluded by the config. So I would be in favor of either changing the default to always consider the ignore/exclude rules, or to include an option to do so even when a full path is provided. |
Hi everyone, I also run into this problem with multiple editors and this behavior was surprising to me, as a user. IMHO you don't want to have multiple configurations stating the same files to exclude: pyproject.toml, pre-commit (not everyone using the tool named We have many tools in the team so it's essential one configuration will be used by everyone. I think that in terms of usability, surprising the user is never a good idea.
If this change of behavior is agreed upon, I can try and create the PR. |
Any progress/decisions on this? |
I agree with Anthony that pre-commit is in a better position to figure out when to run Black on what, so if we could somehow get away with only ever running Black through pre-commit, that would provide a way to solve this problem. I don't think it's OK to assume Black is always going to be called through pre-commit though, so we do need a separate include/exclude mechanism (and unfortunately we do need to worry about parsing As for the performance concerns around running So I think all in all we should encourage people to configure Black with the following in their
|
please don't suggest that per above, thanks even with black's cache it's going to be the wrong thing during merge conflicts and you're back to linting files that aren't checked in and you have to do filesystem traversals which themselves are pretty slow given how frequent this comes up I'm considering changing |
plus, even a trivial invocation of |
#438 (comment) is the closest to a proposal here, but I'm not sure that we need extra parameters. I think all we need to do is change the behavior of Desired Behavior:
Use cases:
The latter use case is important since during merge conflicts, we will only run on the files that the user edited, rather than on every file that anyone has edited. |
weird I thought I mentioned this but I guess not, if you're only invoking through repos:
- repo: ...
rev: ...
hooks:
- id: black
exclude: ^testing/test_data/ or if you're globally excluding repos:
exclude: ^vendor/
- repo: ...
rev: ...
hooks:
- id: black |
This works for me (in my limited use cases):
|
fixes psf#438 I Have not added or amended tests yet, but if we agree on the approach, I can work on them
I have added a PR following @chebee7i proposal, which I think is the most sensical #1032. I think that black needs to work well when integrating with other tools (editors, pre-commit hooks) for larger adoption. this concerns probably were less of a thing some years ago when flake8 was developed, but if we look at other modern tools and ecosystems (node: prettier, eslint, husky) this is the expected behavior. |
this is the 4th top commented issue in the repo, it would help a lot of people if some attention could be spared on this. There is an open PR, so my question is why this is not moving forward? a) simply no time to look into this? b) won't fix? c) something else? They are all acceptable answers :) but I'd like to set some expectations for myself |
@itajaja well for one your open PR has no tests and has a merge conflict -- I also don't think there's been any concrete proposal / agreement on the way to move forward yet |
As per psf/black#438 if you don't your excluded files to be formatted when passing filename (e.x. pre-commit), then use "force-exclude". Available in black>=20.8b1
When using `pre-commit run --all-files`, because the filename is passed explicitly, the file referred to in `extend-exclude` is not properly excluded. Use `force-exclude` instead to say we really mean it. See psf/black#438.
When using `pre-commit run --all-files`, because the filename is passed explicitly, the file referred to in `extend-exclude` is not properly excluded. Use `force-exclude` instead to say we really mean it. See psf/black#438.
When using VS code black plugin, because the filename is passed explicitly, the file referred to in `extend-exclude` is not properly excluded. Use `force-exclude` instead to say we really mean it. see psf/black#438
When using VS code black plugin, because the filename is passed explicitly, the file referred to in `extend-exclude` is not properly excluded. Use `force-exclude` instead to say we really mean it. see psf/black#438
Black does not honor patterns in --excludes when explicitly provided on the cli. This is problematic when used with pre-commit since pre-commit _always_ provides a list of files to act on. `--force-exclude` seems to act identically to `--extend-excludes` but also applies to cli arguments. See psf/black#438 for more background
When using `pre-commit run --all-files`, because the filename is passed explicitly, the file referred to in `extend-exclude` is not properly excluded. Use `force-exclude` instead to say we really mean it. See psf/black#438.
Operating system: OSX
Python version: 3.6.2
Black version: black, version 18.6b4
The problem: certain directories in our repo contain generated python code that we don't want black to change. We've configure our repo to run black via pre-commit. Pre-commit invokes black with a list of changed files on the command line, and black's exclude regex does not work against those files and paths.
i.e.
This makes us sad, since we've carefully put exclusion regexes into our pyproject.toml and black doesn't honor them when pre-commit calls it. Instead, we're having to workaround by configuring pre-commit to skip that path:
The behavior we'd like to see is that black's exclude regex would apply even when full file paths are listed on the commandline. I'd be happy to try for a PR if this seems like desirable behavior to anyone else...
The text was updated successfully, but these errors were encountered: