python / python/mypy

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

オープン
#19,616 コメント 2 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug needs discussion topic-enum topic-literal-types
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された Python 3.13 の例を mypy master に対して実行し、MyEnum.TWO に関係する Literal 引数の処理を追跡します。例が非互換な引数のエラーを報告しなくなり、issue で説明されている関連する enum と Literal の動作を維持できれば、修正は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。