Skip to content

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

Open
@bzoracler

Description

@bzoracler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions