-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
In case the context is |
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)' |
There was a problem hiding this comment.
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.
test-data/unit/semanal-errors.test
Outdated
class A: | ||
pass | ||
[out] | ||
main:2: error: Name 'A' already defined (line unavailable; possibly an import) |
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?).
There was a problem hiding this 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.
test-data/unit/semanal-errors.test
Outdated
class A: | ||
pass | ||
[out] | ||
main:2: error: Name 'A' already defined (possibly by an import) |
There was a problem hiding this comment.
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?
test-data/unit/semanal-errors.test
Outdated
|
||
[out] | ||
main:4: error: Name 'Point' already defined on line 2 | ||
|
There was a problem hiding this comment.
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.
test-data/unit/semanal-errors.test
Outdated
[builtins fixtures/dict.pyi] | ||
|
||
[out] | ||
main:4: error: Name 'Point' already defined on line 2 |
There was a problem hiding this comment.
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 bydef m(): ...
T = TypeVar('T')
followed byclass T: ...
- Ignored import (
import m # type: ignore
orfrom m import f # type: ignore
) followed by definition. - Type alias like
A = List[int]
followed by a redefinition.
test-data/unit/semanal-errors.test
Outdated
pass | ||
[out] | ||
main:12: error: Name 'f' already defined on line 3 | ||
|
There was a problem hiding this comment.
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).
This may be ready to merge (the last CR fixes was only to add more tests, no code changes). |
There was a problem hiding this 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!
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.