-
-
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
"already defined" errors when adding type annotations in multiple code paths #1191
Comments
Hm, I think mypy actually has an intentional rule that you shouldn't annotate a variable twice, and I like that rule. So I am tempted to reject this one. The right fix would be to make the first annotation |
Yeah, this is by design. |
Closing this now. Feel free to reopen if you don't agree :-) |
Somewhat related, this error also triggers even when if True: # For example, should not be const in practice.
foo: int = 0
print(foo)
del foo
else:
foo: int = 0
print(foo) In the following example it even reports an error: "Trying to read deleted variable 'foo'" on the last line. if True: # For example, should not be const in practice.
foo: int = 0
print(foo)
del foo
foo: int = 0
print(foo) I am using Mypy 0.610. |
There should be only one declaration of a given variable per scope. And a function is one scope. |
This recommendations seems wholly unreasonable. Python itself has no such restriction. I feel lousy having to change my variable names just to make mypy happy. |
We are actively working on this restriction. |
The initial implementation will likely only cover redefinition within a single block at the same nesting level, but it's possible that we'll extend it to handle other cases in the future. |
@JukkaL should this github issue be re-opened? I just ran into this is in my code. import typing, logging
def convertLog(log_level: typing.Union[str, int]) -> int:
"""
usage:
level = convertLog(log_level="DEBUG")
assert level == 10
"""
if isinstance(log_level, str):
log_level: int = logging._nameToLevel[log_level.upper()]
return log_level
|
@komuw You can simply remove that |
Same Problem in the flowing to implementations so it definitely should be supported to have for loops with different types for the running variable
|
You can try |
I'm having an issue where this redefinition check ignores pathways that will never happen at runtime, for example where the code takes a path after an key: Union[int, str]
if isinstance(key, int):
...
result: Tuple[int, str] = ...
...
return result
... # here the type of 'key' is 'str' only
result: Tuple[int, str] = ... # here is the redefinition error, pointing at the line inside the if check above as the original definition
...
return result |
I'm fine with the rule "only one annotation per scope". To me, the problem is just that the error is not informative enough. Both the error text and the example in the documentation suggest that it may be a coding issue. Instead, I just had to remove the second annotation! if not resuming:
# Initialize
obj = MyClass()
else:
# Load back
obj: MyClass = loader.deserialize() Without this github issue I couldn't figure out what was wrong. |
any progress on removing the prohibition of declaring the same variable multiple times? this is annoying, should be left to the user |
Stumbled upon this as well, perhaps if this is helpful to some but not to others it could be turned into a warning instead. This feels like checking code style. |
While trying to debug something, I ended up adding type annotations for the definitions of an object in both code paths, and got a
/home/tabbott/foo.py:10: error: Name 'child_pid' already defined
with this (simplified) code:Since the types are the same, it seems like this shouldn't be an error.
This may be a variant of #1153.
The text was updated successfully, but these errors were encountered: