python / python/mypy

Exhaustiveness checks (assert_never) don't work with 2-tuple of enums

オープン
#16,722 コメント 7 件 リアクション 6 件 担当者 0 名 GitHub で見る

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

The code below fails to type check, but should succeed because it's exhaustive.

There are some possibly related issues, but I don't know if they are all the same bug, the other tickets tend to depend on Union which this does not: #16650 #15426 #12364

To Reproduce

Playground link: https://mypy-play.net/?mypy=latest&python=3.12&gist=0d21d288fce8aa13d3cc60f4e418960a

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from enum import Enum
from typing import assert_never

class A(Enum):
    A0 = 0
    A1 = 1

class B(Enum):
    B0 = 0
    B1 = 1

def test(a: A, b: B) -> bool:
    match (a, b):
        case (A.A0, B.B0):
            return True
        case (A.A0, B.B1):
            return True
        case (A.A1, B.B0):
            return True
        case (A.A1, B.B1):
            return True
        case _ as never:
            assert_never(never)

    return False

Actual Behavior

typecheck_test.py:26: error: Argument 1 to "assert_never" has incompatible type "tuple[A, B]"; expected "NoReturn"  [arg-type]

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.11 (but also happens with 3.12 in playground link)

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた mypy playground またはローカルの mypy checkout で、提示された 2 つの enum のタプル例を再現します。次に、構造的パターンマッチングと assert_never の網羅性チェックの経路を調べます。例で互換性のない tuple[A, B] エラーが発生せずに型チェックを通過し、非網羅的なマッチに対する既存の動作が維持されれば完了です。

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

評価

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

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

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