Skip to content

Type alias stubgen fix #18960

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,14 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
continue
if (
isinstance(lvalue, NameExpr)
and not self.is_private_name(lvalue.name)
# it is never an alias with explicit annotation
and not o.unanalyzed_type
and self.is_alias_expression(o.rvalue)
and not self.is_private_name(lvalue.name)
):
self.process_typealias(lvalue, o.rvalue)
continue
is_type_alias = o.unanalyzed_type and getattr(o.type, "name", None) == "TypeAlias"
if not o.unanalyzed_type or is_type_alias:
self.process_typealias(lvalue, o.rvalue)
continue

if isinstance(lvalue, (TupleExpr, ListExpr)):
items = lvalue.items
if isinstance(o.unanalyzed_type, TupleType): # type: ignore[misc]
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,15 @@ from typing import TypeVar
T = TypeVar('T')
alias = Union[T, List[T]]

[case testTypeAlias]
from typing import TypeAlias

alias: TypeAlias = tuple[int, str]

[out]
alias = tuple[int, str]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we should skip TypeAlias here. It should be like this, in my opinion:

Suggested change
alias = tuple[int, str]
from typing import TypeAlias
alias: TypeAlias = tuple[int, str]



[case testEllipsisAliasPreserved]

alias = Tuple[int, ...]
Expand Down