You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect error for unions syntax in comments and strings (Python 3.9) #19722
Minimal example python code is attached below. mypy output is:
main.py:4: error: X | Y syntax for unions requires Python 3.10 [syntax]
main.py:12: error: X | Y syntax for unions requires Python 3.10 [syntax]
Found 2 errors in 1 file (checked 1 source file)
Mypy currently raises X | Y syntax for unions requires Python 3.10 when string-quoted type hints include the | operator under Python < 3.10 (e.g., "str | None"). Since these annotations are provided as strings (permitted forward references under PEP 563), they should not be subject to version-specific syntax restrictions. For compatibility with Python 3.9, mypy should treat such cases as plain forward references rather than parsing them with Python 3.10+ rules.
As a workaround, Optional[str] or Union[str, None] can be used, but the expected behavior is that quoted annotations using | should be accepted without error.
I would be happy to work on this issue if it is considered valid. Please let me know if I can proceed with a pull request.
The best way to move something like this forward is to submit a PR. Unfortunately maintainer time on mypy is limited, so it may take a while before a maintainer is able to review and merge a PR.
I would consider this issue valid, but given that Python 3.9 is about to go out of support in about two days, realistically the outcome may well be that mypy drops support for 3.9 before this gets fixed.
Thank you for your insight. I'd understand if that were to happen though it would still be a shame :( macOS (15) is still shipping with 3.9 as default. I know Apple is here to blame, but still it would be nice to use software out of the box.
Activity
JawadAliAI commentedon Sep 5, 2025
Mypy currently raises X | Y syntax for unions requires Python 3.10 when string-quoted type hints include the | operator under Python < 3.10 (e.g., "str | None"). Since these annotations are provided as strings (permitted forward references under PEP 563), they should not be subject to version-specific syntax restrictions. For compatibility with Python 3.9, mypy should treat such cases as plain forward references rather than parsing them with Python 3.10+ rules.
As a workaround, Optional[str] or Union[str, None] can be used, but the expected behavior is that quoted annotations using | should be accepted without error.
I would be happy to work on this issue if it is considered valid. Please let me know if I can proceed with a pull request.
relikd commentedon Oct 28, 2025
Any progress on the issue? I feel sad that people are willing to work on this but no one is giving the go signal.
JelleZijlstra commentedon Oct 29, 2025
The best way to move something like this forward is to submit a PR. Unfortunately maintainer time on mypy is limited, so it may take a while before a maintainer is able to review and merge a PR.
I would consider this issue valid, but given that Python 3.9 is about to go out of support in about two days, realistically the outcome may well be that mypy drops support for 3.9 before this gets fixed.
relikd commentedon Oct 29, 2025
Thank you for your insight. I'd understand if that were to happen though it would still be a shame :( macOS (15) is still shipping with 3.9 as default. I know Apple is here to blame, but still it would be nice to use software out of the box.
Avasam commentedon May 18, 2026
Looks like mypy 2.0 has dropped support for even parsing Python 3.9:
--python-version 3.9#21243