False negative: `typing.Literal[<Union expression>]`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example in the linked mypy playground with Python 3.10 or 3.12 and the shown Literal[1 | None] annotation. Trace how mypy handles Literal arguments and compare the result with the proposed invalid-type diagnostic and Any reveal; done means arbitrary union expressions are rejected consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100