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 type comments crash inside generic definitions #16849

Conversation

picnixz
Copy link
Contributor

@picnixz picnixz commented Feb 1, 2024

Closes #16649

It's the first time I am contributing to mypy so I am not very familiar with how it works entirely behind the scene. The issue that I had is that a crash happens when using tuple type comments inside functions/classes that depend on a constrained type variable.

After investigation, the reason is that the type checker generates all possible definitions (since constraints are known) and expands the functions definitions and bodies accordingly. However, by doing so, a tuple type comment ('# type: (int, float)') would have a FakeInfo, so ExpandTypeVisitor would fail since it queries t.type.fullname.

By the way, feel free to change where my test should lie.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@picnixz
Copy link
Contributor Author

picnixz commented Jun 22, 2024

@AlexWaygood Could I kindly ask whether you're willing to review this PR?

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I kind of suspect it should be fixed upstream of this, but I spent a few minutes and couldn't find a good place. Since it fixes a crash and is very unlikely to cause problems, this lgtm

@hauntsaninja hauntsaninja merged commit 4ae632b into python:master Jul 1, 2024
18 checks passed
hauntsaninja pushed a commit that referenced this pull request Jul 1, 2024
Closes #16649

It's the first time I am contributing to mypy so I am not very familiar
with how it works entirely behind the scene. The issue that I had is
that a crash happens when using tuple type comments inside
functions/classes that depend on a *constrained* type variable.

After investigation, the reason is that the type checker generates all
possible definitions (since constraints are known) and expands the
functions definitions and bodies accordingly. However, by doing so, a
tuple type comment ('# type: (int, float)') would have a FakeInfo, so
`ExpandTypeVisitor` would fail since it queries `t.type.fullname`.

By the way, feel free to change where my test should lie.
@picnixz picnixz deleted the gh-16649-type-comments-and-constrained-generics branch July 2, 2024 06:53
@picnixz
Copy link
Contributor Author

picnixz commented Jul 2, 2024

Thank you!

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.

Crash when using type comments inside a generic class with constrained type variables.
2 participants