Make LimitedSizeDict a generic (#78440)
* Make LimitedSizeDict a generic * Remove comments * Use super() * Apply suggestion Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>pull/77853/head^2
parent
ad25a966a8
commit
374b72f052
|
@ -1,22 +1,28 @@
|
|||
"""Helpers for script and automation tracing and debugging."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import Any, TypeVar
|
||||
|
||||
_KT = TypeVar("_KT")
|
||||
_VT = TypeVar("_VT")
|
||||
|
||||
|
||||
class LimitedSizeDict(OrderedDict):
|
||||
class LimitedSizeDict(OrderedDict[_KT, _VT]):
|
||||
"""OrderedDict limited in size."""
|
||||
|
||||
def __init__(self, *args, **kwds):
|
||||
def __init__(self, *args: Any, **kwds: Any) -> None:
|
||||
"""Initialize OrderedDict limited in size."""
|
||||
self.size_limit = kwds.pop("size_limit", None)
|
||||
OrderedDict.__init__(self, *args, **kwds)
|
||||
super().__init__(*args, **kwds)
|
||||
self._check_size_limit()
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
def __setitem__(self, key: _KT, value: _VT) -> None:
|
||||
"""Set item and check dict size."""
|
||||
OrderedDict.__setitem__(self, key, value)
|
||||
super().__setitem__(key, value)
|
||||
self._check_size_limit()
|
||||
|
||||
def _check_size_limit(self):
|
||||
def _check_size_limit(self) -> None:
|
||||
"""Check dict size and evict items in FIFO order if needed."""
|
||||
if self.size_limit is not None:
|
||||
while len(self) > self.size_limit:
|
||||
|
|
Loading…
Reference in New Issue