python / python/mypy

MyIntEnum.ONE should be passable to functions expecting Literal[1] arguments

Open
#19,616 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

This is related but distinct from #19576, #16327, and #17162. It's also not specific to IntEnums, but I use them in the example for clarity. StrEnums as well as enums based on (int, Enum), or (str, Enum), etc.

To Reproduce

import enum
from typing import Literal

type One = Literal[1]
type Two = Literal[2]
type Three = Literal[3]

def option(arg: One | Two | Three) -> None:
    ...

class MyEnum(enum.IntEnum):
    ONE = 1
    TWO = 2
    THREE = 3

option(MyEnum.TWO)  #  error: Argument 1 to "option" has incompatible type "Literal[MyEnum.TWO]"; expected "Literal[1, 2, 3]"  [arg-type]

Expected Behavior

No error reported.

Actual Behavior

The error in the comment in the example is a false positive. This code works fine at runtime.

Your Environment

  • Mypy version used: master as of 0d791b29b7ba4e5a9b04c0b6bdba11faf4a186a2
  • Python version used: 3.13

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 by running the supplied Python 3.13 example against mypy master and trace the handling of Literal arguments involving MyEnum.TWO. The fix is complete when the example reports no incompatible-argument error while preserving the related enum and Literal behavior described in the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.