False negative for calling unimplemented abstract methods
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy playground reproduction and the existing checks for abstract methods and class instantiation. Trace how calls to unimplemented static methods on B and b are analyzed. Done means mypy reports the expected errors for those calls without losing the existing diagnostic for instantiating B.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100