Skip to content

comparison-overlap triggers for string enum members when the member name isn't the same as the comparison value #19576

Description

@bzoracler

Bug Report, To Reproduce, and Actual Behaviour

If the enum member's name to compare isn't equal to the comparing value, a false positive comparison-overlap is triggered. See mypy Playground:

from enum import Enum

class A(str, Enum):  # Same behaviour if `class A(enum.StrEnum)`
    a = "b"

A.a == "a"  # OK
A.a == "b"  # E: Non-overlapping equality check (left operand type: "Literal[A.a]", right operand type: "Literal['b']")  [comparison-overlap]

Expected Behavior

No errors

Your Environment

  • Mypy version used: 1.17, master
  • Python version used: 3.12

Activity

  1. added a commit that references this issue on Sep 16, 2025
  2. added a commit that references this issue on Oct 11, 2025
  3. florianvazelle commented on Oct 14, 2025

    @florianvazelle

    Hi @bzoracler, for me, this is a true positive, because you are comparing an enum value and a string. IMO, the right thing to do is to compare A.a.value == "b", no ? 🤔

  4. bzoracler commented on Oct 14, 2025

    @bzoracler
    ContributorAuthor

    @florianvazelle No, as mentioned in the issue title, this is about string enums. String enums (including enum.StrEnum) don't need to access .value to perform str operations or be treated as a str, as explicitly stated in the Python docs:

    StrEnum is the same as Enum, but its members are also strings and can be used in most of the same places that a string can be used.

    You can try it yourself:

    from enum import Enum
    
    class MyStrEnum(str, Enum):
        member = "asdfjkl"
    >>> print(MyStrEnum.member == "asdfjkl")
    True
  5. florianvazelle commented on Oct 14, 2025

    @florianvazelle

    Ok, thank you for clarifying that 🙏

  6. added a commit that references this issue on Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions