python / python/mypy

IntEnum literals are not considerable comparable with ints

Open
#16,327 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-enum
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The handling of types for IntEnum literals shows no overlap with int literals.
The handling for int variables appears to be correct,

To Reproduce

playground

from enum import IntEnum


class CardVal(IntEnum):
    VAL_A = 1
    VAL_2 = 2

try:
    assert CardVal.VAL_A == 1
except AssertionError:
    pass # So we see the second error in mypy

assert CardVal.VAL_2 == 2

Expected Behavior

The type of CardVal.VAL_A gets erased to something considered comparable to Literal[1]

Actual Behavior

From the playground link, using python 3.10 / mypy 1.6.1:

main.py:9: error: Non-overlapping equality check (left operand type: "Literal[CardVal.VAL_A]", right operand type: "Literal[1]")  [comparison-overlap]
main.py:13: error: Non-overlapping equality check (left operand type: "Literal[CardVal.VAL_2]", right operand type: "Literal[2]")  [comparison-overlap]
Found 2 errors in 1 file (checked 1 source file)

The same behaviour has been seen locally with python 3.11 / mypy 1.6.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked mypy playground reproduction using Python 3.10 and strict mode, then trace how IntEnum literals and equality comparisons are handled. The work is done when comparisons between the shown IntEnum members and matching int literals no longer produce comparison-overlap errors, while the existing int-variable behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.