Skip to content

Use X | Y union syntax in error messages on recent Python versions #15082

Closed

Description

@JukkaL

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], display int | str.
  • Instead of Optional[str], display str | 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions