stubtest: don't flag __class_getitem__ as missing for PEP 695 generic stubs#21258
Open
GalSakuri wants to merge 1 commit intopython:masterfrom
Open
stubtest: don't flag __class_getitem__ as missing for PEP 695 generic stubs#21258GalSakuri wants to merge 1 commit intopython:masterfrom
GalSakuri wants to merge 1 commit intopython:masterfrom
Conversation
PEP 695 generic syntax (class Foo[T]: ...) implicitly provides __class_getitem__ at runtime. When a stub uses this syntax and the runtime class defines __class_getitem__ explicitly, stubtest was incorrectly reporting it as missing from the stub. Fix: in verify_typeinfo(), discard "__class_getitem__" from the set of attributes to check when the stub class has PEP 695 type parameters (ClassDef.type_args is not None). Fixes: python#21253
c459acd to
dd25f94
Compare
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 #21253
When a runtime class defines
__class_getitem__explicitly and its stub uses PEP 695 generic syntax (class Foo[T]: ...), stubtest incorrectly reports that__class_getitem__is not present in the stub.PEP 695 generic syntax implicitly provides
__class_getitem__at runtime — Python adds it automatically when the class is defined with type parameter syntax. So a stub that usesclass Foo[T]: ...is correct and should not require an explicit__class_getitem__definition.The fix adds a discard of
__class_getitem__from the to_check set when the stub class uses PEP 695 syntax (detected viastub.defn.type_args is not None), mirroring the existing pattern used for__init__in protocol classes and__new__in TypedDicts.Traditional
Generic[T]subclasses are unaffected since theirtype_argsisNone. Non-generic stubs with a runtime__class_getitem__still correctly report an error.