From 374b72f05280281ca16d4810c20b6815a58d5971 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 14 Sep 2022 13:30:17 +0200 Subject: [PATCH] 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> --- homeassistant/components/trace/utils.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/trace/utils.py b/homeassistant/components/trace/utils.py index 50d1590e4fd..a12618af0ec 100644 --- a/homeassistant/components/trace/utils.py +++ b/homeassistant/components/trace/utils.py @@ -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: