python / python/mypy

--warn-unreachable and isinstance leads to false positive about method resolution order

Aperta
#15,038 2 commenti 1 reazione 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

A small V shaped class hierarchy, a variable annotated as an instance of the supertype, and a few assert isinstance to progressively narrow the type gets a false positive error message from mypy that no such subclass could exist. The code works fine at runtime, though.

To Reproduce

https://gist.github.com/mypy-play/a653520575a1e3279bfcec2f64c4a128

Requires --warn-unreachable:
https://mypy-play.net/?mypy=latest&python=3.11&flags=warn-unreachable&gist=a653520575a1e3279bfcec2f64c4a128

class A: pass
class B(A): pass
class C:pass
class D(B, C):pass

m: A = D()
assert isinstance(m, C)  # remove this line, or move it to after the following one, and the error goes away.
assert isinstance(m, D)  # error: Subclass of "A", "C", and "D" cannot exist: would have inconsistent method resolution order  [unreachable]

Actual Behavior

$ mypy --warn-unreachable b.py
b.py:8: error: Subclass of "A", "C", and "D" cannot exist: would have inconsistent method resolution order  [unreachable]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0 (compiled: yes)
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.0rc1

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 con il reproducer nell’issue ed esegui mypy con --warn-unreachable per confermare la diagnostica. Traccia il narrowing di isinstance e l’analisi del method-resolution-order responsabili del risultato irraggiungibile; il lavoro è completo quando questa gerarchia valida non produce più un falso positivo, mentre i casi realmente irraggiungibili continuano a essere segnalati.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.