Skip to content

Integer incorrectly narrowed to IntEnum #17860

@JukkaL

Description

@JukkaL

The revealed narrowed type is unexpected, and there is a false negative:

from enum import IntEnum

class E(IntEnum):
    X = 1

def f(x: int) -> None:
    if x == E.X:
        print(x.value)  # No mypy error, but fails at runtime
        reveal_type(x)  # "Literal[E.X]"

f(1)

I'd expect the revealed type to be int, since that's the correct runtime type. IntEnum values are equal to the corresponding int objects:

print(1 == E.X)  # True

Also, x.value should be flagged as an error, since it fails at runtime.

If this needs special casing for IntEnums, it's fine. We already special case enums in various ways.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions