False negative for calling unimplemented abstract methods
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
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 der verlinkten mypy-Playground-Reproduktion und den bestehenden Prüfungen für abstrakte Methoden und Klasseninstanziierung. Verfolge, wie Aufrufe nicht implementierter statischer Methoden auf B und b analysiert werden. Fertig ist die Aufgabe, wenn mypy die erwarteten Fehler für diese Aufrufe meldet, ohne die bestehende Diagnose für die Instanziierung von B zu verlieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100