-
-
Notifications
You must be signed in to change notification settings - Fork 3k
"Name already defined" error is confusing #4722
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
That's intentional, because after the if-else X has a useless type. |
|
I was referring to your example. Apparently you didn't capture the essence of the real code you were having trouble with. There are a number of special cases implemented when two branches define different but equivalent (to the user) types, and your example wasn't one of them. It seems defining two different classes isn't one of them either. So I recommend putting |
Ok, thanks! |
In any case, "Name already defined" seems like a very bad explanation for the error. The problem is not that X is already defined—mutating variables is something you do in Python all the time. The problem is that you're trying to reassign something to X that's of a different type. |
OK, reopening as a usability issue -- long-term maintainers get blind for those little usability problems, so thanks for bringing that up! |
I've just run into the same error message, but in a different scenario. I'm trying to generate some usable stubs for the This is what
Which produces the following error:
It would be helpful if the error message could tell me where the original definition came from, and possible also the type of each so I can check whether the incompatibility is a type error or a naming/import error. |
I believe @glenjamin's problem would be fixed by passing in Line 3192 in 8f8d136
(i.e. changing this line to self.name_already_defined(name, context, existing) ).
In my test code, it now prints |
Yes, that sounds like it would be an improvement! |
In semanal.py, name_already_defined takes an optional original_ctx: Optional[SymbolTableNode] argument which is used to print the line number of the original context where a name was defined already. In many cases, the code doesn't pass anything for this argument, even though there is an original context that makes sense. This PR does this for the name_already_defined call in add_symbol, as discussed in #4722.
Can this issue be closed as the PR was merged, or is this unresolved? |
Let's close it. |
Uh oh!
There was an error while loading. Please reload this page.
Mypy complains if an if-else statement redefines a global variable with a class definition.
Failing code:
Error:
mypy version: 0.570
The text was updated successfully, but these errors were encountered: