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

Merged
merged 10 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 8 additions & 6 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 Expand Up @@ -1141,7 +1142,7 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:

def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None:
p = AliasPrinter(self)
self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n")
self.add(f"{self._indent}{lvalue.name}: TypeAlias = {rvalue.accept(p)}\n")
self.record_name(lvalue.name)
self._vars[-1].append(lvalue.name)

Expand Down Expand Up @@ -1194,6 +1195,7 @@ def visit_import_from(self, o: ImportFrom) -> None:
self.import_tracker.reexport(name)
as_name = name
import_names.append((name, as_name))
# here's required = False
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# here's required = False

Copy link
Contributor Author

@makridenko makridenko May 1, 2025

Choose a reason for hiding this comment

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

fixed this, thank you

self.import_tracker.add_import_from("." * relative + module, import_names)
self._vars[-1].extend(alias or name for name, alias in import_names)
for name, alias in import_names:
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]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
alias: TypeAlias = tuple[int, str]
explicit_alias: TypeAlias = tuple[int, str]
implicit_alias = list[int]

Copy link
Contributor Author

@makridenko makridenko May 1, 2025

Choose a reason for hiding this comment

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

fixed this, thank you


[out]
alias: TypeAlias = 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.

Don't forget to add an import 👍

Copy link
Contributor Author

@makridenko makridenko May 1, 2025

Choose a reason for hiding this comment

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

@sobolevn sorry, this PR still WIP, I didn't mean to ask for a review

Copy link
Member

@sobolevn sobolevn May 1, 2025

Choose a reason for hiding this comment

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

No worries, marked as a draft for now :)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
alias: TypeAlias = tuple[int, str]
explicit_alias: TypeAlias = tuple[int, str]
implicit_alias = list[int]

Copy link
Contributor Author

@makridenko makridenko May 1, 2025

Choose a reason for hiding this comment

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

fixed this, thank you



[case testEllipsisAliasPreserved]

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