python / python/mypy

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

Aperta
#16,722 7 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l’esempio fornito di tupla con due enum nel mypy playground collegato o in un checkout locale di mypy, quindi esamina il percorso di controllo dell’esaustività per il pattern matching strutturale e assert_never. Il lavoro è completato quando l’esempio supera il type checking senza l’errore incompatibile tuple[A, B], preservando il comportamento esistente per i match non esaustivi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.