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
lru_cache annotation doesn't work as a property #5858
@probablyodd well its a bit different. If you set # type: ignore to @property in this case,
mypy will completely ignore existence of this decorator and treat such methods as common callable methods. Mypy will just say somethingh like "lru_cache" has no attribute "..." if complex object such as dict returned from property method.
from functools import lru_cache
class Foo:
@property # type: ignore
@lru_cache
def bar(self) -> dict:
return dict(key='value')
Foo().bar.get('key')
test.py:11: error: "lru_cache" has no attribute "get"
Reacted by Julia Cox, Darri Steinn Konráðsson, Gilad Peleg, Sorin Sbarnea, byYuraGudan, Michał Krassowski, Peter Roelants, Robin, Guillaume Algis and Finite State Machine
Taking a look at the actual implementation of lru_cache and mypy's excellent documentation on decorator factories, here is another workaround for having type annotations work properly with functool's lru_cache:
This maintains the signature of whatever callable you are decorating, and you should get proper type annotation checks. Credits goes to @sashgorokhov for inspiring to get this solution with his solution above.
If there is no plan to fix this soon, then can this be documented properly? This error is implicated by the recommendation in the standard library docs for functools.cached_property for caching properties in classes without proper __dict__s, but there's no hint that such recommendation would frustrate Mypy:
# test_5858.pyimportstatisticsfromfunctoolsimportcacheclassDataSet:
__slots__= ("_data",)
def__init__(self, sequence_of_numbers):
self._data=sequence_of_numbers@property# <- error: Decorated property not supported [misc]@cache# <- results in downstream errors involving type "_lru_cache_wrapper[…]"defstdev(self):
returnstatistics.stdev(self._data)
f: float=statistics.stdev(range(10))
f=DataSet(range(10)).stdev
% python --version
Python 3.9.10
% mypy --version
mypy 0.941
% mypy --config-file=/dev/null test_5858.py
/dev/null: No [mypy] section in config file
test_5858.py:11: error: Decorated property not supported
test_5858.py:17: error: Incompatible types in assignment (expression has type"_lru_cache_wrapper[Any]", variable has type"float")
Found 2 errors in 1 file (checked 1 source file)
Activity
[-]lru_cache annotation doesn't work as property[/-][+]lru_cache annotation doesn't work as a property[/+]probablyodd commentedon Dec 1, 2018
Duplicates #1362 and #1608
sashgorokhov commentedon Jan 14, 2019
@probablyodd well its a bit different. If you set
# type: ignoreto@propertyin this case,mypy will completely ignore existence of this decorator and treat such methods as common callable methods. Mypy will just say somethingh like
"lru_cache" has no attribute "..."if complex object such as dict returned from property method.Edit:
Possible adhoc solution:
pgilad commentedon Jun 21, 2020
Besides @sashgorokhov awesome workaround (that adds some complexity to code) - is there any plan to support this ?
malthunayan commentedon Apr 29, 2021
Taking a look at the actual implementation of
lru_cacheandmypy's excellent documentation on decorator factories, here is another workaround for having type annotations work properly withfunctool'slru_cache:This maintains the signature of whatever callable you are decorating, and you should get proper type annotation checks. Credits goes to @sashgorokhov for inspiring to get this solution with his solution above.
Doggie52 commentedon Oct 2, 2021
Is this something that should be adjusted in
mypyor is it technically the fault oflru_cache?Also @malthunayan I can't seem to get your workaround to work with
@lru_cache(maxsize=None).Doggie52 commentedon Oct 2, 2021
Here's my modified workaround - not sure where your
max_sizeis coming from, could have worked in previous Python versions? I'm on 3.8.posita commentedon Mar 19, 2022
If there is no plan to fix this soon, then can this be documented properly? This error is implicated by the recommendation in the standard library docs for
functools.cached_propertyfor caching properties in classes without proper__dict__s, but there's no hint that such recommendation would frustrate Mypy:Fix inference for properties with __call__ (#15926)