-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add new "force exclude" option #12373
Conversation
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Thank you! I'm not sure I'm in favour of this. People are already confused enough about the behaviour of Since mypy will follow all imports, in most cases users are not actually benefitting from pre-commit passing the files in git diff. I recommend setting the pre-commit flag |
Maybe When used manually, I can see why it might make some sense for In a perfect world, I think |
What you call using "manually" is not some niche case. mypy is a standalone tool, not a pre-commit plugin. In my previous comment, I proposed a pre-commit solution to your pre-commit problem that you seem to have ignored: use Additional config options have a real usability cost, especially when they're very similar to existing config options. |
I didn't claim using mypy manually was a niche case, it definitely isn't! But neither is using mypy with VSCode, pre-commit, or other tools (and I also don't think mypy development should be beholden to these other tools). And I didn't mention your solution in my comment, but I promise I didn't ignore it, it's a good solution! I could also make a PR to add that to the docs if you'd like, up to you. However, the point of my PR isn't just to solve my specific problem, it's to address what I think is a general usability problem with how I'm not saying my PR is the best or only way to do that, but I think the long-term direction I outlined in my previous comment (introducing I hear you on the usability cost (not to mention development & testing costs) of adding a new config option. I think this cost is acceptable if Let me know what you think, I appreciate the feedback. |
I came here looking for exactly this feature for exactly this reason. For what it's worth, black has |
Ah, that's right. I forgot because I started this PR a while ago and then neglected it, but I stole the |
There are other good workarounds though. The easiest one is pre-commit supports an exclude list of its own. I think using pre-commit exclude is my recommendation. It's minor duplication of excludes but excludes are normally just one/two lines long so I think that's easier than new option. If you really want to avoid duplication you can also make a python script that constructs the file list to mypy and calls mypy with any logic you like. If we do end up with a new exclude, using pyright as a comparison, pyright has exclude and ignore as separate. ignore is not same as force-exclude but I think would capture goal here. ignore suppresses all error messages for a while regardless of whether it is scanned. Ignore only suppresses error messages and does not affect which files are processed. exclude controls what modules are scanned similar to mypy. |
the issue with passing a subset of files to mypy is that there is a chance that mypy will then not check a file that will now contain errors: # a.py
a = 1 # b.py
from a import a
print(a + 1) If we were to update |
In CI you'd run pre-commit with Btw, a similar ticket I opened for semgrep, and semgrep is much faster going over our 100k+ LOC repos: |
I think In general, I think there are some pitfalls that make mypy not a natural fit for pre-commit. See also #13916 |
Description
This PR adds a
--force-exclude
CLI option and corresponding config option that behaves identically to--exclude
, except files and modules passed explicitly tomypy
will still be ignored. This addresses problems people have had where individual filepaths are passed tomypy
(typically by some tool like pre-commit or VSCode) and those individual filepaths unexpectedly get processed despite matching the--exclude
regex, e.g., issues #11094 and #11760.The exact case that motivated this PR was a mypy pre-commit hook scanning .py files generated by the protobuf compiler, even though I had
exclude = _pb2\.py$
in.mypy.ini
.Test Plan
I added a new unit test at
test_find_sources.py::test_find_sources_force_exclude
. This is essentially the same as the pre-existing testtest_find_sources_exclude
; I simply removed the default behavior checks at the top and modified the remaining logic to account for the handling of individual filepaths.I also rebuilt the docs and found no markup errors after manual inspection. Nor did I find problems after running
make linkcheck
.