Skip to content

Use PEP 604 syntax for TypeStrVisitor #19179

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 1 commit into from
May 30, 2025
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
10 changes: 6 additions & 4 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3489,8 +3489,9 @@ def visit_literal_type(self, t: LiteralType, /) -> str:
return f"Literal[{t.value_repr()}]"

def visit_union_type(self, t: UnionType, /) -> str:
s = self.list_str(t.items)
return f"Union[{s}]"
use_or_syntax = self.options.use_or_syntax()
s = self.list_str(t.items, use_or_syntax=use_or_syntax)
return s if use_or_syntax else f"Union[{s}]"

def visit_partial_type(self, t: PartialType, /) -> str:
if t.type is None:
Expand Down Expand Up @@ -3523,14 +3524,15 @@ def visit_type_alias_type(self, t: TypeAliasType, /) -> str:
def visit_unpack_type(self, t: UnpackType, /) -> str:
return f"Unpack[{t.type.accept(self)}]"

def list_str(self, a: Iterable[Type]) -> str:
def list_str(self, a: Iterable[Type], *, use_or_syntax: bool = False) -> str:
"""Convert items of an array to strings (pretty-print types)
and join the results with commas.
"""
res = []
for t in a:
res.append(t.accept(self))
return ", ".join(res)
sep = ", " if not use_or_syntax else " | "
return sep.join(res)


class TrivialSyntheticTypeTranslator(TypeTranslator, SyntheticTypeVisitor[Type]):
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -10436,14 +10436,14 @@ D = "y"
C = str
D = int
[out]
a.py:4: note: Revealed type is "Union[builtins.int, builtins.str]"
a.py:4: note: Revealed type is "builtins.int | builtins.str"
==
a.py:2: error: Unsupported left operand type for | ("str")
a.py:3: error: Variable "a.A" is not valid as a type
a.py:3: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
a.py:4: note: Revealed type is "A?"
==
a.py:4: note: Revealed type is "Union[builtins.str, builtins.int]"
a.py:4: note: Revealed type is "builtins.str | builtins.int"

[case testUnionOfSimilarCallablesCrash]
import b
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,8 @@ def foo(x: T) -> T:
return x
[out]
_testTypeAliasWithNewStyleUnion.py:5: note: Revealed type is "typing._SpecialForm"
_testTypeAliasWithNewStyleUnion.py:25: note: Revealed type is "Union[type[builtins.int], builtins.str]"
_testTypeAliasWithNewStyleUnion.py:28: note: Revealed type is "Union[type[builtins.int], builtins.str]"
_testTypeAliasWithNewStyleUnion.py:25: note: Revealed type is "type[builtins.int] | builtins.str"
_testTypeAliasWithNewStyleUnion.py:28: note: Revealed type is "type[builtins.int] | builtins.str"

[case testTypeAliasWithNewStyleUnionInStub]
import m
Expand Down Expand Up @@ -1711,7 +1711,7 @@ reveal_type(e.foo)
reveal_type(E.Y.foo)
[out]
_testEnumNameWorkCorrectlyOn311.py:11: note: Revealed type is "builtins.str"
_testEnumNameWorkCorrectlyOn311.py:12: note: Revealed type is "Union[Literal[1]?, Literal[2]?]"
_testEnumNameWorkCorrectlyOn311.py:12: note: Revealed type is "Literal[1]? | Literal[2]?"
_testEnumNameWorkCorrectlyOn311.py:13: note: Revealed type is "Literal['X']?"
_testEnumNameWorkCorrectlyOn311.py:14: note: Revealed type is "builtins.int"
_testEnumNameWorkCorrectlyOn311.py:15: note: Revealed type is "builtins.int"
Expand Down Expand Up @@ -1780,9 +1780,9 @@ WrongEllipsis = tuple[float, float, ...] | str # Error

reveal_type(tuple[int, str]((1, "x")))
[out]
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "Union[builtins.str, tuple[builtins.float, builtins.float, builtins.str]]"
_testTupleWithDifferentArgsPy310.py:16: note: Revealed type is "Union[tuple[builtins.float], builtins.str]"
_testTupleWithDifferentArgsPy310.py:17: note: Revealed type is "Union[builtins.tuple[builtins.float, ...], builtins.str]"
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "builtins.str | tuple[builtins.float, builtins.float, builtins.str]"
_testTupleWithDifferentArgsPy310.py:16: note: Revealed type is "tuple[builtins.float] | builtins.str"
_testTupleWithDifferentArgsPy310.py:17: note: Revealed type is "builtins.tuple[builtins.float, ...] | builtins.str"
_testTupleWithDifferentArgsPy310.py:18: note: Revealed type is "tuple[builtins.float, builtins.str]"
_testTupleWithDifferentArgsPy310.py:19: note: Revealed type is "builtins.tuple[builtins.float, ...]"
_testTupleWithDifferentArgsPy310.py:20: note: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]"
Expand Down