--warn-unreachable and isinstance leads to false positive about method resolution order
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer im Issue und führe mypy mit --warn-unreachable aus, um die Diagnose zu bestätigen. Verfolge die isinstance-Eingrenzung und die Analyse der Method-Resolution-Order, die für das nicht erreichbare Ergebnis verantwortlich sind; abgeschlossen ist die Arbeit, wenn diese gültige Hierarchie keinen False Positive mehr erzeugt, während echte nicht erreichbare Fälle weiterhin gemeldet werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100