Skip to content

Fix undefined error line number #3420

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,10 @@ def name_not_defined(self, name: str, ctx: Context) -> None:
def name_already_defined(self, name: str, ctx: Context,
original_ctx: Optional[SymbolTableNode] = None) -> None:
if original_ctx:
extra_msg = ' on line {}'.format(original_ctx.node.get_line())
if original_ctx.node and original_ctx.node.get_line() != -1:
Copy link
Member

Choose a reason for hiding this comment

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

IIUC the idea, then I would check for original_ctx.mod_id, and if it is not None then mention the module where original definition occurred. Otherwise, I would just set extra_msg = ''.

Copy link
Member

Choose a reason for hiding this comment

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

(This comment belongs to the else: branch below.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're trying to catch the cases where the definition is repeated twice in the same module, and indicate the line where the first one occurred. I don't think mod_id would ever be a different module if it gets to this line, right? It could be either None (when the definition was added from an import perhaps) or the same as the current module.

extra_msg = ' on line {}'.format(original_ctx.node.get_line())
else:
extra_msg = ' (line unavailable; possibly an import)'
else:
extra_msg = ''
self.fail("Name '{}' already defined{}".format(name, extra_msg), ctx)
Expand Down