From 303f01cc35d97dae3073e8991c79857aa1a53182 Mon Sep 17 00:00:00 2001 From: Alexey Makridenko Date: Thu, 24 Apr 2025 11:05:17 +0200 Subject: [PATCH 1/2] fix TypeAlias stubgen --- mypy/stubgen.py | 11 ++++++----- test-data/unit/stubgen.test | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 881686adc5ed..ce2172425327 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -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] diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index bf17c34b99a7..e8698b70c331 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -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] + + [case testEllipsisAliasPreserved] alias = Tuple[int, ...] From 6e1bcea3f7a5a61b73a96bde55380c534b4dc17d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Apr 2025 09:12:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/stubgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index ce2172425327..f20cbb302b9e 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -923,7 +923,7 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None: and self.is_alias_expression(o.rvalue) and not self.is_private_name(lvalue.name) ): - is_type_alias = o.unanalyzed_type and getattr(o.type, 'name', None) == 'TypeAlias' + 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