Skip to content

Commit 4187680

Browse files
committed
[stubtest] Allow runtime-existing aliases of @type_check_only classes
1 parent 7c4ec52 commit 4187680

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

mypy/stubtest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,13 @@ def _verify_metaclass(
506506

507507
@verify.register(nodes.TypeInfo)
508508
def verify_typeinfo(
509-
stub: nodes.TypeInfo, runtime: MaybeMissing[type[Any]], object_path: list[str]
509+
stub: nodes.TypeInfo,
510+
runtime: MaybeMissing[type[Any]],
511+
object_path: list[str],
512+
*,
513+
is_alias_target: bool = False,
510514
) -> Iterator[Error]:
511-
if stub.is_type_check_only:
515+
if stub.is_type_check_only and not is_alias_target:
512516
# This type only exists in stubs, we only check that the runtime part
513517
# is missing. Other checks are not required.
514518
if not isinstance(runtime, Missing):
@@ -1449,7 +1453,7 @@ def verify_typealias(
14491453
# Okay, either we couldn't construct a fullname
14501454
# or the fullname of the stub didn't match the fullname of the runtime.
14511455
# Fallback to a full structural check of the runtime vis-a-vis the stub.
1452-
yield from verify(stub_origin, runtime_origin, object_path)
1456+
yield from verify_typeinfo(stub_origin, runtime_origin, object_path, is_alias_target=True)
14531457
return
14541458
if isinstance(stub_target, mypy.types.UnionType):
14551459
# complain if runtime is not a Union or UnionType

mypy/test/teststubtest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,17 @@ def func2() -> None: ...
24682468
runtime="def func2() -> None: ...",
24692469
error="func2",
24702470
)
2471+
# A type that exists at runtime is allowed to alias a type marked
2472+
# as '@type_check_only' in the stubs.
2473+
yield Case(
2474+
stub="""
2475+
@type_check_only
2476+
class _X1: ...
2477+
X2 = _X1
2478+
""",
2479+
runtime="class X2: ...",
2480+
error=None,
2481+
)
24712482

24722483

24732484
def remove_color_code(s: str) -> str:

0 commit comments

Comments
 (0)