You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mypy incorrectly says signature is incompatible with supertype when using decorator #3260
I played with this a bit and it looks like this is specific to functools.lru_cache(), most probably since it is a class that defines a __call__ method with decorator-like signature. So this is different from #1441.
I also ran with this issue today, precisely with the @lry_cache decorator on a subclass's method overriding a base method. Are there any solutions or workarounds at this point?
@stlehmann Mypy is right in your case, it fails Liskov. The subclass method requires more arguments than the superclass method, so it is not a proper subtype.
@stlehmann You can silence the mypy error by adding a # type: ignore comment on line 14. It won't cause mypy to ignore the signature, but it will stop complaints about the Liskov violation.
@gvanrossum Thanks for the explanation. I googled Liskov and it makes perfectly sense. However in this case I can't change the API so I will use #type: ignore to silence the error message as proposed by @JukkaL
I'm not sure the lru_cache() issue is resolved as of 0.670? The example provided applied lru_cache() to both methods, but this error is still raised if lru_cache() is only used on one method. Is that expected? If not, I'm happy to open a new bug for it if that's the proper way to handle it.
I found this when I had defined an abstractmethod (which therefore didn't need the lru_cache decorator).
class A:
def a(self) -> str:
return 'a'
class B(A):
@functools.lru_cache()
def a(self) -> str:
return super().a() + 'b'
results in:
mypy-test.py:9: error: Signature of "a" incompatible with supertype "A"
I am still encountering this issue with the following use case on MyPy version 0.991:
fromfunctoolsimportcacheclassBase:
@classmethod@cachedefvalue(cls) ->str:
# Some expensive default implementationreturn"[computed default]"classSimple(Base):
@classmethoddefvalue(cls) ->str:
return"Simple value that doesn't need caching"
error: Signature of "value" incompatible with supertype "Base" [override]
Activity
ilevkivskyi commentedon Apr 26, 2017
I played with this a bit and it looks like this is specific to
functools.lru_cache(), most probably since it is a class that defines a__call__method with decorator-like signature. So this is different from #1441.JelleZijlstra commentedon Jun 15, 2017
I just ran into this too with a method decorated with
@contextlib.asynccontextmanager(https://docs.python.org/3.7/library/contextlib.html#contextlib.asynccontextmanager).jdferreira commentedon Jul 7, 2018
I also ran with this issue today, precisely with the @lry_cache decorator on a subclass's method overriding a base method. Are there any solutions or workarounds at this point?
stlehmann commentedon Jul 23, 2018
I' ve als got this issue with a stub-file for PyQt5:
Results in the following mypy error:
gvanrossum commentedon Jul 23, 2018
@stlehmann Mypy is right in your case, it fails Liskov. The subclass method requires more arguments than the superclass method, so it is not a proper subtype.
JukkaL commentedon Jul 23, 2018
@stlehmann You can silence the mypy error by adding a
# type: ignorecomment on line 14. It won't cause mypy to ignore the signature, but it will stop complaints about the Liskov violation.stlehmann commentedon Jul 24, 2018
@gvanrossum Thanks for the explanation. I googled Liskov and it makes perfectly sense. However in this case I can't change the API so I will use
#type: ignoreto silence the error message as proposed by @JukkaLgvanrossum commentedon Jul 24, 2018
ilevkivskyi commentedon Sep 26, 2018
I tried both examples (
lru_cache()andasynccontextmanager) they both work correctly on master.ikelos commentedon Feb 28, 2019
I'm not sure the
lru_cache()issue is resolved as of 0.670? The example provided appliedlru_cache()to both methods, but this error is still raised iflru_cache()is only used on one method. Is that expected? If not, I'm happy to open a new bug for it if that's the proper way to handle it.I found this when I had defined an
abstractmethod(which therefore didn't need thelru_cachedecorator).results in:
ilevkivskyi commentedon Feb 28, 2019
Yes, this is a bug, but we already have an issue for this, see #5836
ikelos commentedon Feb 28, 2019
Ok, cool. I'll follow along there, thanks! 5:)
igor-petrik-invitae commentedon Jan 20, 2023
I am still encountering this issue with the following use case on MyPy version 0.991:
error: Signature of "value" incompatible with supertype "Base" [override]