Skip to content

stubtest: emit error if a stub defines a public type alias that doesn't exist at runtime #12608

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 5 commits into from
Apr 18, 2022
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
7 changes: 5 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,10 +956,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