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 negative: typing.Literal[<Union expression>] #17260

Open
bzoracler opened this issue May 17, 2024 · 0 comments
Open

False negative: typing.Literal[<Union expression>] #17260

bzoracler opened this issue May 17, 2024 · 0 comments
Labels
bug mypy got something wrong semantic-analyzer Problems that happen during semantic analysis topic-literal-types topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@bzoracler
Copy link
Contributor

Bug Report, To Reproduce, & Actual Behaviour

See mypy playground

from typing import Literal

a: Literal[1 | None]
reveal_type(a)
note: Revealed type is "Union[Literal[1], None]"
Success: no issues found in 1 source file

For the runtime counterpart, this will result in a TypeError: unsupported operand type(s) for |: 'int' and 'NoneType' if the module doesn't have from __future__ import annotations or invalid results for runtime annotation inspectors (e.g. in the case of typing.Literal[1 | 2]).

Expected Behavior

Maybe:

error: Invalid type: Literal[...] cannot contain arbitrary expressions  [valid-type]
note: Revealed type is "Any"

Your Environment

  • Mypy version used: 1.10
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10 & 3.12
@bzoracler bzoracler added the bug mypy got something wrong label May 17, 2024
@JelleZijlstra JelleZijlstra added semantic-analyzer Problems that happen during semantic analysis topic-literal-types labels May 18, 2024
@hauntsaninja hauntsaninja added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong semantic-analyzer Problems that happen during semantic analysis topic-literal-types topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

3 participants