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

False positive for PEP 604 None | t when used as value #12368

Closed
NiklasRosenstein opened this issue Mar 17, 2022 · 3 comments · Fixed by #14008
Closed

False positive for PEP 604 None | t when used as value #12368

NiklasRosenstein opened this issue Mar 17, 2022 · 3 comments · Fixed by #14008
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)

Comments

@NiklasRosenstein
Copy link

NiklasRosenstein commented Mar 17, 2022

Bug Report

Using None | t instead of t | None prompts an error when used as a value (not a type hint).

https://peps.python.org/pep-0604/#proposal

To Reproduce

a: None | int
b = None | int  # error: Unsupported left operand type for | ("None")  [operator]

Expected Behavior

Mypy does not complain about the second line.

Your Environment

  • Mypy version used: 90.940
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
    pretty = true
    namespace_packages = true
    warn_redundant_casts = true
    warn_no_return = true
    warn_unreachable = true
    show_error_context = true
    show_error_codes = true
    check_untyped_defs = true
  • Python version used: 3.10.2
  • Operating system and version: Ubuntu 20.04 WSL
@NiklasRosenstein NiklasRosenstein added the bug mypy got something wrong label Mar 17, 2022
@jhance
Copy link
Collaborator

jhance commented Mar 17, 2022

I assume this means that isinstance(5, None | int) fails, which seems like a major bug.

@NiklasRosenstein
Copy link
Author

@jhance Your assumption is correct.

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 17, 2022

This looks similar to other raised issues 🤔

#12005
#11673, #12363
#11582 (kind of the opposite of this issue)
#11426

a: None | int
b = None | int # error: Unsupported left operand type for | ("None")
isinstance(5, None | int) # error: Unsupported left operand type for | ("None")

playground

#12369 is probably related as well.

@JelleZijlstra JelleZijlstra added the topic-pep-604 PEP 604 (union | operator) label Mar 19, 2022
JukkaL added a commit that referenced this issue Nov 4, 2022
Fix Python 3.10 `|` union syntax in type aliases, when one of
the operands is a type alias or a type with an overloaded `__init__`.

Fixes #12368. Fixes #12005. Fixes #11426.
JukkaL added a commit that referenced this issue Nov 7, 2022
Fix Python 3.10 `|` union syntax in type aliases, when one of
the operands is a type alias or a type with an overloaded `__init__`.

We can now infer `typing._SpecialForm` for type aliases in a runtime 
context.

Also create a bunch of minimal test-only stubs for stdlib modules 
to fix some test failures caused by the missing `typing._SpecialForm`
in the default test stubs. This is generally what we want in any case, 
since using typeshed stubs with minimal builtins/typing stubs can
result in unpredictable behavior and slow tests.

Fixes #12368. Fixes #12005. Fixes #11426.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants