Skip to content

stubtest: check type aliases #10908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 2, 2021
Merged
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
32 changes: 28 additions & 4 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__")
Expand Down Expand Up @@ -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


Expand Down
24 changes: 24 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down