-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Adding the --strict
option to mypy
#10076
Comments
I opened the issue here because it is not exactly a Mypy problem. It is related to the way we use it on pypa/pip checks. |
--no-implicit-optional
availability--no-implicit-optional
option
Yea, we have to enable a bunch of mypy's strictness flags (this is probably the highest impact among those). The ideal goal would be to get to a point where we can run |
How can we make that for the CI checks? |
Sorry, I found it. We are already using options for Mypy checks ( |
It must close pypa#10076, to include all the strict flags from Mypy to be used on pypa/pip.
Can I self-assign this, as I created the related pull request? |
Assigned |
Hi, do we still wanna get this done? You know, we couldn't apply #10091, but maybe we can try again :) Thoughts @pypa/pip-committers? |
I think so -- ideally, we'd be passing |
As of c260ecc, running
I'd like to fix all of those, TBH, so that we're getting the benefits of the complete checks/protections that mypy can provide. |
Sounds good to me. We once tried to add |
There are lots of errors cs mypy cant use annotations of 3rd party libraries (like |
FYI. You can configure mypy (in pyproject.toml/setup.cfg etc. ) to ignore certain errors in certain packages. https://mypy.readthedocs.io/en/stable/config_file.html?highlight=pyproject%20toml#examples This what we do in airflow for one (and I believe vendored-in code should be minimally modified if ever so probably it's better to ignore those in vendored packages, rather than fix them).
My 3 cents. |
I try to adapt strict=True. I also need types for requests, so additional dependency |
It does not recognize requests.Response as it was imported from _vendor.requests. So the solution would be adding #type: ignore comments to the function signatures and so on. |
I'm not sure I follow what you're seeing. Could you point to a commit that shows the behaviour that you're seeing? |
That defeats the whole point of having stubs in place for vendored modules -- we want to have the type from these modules and want the type checking benefits that come from that, while ignoring errors in only the vendored source files (ie. in pip._vendor). We've got the appropriate mypy configuration to achieve that. We also have redirects for the vendored modules, via stub files generated for them. If that isn't working for some reason, I'd like to get clearer details on what that looks like that's more detailed than "there are lots of errors". :) |
You probably misunderstood what I was explaining. I was only referring to "no_implicit_option" case. (and this is precisely what we are ignoring in the example). This particular setting is quite problematic and disabling it has benefits in this case. Because when the 3rd-party library has it set to False and your code has it set to true (like it is in this case and is in cae of google packages I mentioned) it's the easiest way to reap all the benefits of the vendored-in code without changing it. In the case I described, Google libraries were built with "no_implicit_optional=False" and our code with = True, setting this (and only this) setting for Google packages allowed us to still get all the Static Typing benefitsm while ignoring problem that Google code had MyPy in this case is still able to correclty infer the parameter type, but simply does not complain. |
That option is a subset of strict (which is what the rest of the discussion is about) and, as I said, we have mypy configured to not report errors about vendored code but to pick up annotations appropriately. |
@pradyunsg pip/src/pip/_internal/network/utils.py Line 43 in 9066584
it would raise Unsupported operand types for <= ("int" and "None") because mypy does not know about types-requests stubs, but this works
if TYPE_CHECKING:
from requests.models import CONTENT_CHUNK_SIZE, Response |
How are you running mypy? I reckon this is because we don’t have any types-requests in https://github.com/pypa/pip/blob/main/.pre-commit-config.yaml#L47 |
Via pre-commit. I've added types-requests=2.28.1 in additional-dependencies |
Hmm... I think there's something more happening here. |
It happens because of imports. Vendor pyi or rewrite imports |
--no-implicit-optional
option--strict
option to mypy
Renamed this thread, since we're now discussing to add |
Here's a checklist ordered by benefit:
I think once these are done, pip will be in a great place, with much lower marginal benefit to any additional type checker configuration changes |
Overview
This is the second part of the discussion from #10065 (review).
Description
I have been working on converting the type hint commentaries into proper annotations (#10004, #10018, #10065, #10074). But I had a simple mistake on #10065 by annotating this:
when it must be annotated like this:
We expected that Mypy was going to fail, but it just passed by. Then, we saw that Mypy accepts implicit optional annotations, until we use
--no-implicit-optional
.What we want
We want Mypy to use
--no-implicit-optional
on its checks, to make a better diagnosis. View the PR review where the original discussion is for more information.Code of Conduct
The text was updated successfully, but these errors were encountered: