diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 7fa0f5937f99..d3bc40bc27e8 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -955,10 +955,13 @@ def verify_decorator( def verify_typealias( stub: nodes.TypeAlias, runtime: MaybeMissing[Any], object_path: List[str] ) -> Iterator[Error]: + stub_target = mypy.types.get_proper_type(stub.target) if isinstance(runtime, Missing): - # ignore type aliases that don't have a runtime counterpart + yield Error( + object_path, "is not present at runtime", stub, runtime, + stub_desc=f"Type alias for: {stub_target}" + ) return - stub_target = mypy.types.get_proper_type(stub.target) if isinstance(stub_target, mypy.types.Instance): yield from verify(stub_target.type, runtime, object_path) return diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 1cdcd34c666c..869d2e110a66 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -712,6 +712,18 @@ class Y: ... runtime="A = (int, str)", error="A", ) + # Error if an alias isn't present at runtime... + yield Case( + stub="B = str", + runtime="", + error="B" + ) + # ... but only if the alias isn't private + yield Case( + stub="_C = int", + runtime="", + error=None + ) @collect_cases def test_enum(self) -> Iterator[Case]: