'Subclass of "A" and "B" cannot exist: would have incompatible method signatures' is usually wrong
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Mypy reports Subclass of "A" and "B" cannot exist: would have incompatible method signatures in many cases if given x: A, you run isinstance(x, B). However, this is often (always?) incorrect, as mypy does not account for subclasses that use method signatures that are subtypes of both classes.
Example (playground):
from typing import Never
class A:
def f(self) -> int: return 0
class B:
def f(self) -> str: return ""
class X(A, B):
def f(self) -> Never: raise Exception
x: A = X()
if isinstance(x, B): # E: Subclass of "A" and "B" cannot exist: would have incompatible method signatures
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "__main__.A"
Here, mypy accepts the definition of a class X that is a subclass of A and B, yet also says that such a subclass cannot exist.
Any callable type is a subclass of (*args: object, **kwargs: object) -> Never, so incompatible method signatures cannot be a reason that a common subclass cannot exist.
It gets a little trickier with attributes. However, mypy accepts this class and then again claims it cannot exist (playground):
from typing import Never
class A:
x: int
class B:
x: str
class X(A, B):
@property
def x(self) -> Never: raise Exception
@x.setter
def x(self, x: int | str) -> None: pass
x: A = X()
if isinstance(x, B): # E: Subclass of "A" and "B" cannot exist: would have incompatible method signatures
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "__main__.A"
(Note though that pyright rejects this form of inheritance; possibly mypy should reject this too in the future if we revise how we treat inheritance of attributes.)
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
Reproduziere die aufrufbaren und Attributbeispiele in den verlinkten mypy-play-Playgrounds und verfolge dann die Typverengung und die Prüfungen auf inkompatible Signaturen, die die Diagnose erzeugen. Ermittle, ob gültige gemeinsame Unterklassen den Fehler und die Warnung wegen Unerreichbarkeit unterdrücken sollten, und füge eine Regressionstestabdeckung hinzu, die das beabsichtigte Verhalten demonstriert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100