Skip to content

Commit 057508b

Browse files
authored
Use PEP 604 syntax for TypeStrVisitor (#19179)
1 parent 3957025 commit 057508b

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

mypy/types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,8 +3489,9 @@ def visit_literal_type(self, t: LiteralType, /) -> str:
34893489
return f"Literal[{t.value_repr()}]"
34903490

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

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

3526-
def list_str(self, a: Iterable[Type]) -> str:
3527+
def list_str(self, a: Iterable[Type], *, use_or_syntax: bool = False) -> str:
35273528
"""Convert items of an array to strings (pretty-print types)
35283529
and join the results with commas.
35293530
"""
35303531
res = []
35313532
for t in a:
35323533
res.append(t.accept(self))
3533-
return ", ".join(res)
3534+
sep = ", " if not use_or_syntax else " | "
3535+
return sep.join(res)
35343536

35353537

35363538
class TrivialSyntheticTypeTranslator(TypeTranslator, SyntheticTypeVisitor[Type]):

test-data/unit/fine-grained.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10436,14 +10436,14 @@ D = "y"
1043610436
C = str
1043710437
D = int
1043810438
[out]
10439-
a.py:4: note: Revealed type is "Union[builtins.int, builtins.str]"
10439+
a.py:4: note: Revealed type is "builtins.int | builtins.str"
1044010440
==
1044110441
a.py:2: error: Unsupported left operand type for | ("str")
1044210442
a.py:3: error: Variable "a.A" is not valid as a type
1044310443
a.py:3: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
1044410444
a.py:4: note: Revealed type is "A?"
1044510445
==
10446-
a.py:4: note: Revealed type is "Union[builtins.str, builtins.int]"
10446+
a.py:4: note: Revealed type is "builtins.str | builtins.int"
1044710447

1044810448
[case testUnionOfSimilarCallablesCrash]
1044910449
import b

test-data/unit/pythoneval.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,8 @@ def foo(x: T) -> T:
16321632
return x
16331633
[out]
16341634
_testTypeAliasWithNewStyleUnion.py:5: note: Revealed type is "typing._SpecialForm"
1635-
_testTypeAliasWithNewStyleUnion.py:25: note: Revealed type is "Union[type[builtins.int], builtins.str]"
1636-
_testTypeAliasWithNewStyleUnion.py:28: note: Revealed type is "Union[type[builtins.int], builtins.str]"
1635+
_testTypeAliasWithNewStyleUnion.py:25: note: Revealed type is "type[builtins.int] | builtins.str"
1636+
_testTypeAliasWithNewStyleUnion.py:28: note: Revealed type is "type[builtins.int] | builtins.str"
16371637

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

17811781
reveal_type(tuple[int, str]((1, "x")))
17821782
[out]
1783-
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "Union[builtins.str, tuple[builtins.float, builtins.float, builtins.str]]"
1784-
_testTupleWithDifferentArgsPy310.py:16: note: Revealed type is "Union[tuple[builtins.float], builtins.str]"
1785-
_testTupleWithDifferentArgsPy310.py:17: note: Revealed type is "Union[builtins.tuple[builtins.float, ...], builtins.str]"
1783+
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "builtins.str | tuple[builtins.float, builtins.float, builtins.str]"
1784+
_testTupleWithDifferentArgsPy310.py:16: note: Revealed type is "tuple[builtins.float] | builtins.str"
1785+
_testTupleWithDifferentArgsPy310.py:17: note: Revealed type is "builtins.tuple[builtins.float, ...] | builtins.str"
17861786
_testTupleWithDifferentArgsPy310.py:18: note: Revealed type is "tuple[builtins.float, builtins.str]"
17871787
_testTupleWithDifferentArgsPy310.py:19: note: Revealed type is "builtins.tuple[builtins.float, ...]"
17881788
_testTupleWithDifferentArgsPy310.py:20: note: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]"

0 commit comments

Comments
 (0)