diff --git a/mypy/types.py b/mypy/types.py index 41a958ae93cc..5b8302de1ea1 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -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: @@ -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]): diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index 670ab42e1983..5df62c80168b 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -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 diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 081d21f14857..3cd509d44290 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -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 @@ -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" @@ -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]]"