Skip to content

Type narrowing an enum shouldn't narrow to a literal #19753

@A5rocks

Description

@A5rocks

Bug Report

If you compare an enum to a literal, then the type becomes that literal. Somewhat a followup to #19594.

To Reproduce

from enum import Enum

class A(str, Enum):
    a = "b"
    b = "a"

def test(a: A) -> None:
    if a == "b":
        reveal_type(a)

Expected Behavior

main.py:9: note: Revealed type is "Literal[A.a]"
Success: no issues found in 1 source file

Actual Behavior

main.py:9: note: Revealed type is "Literal['b']"
Success: no issues found in 1 source file

Your Environment

Reproduced in mypy playground.

  • Mypy version used: 1.17.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 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