-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Reusing variable name in different scopes #5750
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
Comments
OK, let us keep this open, although it is just another aspect of a known problem. @JukkaL is currently exploring possible solutions. |
I beg to differ. In your example, at least, it looks like a single variable, because |
The error is still showing up even if I set return to |
If I try to treat it as a single variable, and try to define an unified type like this: var = list() # type: Union[Dict[str, str], List[str]] I get this error:
It feels that as far as type checker is concerned it probably would be easier to treat them as two different variables and then do I found this pattern in code that works with JSON data if that helps. |
The feature I'm working on won't support this use case yet, though it would be nice if mypy would support this better. Maybe we need to revisit #2008 at some point. Here's a workaround that works right now using Python 3.6+ syntax: ...
var: Union[Dict[str, str], List[str]]
if arg:
var = list()
var.append('something')
else:
var = dict()
var['key'] = 'something'
return var |
I saw that there is a similar bug around redefining a variable, but I believe this one is slightly different problem, here is the code:
The variable
var
while it uses the same name, it is technically a different variable, but mypy 0.630 is complaining about it:Ideally it should have no problem with it and at the point where there is
return
statement it should just do an union so the type would beUnion[Dict[str, str], List[str]]
The text was updated successfully, but these errors were encountered: