MCPcopy
hub / github.com/pydantic/pydantic / LimitedDict

Class LimitedDict

pydantic/_internal/_generics.py:42–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41
42class LimitedDict(dict[KT, VT]):
43 def __init__(self, size_limit: int = _LIMITED_DICT_SIZE) -> None:
44 self.size_limit = size_limit
45 super().__init__()
46
47 def __setitem__(self, key: KT, value: VT, /) -> None:
48 super().__setitem__(key, value)
49 if len(self) > self.size_limit:
50 excess = len(self) - self.size_limit + self.size_limit // 10
51 to_remove = list(self.keys())[:excess]
52 for k in to_remove:
53 del self[k]
54
55
56# weak dictionaries allow the dynamically created parametrized versions of generic models to get collected

Callers 1

test_limited_dictFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_limited_dictFunction · 0.72