Skip to content

stubtest: don't flag __class_getitem__ as missing for PEP 695 generic stubs#21258

Open
GalSakuri wants to merge 1 commit intopython:masterfrom
GalSakuri:fix/stubtest-class-getitem-pep695
Open

stubtest: don't flag __class_getitem__ as missing for PEP 695 generic stubs#21258
GalSakuri wants to merge 1 commit intopython:masterfrom
GalSakuri:fix/stubtest-class-getitem-pep695

Conversation

@GalSakuri
Copy link
Copy Markdown

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 uses class 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 via stub.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 their type_args is None. Non-generic stubs with a runtime __class_getitem__ still correctly report an error.

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
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.

stubtest false positive when __class_getitem__ is implemented in class body and corresponding body in stub is generic

1 participant