'Subclass of "A" and "B" cannot exist: would have incompatible method signatures' is usually wrong
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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.)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci gli esempi di oggetti chiamabili e attributi nei playground mypy-play collegati, quindi traccia il restringimento dei tipi e i controlli delle firme incompatibili che producono la diagnostica. Determina se le sottoclassi comuni valide debbano sopprimere l'errore e l'avviso di codice irraggiungibile, e aggiungi una copertura di regressione che dimostri il comportamento previsto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100