-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
add a mypy pre-commit hook #26954
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 a mypy pre-commit hook #26954
Conversation
Most of our config is in fyi to run I usually do:
(though ✔️ I edited a stub file to make sure that if I broke something there it flagged ❌ I edited a backends py file to see that it got ignored (it flagged that file, despite being in the excludes in mypy config) ❓ : I edited a gallery entry which did not previously have an error to introduce an error, wanting it to not check (currently a lot of of our galleries would fail, there has been no effort towards typing those files)
We could be explicit about which files we include on the hook, which would probably resolve all(?) of these (but I'm not a fan of duplicating the info there and in mypy's own config... just not sure how to have one source of truth) using a combination of Finally, since its running in its own venv, we probably need to tell it to install dependencies that are referred to in stubs (it ignores them if they are missing, but that will deviate from behavior either running outside of pre-commit locally or the CI check, which is just confusing. Not sure if you can just do matplotlib/requirements/testing/mypy.txt Lines 6 to 10 in e3a5cee
which we interact with (some more than others) We also pin mypy version right now, because there is a bug (that I reported) in recent versions of mypy that affects one file... leaning towards |
I guess it is also true that the hook, which only checks edited files, will not flag things if you cause e.g. child classes in other files to be be broken (CI will still see it, but it is a balancing act a bit there) this is likely relatively rare, but something that could be confusing |
My major reason for wanting to check this locally (via pre-commit) is so that I don't have to rebase b/c every push is a new commit. lib\matplotlib\colors.pyi:81: error: Overloaded function signature 3 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
lib\matplotlib\colors.pyi:165: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [misc]
lib\matplotlib\colors.pyi:171: error: Overloaded function signature 3 will never be matched: signature 2's parameter type(s) are the same or broader [misc] |
e629886
to
1cb7202
Compare
I've gotten it to use the .toml config, but I have no idea how to deal with this error:
|
Ok, using the cuda mypy config showed me that I can just pass in where to look and not mess around with a mypy_path, which fixes that error. Which means that this now is pointed to "matplotlib/lib" and pulls it's configuration from pyproject.toml |
1890260
to
8ddf142
Compare
"lib/matplotlib/backends/", | ||
"lib/matplotlib/sphinxext", | ||
"lib/matplotlib/testing/jpl_units", | ||
"lib/mpl_toolkits/", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are included in the first two regex entry (which is a regex to allow the mypy -m matplotlib
form which looks at installed version which is needed for stubtest.
Additionally, tests
should be included, while we do not fully type hint it, it provides a set of code that was not a large burden to add minimal types to but helps to check more usage than stubs alone do.
I would not mind expanding this to galleries once type hints are more established (so not super soon), but there are complexities such as reused variables which change types, etc.
I'm not totally opposed to adding galleries
and docs
to this list for now, though when run as mypy lib/matplotlib
they are not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for reasons I don't understand, the regex is totally ignored when I run on my machine but the explicit listing isn't. I didn't remove the regex in case you need it for something else
This form has the advantage of catching errors caused in other files, but the disadvantage of always taking the time to run on every file Judging from the CI run, the time is not bad (~38 s without mypy, ~43 with mypy) I'm guessing checks run in parallel to some degree. Overall I'd say i'm +0.5 on including this running against the whole codebase like it is (modulo config cleanup), but would like to hear if others have objections to the hook time going up. |
Yeah, that also seems like it's best practice and also the only way I've sorted to avoid:
but also mypy only gets run if a .py file gets changed and I think (via some hack testing) setting |
3605b7e
to
5c65fe5
Compare
excludes to mypy toml config
5c65fe5
to
9923ef9
Compare
Um did I break the stubtests? eta: never mind, I broke pyproject.toml locally |
Add mypy pre-commit hook, not sure which args are needed to have it behave the same as the one in ci. See discussion in #26928 -> I both forgot what I'm supposed to run locally and didn't realize I could run mypy locally. attn @ksunden