Skip to content

Regression related to forward reference and nested classes #18988

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
JukkaL opened this issue Apr 28, 2025 · 5 comments · Fixed by #19000
Closed

Regression related to forward reference and nested classes #18988

JukkaL opened this issue Apr 28, 2025 · 5 comments · Fixed by #19000
Labels
bug mypy got something wrong

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 28, 2025

This example started generating a false positive about an undefined name (simplified from a real-world generated protobuf stubs):

from typing import NewType

class W[T]: pass

class R:
    class M(W[Action.V], type):  # Name "Action.V" is not defined
        FOO = R.Action.V(0)
    class Action(metaclass=M):
        V = NewType('V', int)

class Action:
    pass

This was a side effect of #18625.

There may be some confusion between R.Action and Action classes.

This looks like a release blocker. This is happening in generated protobuf stubs code which can't be edited easily.

If this is non-trivial to fix, we could temporarily revert the original PR until a fix is available.

cc @ilevkivskyi as the author of the above PR

@JukkaL JukkaL added the bug mypy got something wrong label Apr 28, 2025
@ilevkivskyi
Copy link
Member

A possible quick fix may be to reduce precedence of global forward reference (although this may further complicate precedence rules for forward references). I can make a PR later today and check what is the fallout (likely there will be none, so it is more a question of documenting this clearly at some point).

@sterliakov
Copy link
Collaborator

Line 6 in your example produces a runtime NameError (verified on 3.12, 3.13, and my common sense), so if anything is to be changed here, that should only apply to stubs?..

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 29, 2025

There could also be a string literal forward reference in a non-stub file. The main thing is maintaining backward compatibility.

@ilevkivskyi
Copy link
Member

Yeah, I am going to go now with a minimal change to preserve backwards compatibility in this case. If there will be further complains from real code, we can further adjust the rules (and sorry for a delay, was distracted by unrelated things).

@ilevkivskyi
Copy link
Member

Here is a possible fix #19000

@JukkaL JukkaL closed this as completed in 70ee798 Apr 30, 2025
jhance pushed a commit that referenced this issue May 8, 2025
Fixes #18988

This should be a minimal change to restore backwards compatibility for
an edge case with forward references.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants