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
10 changes: 10 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,16 @@ def verify_typeinfo(
mangled_entry = f"_{stub.name.lstrip('_')}{entry}"
stub_to_verify = next((t.names[entry].node for t in stub.mro if entry in t.names), MISSING)
assert stub_to_verify is not None
# A generic class is subscriptable at the type level via its type
# parameters; a runtime `__class_getitem__` is just the implementation
# detail backing that. Don't report it as missing from a stub that
# already declares the class as generic. See #21253.
if (
entry == "__class_getitem__"
and isinstance(stub_to_verify, Missing)
and stub.is_generic()
):
continue
try:
try:
runtime_attr = getattr(runtime, mangled_entry)
Expand Down
27 changes: 27 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,33 @@ def f(self, number, text): pass
error=None,
)

@collect_cases
def test_class_getitem_on_generic(self) -> Iterator[Case]:
# A runtime `__class_getitem__` backs subscriptability that a stub
# expresses via generic type parameters, so it should not be reported
# as missing from a generic stub. See #21253.
yield Case(
stub="""
from typing import Generic, TypeVar
_T = TypeVar("_T")
class GenericClassGetItem(Generic[_T]): ...
""",
runtime="""
class GenericClassGetItem:
def __class_getitem__(cls, item, /): return cls
""",
error=None,
)
# But a non-generic stub should still flag the extra runtime method.
yield Case(
stub="class PlainClassGetItem: ...",
runtime="""
class PlainClassGetItem:
def __class_getitem__(cls, item, /): return cls
""",
error="PlainClassGetItem.__class_getitem__",
)

@collect_cases
def test_types(self) -> Iterator[Case]:
yield Case(
Expand Down
Loading