Skip to content

Add line # of previous definition (resubmit #3396) #3424

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

Merged
merged 7 commits into from
Jun 16, 2017
Merged

Conversation

pkch
Copy link
Contributor

@pkch pkch commented May 23, 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).

This is a resubmission of #3396 which caused the crash in #3415 and was reverted. The old PR is in commit 39c3058; after that, I added 2 more commits: the failing test that repros the crash, and the fix to it.

@pkch
Copy link
Contributor Author

pkch commented May 23, 2017

In case the context is None, or line number is -1, I put a message suggesting that the user look at imports. Is it ok? There could be other cases where this branch is hit that I don't list, but I thought this partial suggestion might still help.

mypy/semanal.py Outdated
if original_ctx.node and original_ctx.node.get_line() != -1:
extra_msg = ' on line {}'.format(original_ctx.node.get_line())
else:
extra_msg = ' (line unavailable; possibly an import)'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify the message a bit. What about just (possibly by an import). The 'line unavailable' part doesn't provide useful information really.

class A:
pass
[out]
main:2: error: Name 'A' already defined (line unavailable; possibly an import)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are further ideas for test cases:

  • Original definition is a decorated function.
  • Original definition is an overloaded function.
  • Original definition is a named tuple definition.
  • Original definition is a typed dict definition.
  • Original definition is a module (import m).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a module is simply imported twice, there's no error message. Did you mean some other test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkch Maybe Jukka means something like this:

import re
re = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that will cause a completely different error message (types are not compatible). It won't get to the point of complaining about duplicate definition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about

import re
import math
re = math

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, there's no error message at all (as expected?).

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Some style nits and ideas for additional test cases only.

class A:
pass
[out]
main:2: error: Name 'A' already defined (possibly by an import)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use # E: ... comments in these cases instead?


[out]
main:4: error: Name 'Point' already defined on line 2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra empty lines at the end of file.

[builtins fixtures/dict.pyi]

[out]
main:4: error: Name 'Point' already defined on line 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions for additional test cases:

  • import m followed by def m(): ...
  • T = TypeVar('T') followed by class T: ...
  • Ignored import (import m # type: ignore or from m import f # type: ignore) followed by definition.
  • Type alias like A = List[int] followed by a redefinition.

pass
[out]
main:12: error: Name 'f' already defined on line 3

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove second empty line (here and below).

@pkch
Copy link
Contributor Author

pkch commented Jun 8, 2017

This may be ready to merge (the last CR fixes was only to add more tests, no code changes).

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now. Thanks for the updates!

@JukkaL JukkaL merged commit 94e3f9c into python:master Jun 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants