False negative for calling unimplemented abstract methods
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
Bug Report
Hello. I just try to use the Protocol feature of Python, and I remark a false negative with mypy when you call unimplemented static methods. The problem is also present if you replace the Protocol inheritance by an ABC one.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=28822a92ae617fb3a81e17cdf01eb894
class A(Protocol):
@staticmethod
def static_meth() -> int: ...
def meth(self) -> int: ...
class B(A):
def func(self) -> int:
return B.static_meth() + self.meth() # <== Calling B.static_meth() should be reported as an error, because it returns None during execution, which is an invalid type here !! Pylance does, not about the NoneType of this call, but because it says the static method is abstract and unimplemented, which is a better reason in my opinion.
class C(B):
@staticmethod
def static_meth() -> int:
return 1
def meth(self) -> int:
return 2
b = B() # <== this is the only error reported by mypy on this entire script, which is correct, Pylance reports it too : Cannot instantiate abstract class "B" with abstract attributes "meth" and "static_meth"mypy[abstract](https://mypy.readthedocs.io/en/stable/_refs.html#code-abstract). Notice how the error message says the static_meth is abstract here, but not when it is called on B or b.
c = C()
print(c.func()) # <== Causes an error during execution that should have been avoided if mypy had reported the invalid call to B.static_meth() in the implementation of func() : TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
print(B.static_meth()) # <== No error detected here, calling B.static_meth() should be reported as an error because it is not implemented! Only Pylance does.
print(b.static_meth()) # <== No error detected here, calling b.static_meth() should be reported as an error because it is not implemented! Pylance does not either.
Expected Behavior
mypy should report all the calls to B.static_meth() and b.static_meth() because static_meth() is not implemented in the B class or in one of its ancestors.
Actual Behavior
mypy doesn't report the calls to unimplemented static methods on classes and instances declared in a Protocol or an ABC sub class, although it says exactly this when you try to instantiate a sub class having no implementation of these methods. Pylance does, but not in all the expected cases. :/
During execution, Python returns None when you call unimplemented static methods declared in an ancestor class, instead of throwing an error, which is a strange choice...
Your Environment
- Mypy version used: 1.11.2 (still present on the latest version 1.13, as you can see with the link to mypy playground above)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.12.6
Regards.
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
Inizia dalla riproduzione collegata nel mypy playground e dai controlli esistenti per i metodi astratti e l’istanza di classi. Traccia come vengono analizzate le chiamate a metodi statici non implementati su B e b. Il lavoro è completato quando mypy segnala gli errori previsti per quelle chiamate senza perdere la diagnostica esistente per l’istanziazione di B.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100