Skip to content

Commit 9e9de71

Browse files
stubtest: error if type alias doesn't exist at runtime (#12608)
Co-authored-by: hauntsaninja <> Co-authored-by: Shantanu <[email protected]>
1 parent 9cab296 commit 9e9de71

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

mypy/stubtest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,13 @@ def verify_decorator(
955955
def verify_typealias(
956956
stub: nodes.TypeAlias, runtime: MaybeMissing[Any], object_path: List[str]
957957
) -> Iterator[Error]:
958+
stub_target = mypy.types.get_proper_type(stub.target)
958959
if isinstance(runtime, Missing):
959-
# ignore type aliases that don't have a runtime counterpart
960+
yield Error(
961+
object_path, "is not present at runtime", stub, runtime,
962+
stub_desc=f"Type alias for: {stub_target}"
963+
)
960964
return
961-
stub_target = mypy.types.get_proper_type(stub.target)
962965
if isinstance(stub_target, mypy.types.Instance):
963966
yield from verify(stub_target.type, runtime, object_path)
964967
return

mypy/test/teststubtest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,18 @@ class Y: ...
712712
runtime="A = (int, str)",
713713
error="A",
714714
)
715+
# Error if an alias isn't present at runtime...
716+
yield Case(
717+
stub="B = str",
718+
runtime="",
719+
error="B"
720+
)
721+
# ... but only if the alias isn't private
722+
yield Case(
723+
stub="_C = int",
724+
runtime="",
725+
error=None
726+
)
715727

716728
@collect_cases
717729
def test_enum(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)