diff --git a/mypy/stubtest.py b/mypy/stubtest.py index f48d0ff05ffe..138f126c9d1a 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -858,8 +858,31 @@ def verify_decorator( def verify_typealias( stub: nodes.TypeAlias, runtime: MaybeMissing[Any], object_path: List[str] ) -> Iterator[Error]: - if False: - yield None + if isinstance(runtime, Missing): + # ignore type aliases that don't have a runtime counterpart + 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 + if isinstance(stub_target, mypy.types.UnionType): + if not getattr(runtime, "__origin__", None) is Union: + yield Error(object_path, "is not a Union", stub, runtime, stub_desc=str(stub_target)) + # could check Union contents here... + return + if isinstance(stub_target, mypy.types.TupleType): + if tuple not in getattr(runtime, "__mro__", ()): + yield Error( + object_path, "is not a subclass of tuple", stub, runtime, + stub_desc=str(stub_target) + ) + # could check Tuple contents here... + return + if isinstance(stub_target, mypy.types.AnyType): + return + yield Error( + object_path, "is not a recognised type alias", stub, runtime, stub_desc=str(stub_target) + ) SPECIAL_DUNDERS = ("__init__", "__new__", "__call__", "__init_subclass__", "__class_getitem__") @@ -887,10 +910,11 @@ def is_probably_a_function(runtime: Any) -> bool: def safe_inspect_signature(runtime: Any) -> Optional[inspect.Signature]: try: return inspect.signature(runtime) - except (ValueError, RuntimeError, TypeError): - # inspect.signature throws sometimes + except Exception: + # inspect.signature throws ValueError all the time # catch RuntimeError because of https://bugs.python.org/issue39504 # catch TypeError because of https://github.com/python/typeshed/pull/5762 + # catch AttributeError because of inspect.signature(_curses.window.border) return None diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 0ce5df00aafd..7275037a9246 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -571,6 +571,30 @@ def __init__(self): error=None, ) + @collect_cases + def test_type_alias(self) -> Iterator[Case]: + yield Case( + stub=""" + class X: + def f(self) -> None: ... + Y = X + """, + runtime=""" + class X: + def f(self) -> None: ... + class Y: ... + """, + error="Y.f", + ) + yield Case( + stub=""" + from typing import Tuple + A = Tuple[int, str] + """, + runtime="A = (int, str)", + error="A", + ) + @collect_cases def test_enum(self) -> Iterator[Case]: yield Case(