Skip to content
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

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

Closed
JukkaL opened this issue Apr 19, 2023 · 0 comments · Fixed by #15102
Closed

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

JukkaL opened this issue Apr 19, 2023 · 0 comments · Fixed by #15102

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 19, 2023

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.

JukkaL pushed a commit that referenced this issue Apr 24, 2023
This should fix #15082:
If the python version is set to 3.10 or later union error reports are
going to be displayed using the `X | Y` syntax.
If None is found in a union it is shown as the last element in order to
improve visibility (`Union[bool, None, str]` becomes `"bool | str |
None"`.
To achieve this an option `use_or_syntax()` is created that is set to
true if the python version is 3.10 or later.

For testing a hidden flag --force-union-syntax is used that sets the
option to false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant