-
-
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
mypy is unhappy with repeated use of _
as a throw-away variable
#1332
Comments
A complication is that |
In the |
In my opinion, any variable (no matter its name) which is used only write only can be safely typed to Any (or to object), given that this can never be an unsafe operation, so it's sound and cover this pattern well (and some alternative things like using __ as placeholder) |
At module and class top levels we can't determine whether a variable will be read outside the module/class, so at least here a naming convention helps. |
Yes, my comment was made thinking about locals |
Instead of special casing write-only variables to have the Any type, we could also just be willing to infer Union types for things. E.g.: _ = 1
_ = "foo"
# _ now has type Union[int, str] Come to think of it, this would likely also solve the "variable must only have a single type" problem (provided that is something we want to solve) because of the way we already understand assignments to Union types. |
Isn't the problem with union types that next you will have to use
isinstance() before using it?
|
Not necessarily! We add more specific types to the binder on assignment, so e.g.: from typing import Union
x = 0 # type: Union[str, int]
x = "foo"
reveal_type(x) # Revealed type is 'builtins.str'
x = 0
reveal_type(x) # Revealed type is 'builtins.int' |
Closing this as a duplicate of #465. |
_
is commonly used as a throw-away variable in list unpacking, but this will make mypy unhappy if two lists with different types are unpacked in the same function. For exampleresults in:
To support this pattern, we may want to consider special-casing
_
and not generating this sort of type error if the variable is never read.The text was updated successfully, but these errors were encountered: