mypy accepts invalid PEP 604 syntax with forward reference
Bug Report
PEP 604 introduced the | syntax, however it's not entirely equivalent to the Union syntax. Forward references with | are not supported and result in TypeErrors TypeError: unsupported operand type(s) for |: 'type' and 'str'. Mypy doesn't warn the user about this and accepts | with forward references.
To Reproduce
T = list[int | "T"]
Expected Behavior
Some kind of error telling me that this syntax is not supported.
Actual Behavior
No errors reported.
Your Environment
- Mypy version used: 0.991
- Mypy command-line flags: --strict or no flags, doesn't matter
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11
I originally raised this as a discussion in pyright repo. And since then pyright has implemented https://github.com/microsoft/pyright/commit/6eb846709f4fec0180593f3e89f1dd5b20f41956 to detect this incorrect usage of | (available in release 1.1.285). I have to admit that documentation on this is a bit murky as I was expecting | to be equivalent to Union and I wasn't the only one, see https://github.com/python/cpython/issues/90015. From what I can gather in this CPython issue runtime won't be changed to make this syntax valid and docs should be changed instead (but neither happened yet).
#11582 might be very close to this and it even was investigated and worked on.