Fix __new__ forward reference class aliasing - #583
Open
jaideeppyne wants to merge 1 commit into
Open
Conversation
Owner
|
Is this substantially different from #581? |
Contributor
Author
|
No, not substantially: both address #578 and remove the injected I missed #581 when opening this. Having compared the patches, #581 also covers subclass construction, argument checking without an existing class cell, and Enum construction, without changing the shared forward-reference evaluator. I don't see a separate use case here that warrants reviewing both implementations, and I defer to #581. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #578.
When instrumentation sees a string self-reference in
__new__, it currently rewrites the annotation to the class name and injectsClassName = clsinto the method body. That avoids the class-creation-timeNameErrorfrom #398, but it leaves a runtime-visible local alias. Later subclass construction then makesClassNameresolve to the subclass inside the base__new__body.This changes exact string self-references in
__new__toForwardRef("ClassName")instead of injecting a local class-name alias. Forward reference evaluation still falls back tomemo.self_typewhen the referenced name is not yet resolvable and it matches the class currently being created, preserving the #398 behavior without shadowing user code after class creation.Added a regression that constructs a subclass under both
@typecheckedand import-hook instrumentation and asserts the base class name still resolves to the base class insideBase.__new__.Validation run locally:
uv run --group test mypy srcis blocked in this local environment by an unrelated missingtypeshed.stdlib.typesimport fromsrc/typeguard/_decorators.py.