Description
As a follow-up to #15070, display union types using the new X | Y
syntax in error messages when targeting Python 3.10 or later.
Examples:
- Instead of
Union[int, str]
, displayint | str
. - Instead of
Optional[str]
, displaystr | None
If there is None
in a union type, perhaps always display the None
as the final item. This way we'd never show a type like None | str
(even if type inference would produce this internal representation), since it's not idiomatic.
Generalize the implementation technique used in #15070, which added a version check for using list[x]
instead of List[x]
in error messages on Python 3.9 and later. In particular, use the hidden command line option in tests to always use the old syntax, so that tests will run consistently. Add specific test cases that enable the X | Y
syntax only in the new tests that test this syntax.