Skip to content

Confusing error message when class definition duplicated across if-else #1874

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

Closed
sangsta opened this issue Jul 15, 2016 · 3 comments
Closed
Labels

Comments

@sangsta
Copy link

sangsta commented Jul 15, 2016

I have a block of code that looks like this:

if foo:
    class A(object):
        ...
else:
    class A(object):
        ...

mypy complains for the second class definition:

error: Name 'A' already defined

However, this is technically not true.

@gvanrossum
Copy link
Member

Yeah, this is a common issue. There are plans to fix this but they haven't landed yet. For now, just put # type: ignore on the second class statement.

@gnprice gnprice changed the title Bug in class definition within if-else blocks Confusing error message when class definition duplicated across if-else Jul 16, 2016
@gnprice
Copy link
Collaborator

gnprice commented Jul 16, 2016

It seems like there are (at least) two or three things that could be done here:

  • We don't allow these, and in many cases it'd be helpful if we did. This is complicated to get right, though. This is covered in existing issues.
  • The error message is confusingly worded, because at runtime the class is never "already" defined.
  • The error message isn't specific about where the other definition is -- which, when the code isn't a tiny example program, leaves the user to wonder if the puzzling message is really referring to that other definition up in the if or to some other place where it really was "already" defined, if they've even spotted the previous definition yet.

I think fixing the last point would in itself mitigate the confusing wording a lot, because it'd be totally explicit what other definition the checker has in mind and the user can figure out that that might be a problem even when they're never duplicated at runtime. So if nothing else, we should add a previously defined here note on that error.

@gnprice gnprice added this to the 0.4.x milestone Jul 16, 2016
@JukkaL JukkaL added the bug mypy got something wrong label Jan 16, 2017
@gvanrossum gvanrossum removed this from the 0.4.x milestone Mar 29, 2017
gvanrossum pushed a commit that referenced this issue May 22, 2017
*Partial* fix for #1874 - just improve the error message. The duplicate definitions in different if/else branches are still not allowed.
JukkaL pushed a commit that referenced this issue Jun 16, 2017
Partial fix for #1874: improve the error message. The duplicate definitions in 
different if/else branches are still not allowed (this is much harder to fix).
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 20, 2018

The message is now Name 'A' already defined on line 2, which partially addresses the issue. If this is still too confusing, we can reopen the issue.

@JukkaL JukkaL closed this as completed Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants