Skip to content

Commit 4b46d09

Browse files
[stubgen] Fix TypeAlias handling (#18960)
Fixes #18905 `TypeAlias` is an unanalyzed type, but is also an alias. So I changed a little bit of checking in the `visit_assignment_stmt` method. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1af7b2c commit 4b46d09

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

mypy/stubgen.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -920,13 +920,20 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
920920
continue
921921
if (
922922
isinstance(lvalue, NameExpr)
923-
and not self.is_private_name(lvalue.name)
924-
# it is never an alias with explicit annotation
925-
and not o.unanalyzed_type
926923
and self.is_alias_expression(o.rvalue)
924+
and not self.is_private_name(lvalue.name)
927925
):
928-
self.process_typealias(lvalue, o.rvalue)
929-
continue
926+
is_explicit_type_alias = (
927+
o.unanalyzed_type and getattr(o.type, "name", None) == "TypeAlias"
928+
)
929+
if is_explicit_type_alias:
930+
self.process_typealias(lvalue, o.rvalue, is_explicit_type_alias=True)
931+
continue
932+
933+
if not o.unanalyzed_type:
934+
self.process_typealias(lvalue, o.rvalue)
935+
continue
936+
930937
if isinstance(lvalue, (TupleExpr, ListExpr)):
931938
items = lvalue.items
932939
if isinstance(o.unanalyzed_type, TupleType): # type: ignore[misc]
@@ -1139,9 +1146,15 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:
11391146
else:
11401147
return False
11411148

1142-
def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None:
1149+
def process_typealias(
1150+
self, lvalue: NameExpr, rvalue: Expression, is_explicit_type_alias: bool = False
1151+
) -> None:
11431152
p = AliasPrinter(self)
1144-
self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n")
1153+
if is_explicit_type_alias:
1154+
self.import_tracker.require_name("TypeAlias")
1155+
self.add(f"{self._indent}{lvalue.name}: TypeAlias = {rvalue.accept(p)}\n")
1156+
else:
1157+
self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n")
11451158
self.record_name(lvalue.name)
11461159
self._vars[-1].append(lvalue.name)
11471160

test-data/unit/stubgen.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,19 @@ from typing import TypeVar
15441544
T = TypeVar('T')
15451545
alias = Union[T, List[T]]
15461546

1547+
[case testExplicitTypeAlias]
1548+
from typing import TypeAlias
1549+
1550+
explicit_alias: TypeAlias = tuple[int, str]
1551+
implicit_alias = list[int]
1552+
1553+
[out]
1554+
from typing import TypeAlias
1555+
1556+
explicit_alias: TypeAlias = tuple[int, str]
1557+
implicit_alias = list[int]
1558+
1559+
15471560
[case testEllipsisAliasPreserved]
15481561

15491562
alias = Tuple[int, ...]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy