-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Inferring a more precise type on assignment to a variable with Any type #2928
Comments
It seems that in general variables whose initial type is As a work-around, if the initial type is |
The |
One thing to note is that we don't distinguish between variables that have type annotations and variables that set their type from the first assignment. The example would look a lot more reasonable if it was: from typing import Any
def f(x: Any) -> None:
y = x # type: Any
y = 1
reveal_type(y) # Any In this case example (where the type is explicit), I don't think it makes sense to reduce |
This is an old argument (so far carried out in private I think) but I don't think the explicit
and
I think both of these should reveal the same type ( (In fact, if anything, the |
This would be nice to fix, on one hand. On the other hand, this could generate a lot of false positives in existing code that type checks cleanly, so I'm not sure if fixing this is actually worth it. |
Mypy infers the type
Any
fory
after the second assignment:Inferring
int
as the type might be less surprising. However, if a variable is explicitly declared with anAny
type, such asx
in the above example, it's less clear what would be the right thing to do.A more general solution would allow redefining variables with arbitrary new types (#1174).
The text was updated successfully, but these errors were encountered: