Skip to content
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

[stubgen] Preserve PEP 604 Unions in generated pyi files #14601

Merged
merged 1 commit into from
Feb 7, 2023
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
4 changes: 4 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
TypeList,
TypeStrVisitor,
UnboundType,
UnionType,
get_proper_type,
)
from mypy.visitor import NodeVisitor
Expand Down Expand Up @@ -326,6 +327,9 @@ def visit_none_type(self, t: NoneType) -> str:
def visit_type_list(self, t: TypeList) -> str:
return f"[{self.list_str(t.items)}]"

def visit_union_type(self, t: UnionType) -> str:
return " | ".join([item.accept(self) for item in t.items])

def args_str(self, args: Iterable[Type]) -> str:
"""Convert an array of arguments to strings and join the results with commas.

Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -2783,3 +2783,13 @@ T = TypeVar("T", bound=str | None)
from typing import TypeVar

T = TypeVar('T', bound=str | None)


[case testPEP604UnionType]
a: str | int

def f(x: str | None) -> None: ...
[out]
a: str | int

def f(x: str | None) -> None: ...