Type Hint the Python 3.8 LRU Cache Example #1253
-
Can someone please provide me guidance as to the best (recommended) way to type hint the class LRU(OrderedDict):
'Limit size, evicting the least recently looked-up key when full'
def __init__(self, maxsize=128, /, *args, **kwds):
self.maxsize = maxsize
super().__init__(*args, **kwds)
def __getitem__(self, key):
value = super().__getitem__(key)
self.move_to_end(key)
return value
def __setitem__(self, key, value):
if key in self:
self.move_to_end(key)
super().__setitem__(key, value)
if len(self) > self.maxsize:
oldest = next(iter(self))
del self[oldest] I'd like to start with the I know I can type hint
From the cpython source, I know The Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I don't think PEP 646 is useful here. The |
Beta Was this translation helpful? Give feedback.
I don't think PEP 646 is useful here. The
*args
/**kwargs
are forwarded to the OrderedDict constructor, which appears to be the same as the dict constructor (according to typeshed). So a precise type signature would simply duplicate thedict.__init__
signature: https://github.com/python/typeshed/blob/8a47a28e768c577385d6232d4afea496b60403e7/stdlib/builtins.pyi#L1022.