Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
stubtest: don't flag __class_getitem__ missing for PEP 695 generic stubs
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: #21253
  • Loading branch information
galsakuri committed Apr 16, 2026
commit dd25f94ac965dd9d0360e60ff4a0c99557d17fc1
4 changes: 4 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ def verify_typeinfo(
to_check.discard("__init__")
if is_runtime_typeddict:
to_check.discard("__new__")
# PEP 695 generic syntax (class Foo[T]: ...) implicitly provides __class_getitem__
# at runtime, so don't require an explicit stub definition.
if stub.defn.type_args is not None:
to_check.discard("__class_getitem__")

for entry in sorted(to_check):
mangled_entry = entry
Expand Down
13 changes: 13 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,19 @@ def test_dunders(self) -> Iterator[Case]:
runtime="class D:\n def __class_getitem__(cls, type): ...",
error=None,
)
if sys.version_info >= (3, 12):
# PEP 695 generic syntax implicitly provides __class_getitem__ at runtime
yield Case(
stub="class D2[T]: ...",
runtime="class D2:\n def __class_getitem__(cls, _): return cls",
error=None,
)
# Non-generic stub still reports missing __class_getitem__
yield Case(
stub="class D3: ...",
runtime="class D3:\n def __class_getitem__(cls, _): return cls",
error="D3.__class_getitem__",
)
yield Case(
stub="class E:\n def __getitem__(self, item: object) -> object: ...",
runtime="class E:\n def __getitem__(self, item: object, /) -> object: ...",
Expand Down
Loading