Skip to content

mypy accepts invalid PEP 604 syntax with forward reference #14397

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

Open
Molkree opened this issue Jan 5, 2023 · 1 comment
Open

mypy accepts invalid PEP 604 syntax with forward reference #14397

Molkree opened this issue Jan 5, 2023 · 1 comment
Labels
feature topic-pep-604 PEP 604 (union | operator) topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@Molkree
Copy link

Molkree commented Jan 5, 2023

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"]

Playground

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 microsoft/pyright@6eb8467 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 python/cpython#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).

@Molkree Molkree added the bug mypy got something wrong label Jan 5, 2023
@AlexWaygood AlexWaygood added feature topic-pep-604 PEP 604 (union | operator) and removed bug mypy got something wrong labels Jan 5, 2023
@JelleZijlstra JelleZijlstra added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Jan 5, 2023
@Molkree
Copy link
Author

Molkree commented Jan 5, 2023

#11582 might be very close to this and it even was investigated and worked on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-pep-604 PEP 604 (union | operator) topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

3 participants