stubtest: don't flag __class_getitem__ as missing on a generic stub#21582
Open
ShirGanon wants to merge 1 commit into
Open
stubtest: don't flag __class_getitem__ as missing on a generic stub#21582ShirGanon wants to merge 1 commit into
ShirGanon wants to merge 1 commit into
Conversation
A class that implements `__class_getitem__` at runtime to support subscription is correctly modelled in a stub by declaring the class as generic (PEP 695 `class C[T]` or `Generic[T]`). stubtest nevertheless reported `C.__class_getitem__ is not present in stub` in that case. Skip the `__class_getitem__` runtime member when the stub class is generic (`TypeInfo.is_generic()`) and does not explicitly declare it. Non-generic stubs still report the missing method as before. Fixes python#21253
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
A class that implements
__class_getitem__at runtime (to make itself subscriptable) is correctly modelled in a stub by declaring the class as generic — either with PEP 695 syntax (class C[T]) orGeneric[T]. stubtest, however, reported a false positive in that case:verify_typeinfoiterates the runtime class members and, finding__class_getitem__on the runtime class with no counterpart in the (generic) stub, flagged it as missing.Change
In
verify_typeinfo, skip the__class_getitem__runtime member when the stub class is generic (TypeInfo.is_generic()) and does not explicitly declare it — the generic type parameters already express that subscriptability at the type level. A non-generic stub that omits a runtime__class_getitem__is still reported as before, so the change only suppresses the false positive.Testing
test_class_getitem_on_genericinmypy/test/teststubtest.pywith two cases:Generic[T]) + runtime__class_getitem__→ no error;__class_getitem__→ still reports__class_getitem__ is not present in stub.stubtest.pychange and passes with it.mypy/test/teststubtest.pysuite passes (68 tests), no regressions.black,ruff, and mypy self-check onmypy/stubtest.pyare clean.Disclosure: drafted with Claude Code; reviewed, understood, and tested locally.