Skip to content
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

Fix crash with function redefinition #14064

Merged
merged 2 commits into from
Nov 11, 2022

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Nov 11, 2022

Fixes #14027 (issue was surfaced by #13509)

@github-actions

This comment has been minimized.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Seems OK as quick fix but the error message is still pretty bad.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Don't want to block this, but I think I have a better solution, see comment below.

# weirdly, needs to be an empty tuple. empty lists and various other special assignments
# don't trigger the bug
cond = ()
if cond:
Copy link
Member

@ilevkivskyi ilevkivskyi Nov 11, 2022

Choose a reason for hiding this comment

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

A simpler test would be just if False: The problem is that mypy considers if branch unreachable, and therefore doesn't type check it, and therefore the original definition doesn't get its type inferred (and stays None).

I think a better solution is to simply not show any error in this case:

  • It will be more consistent with not showing error in unreachable code
  • Technically, there is no redefinition, only one definition is ever executed.

Copy link
Member

Choose a reason for hiding this comment

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

Note however, this will be inconsistent with foo: int = 1, where user will get an error, even if the first definition is unreachable, but IMO this is OK.

@ilevkivskyi
Copy link
Member

Btw it is a wider problem, some code may be semantically reachable (because types of expressions in branches are not known), but not type checked, because during type-checking expression type is inferred as something false-y. A possible option to explore in the future (not now) is to still actually type check unreachable code, only for the purpose of type inference, but don't show any errors there.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja merged commit 67855fa into python:master Nov 11, 2022
@hauntsaninja hauntsaninja deleted the fix-redef-crash branch November 11, 2022 20:08
hauntsaninja added a commit that referenced this pull request Nov 11, 2022
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.

latest mypy 0.990 crashes with SQLAlchemy main
3 participants