Skip to content

Commit

Permalink
stubtest: error if type alias doesn't exist at runtime (#12608)
Browse files Browse the repository at this point in the history
Co-authored-by: hauntsaninja <>
Co-authored-by: Shantanu <[email protected]>
  • Loading branch information
AlexWaygood and hauntsaninja authored Apr 18, 2022
1 parent 9cab296 commit 9e9de71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit 9e9de71

Please sign in to comment.