Skip to content

[1.14 regression] truthy check for enums #18376

Closed
@delfick

Description

@delfick

Bug Report

It appears the truthy check for enums is incorrect in 1.14 when warn_unreachable is set to True.

For example

import enum


class MyEnum(enum.StrEnum):
    EMPTY = ""
    NOT_EMPTY = "asdf"


def is_truthy(e: MyEnum) -> None:
    if e:
        print(f"{e} was truthy: {len(e)}")
    else:
        print(f"{e} was falsey: {len(e)}")


is_truthy(MyEnum.EMPTY)
is_truthy(MyEnum.NOT_EMPTY)

With a mypy.ini

[mypy]
warn_unreachable = True

If we run the script we get:

 was falsey: 0
asdf was truthy: 4

Which shows the behaviour of these enums is such that they can be used in an if condition

but running mypy a.py results in:

a.py:13: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.14
  • Python version used: tested with and saw same problem on python3.12 and python3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions