Skip to content

False-positive typecheck with StrEnum #17162

@roganov

Description

@roganov

Bug Report

When comparing a member of StrEnum to a string, it seems that Mypy assumes that the check is always False and does not type-check code under the condition.

To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=f7c8364d301ada4067a9a6f515b9e161

import typing
import enum

class MyEnum(str, enum.Enum):
    A = 'a'

class MyEnum2(enum.StrEnum):
    A = 'a'

if MyEnum.A == 'a':
    1 + ''

if MyEnum2.A == 'a':
    1 + ''

Expected Behavior

Mypy should flag both 1 + '' expressions as type errors because these conditions evaluate to True in runtime.

Actual Behavior

Mypy does not report errors

Your Environment

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