From 7aa1359c4a7f5d3e494ed7aa56be57c2c3afde9f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 Feb 2023 03:57:36 -0600 Subject: [PATCH] Cache try_parse_enum (#87911) --- homeassistant/util/enum.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/homeassistant/util/enum.py b/homeassistant/util/enum.py index 7d1e3970586..f0de2206f1f 100644 --- a/homeassistant/util/enum.py +++ b/homeassistant/util/enum.py @@ -1,11 +1,23 @@ """Helpers for working with enums.""" +from collections.abc import Callable import contextlib from enum import Enum -from typing import Any, TypeVar +from typing import TYPE_CHECKING, Any, TypeVar + +# https://github.com/python/mypy/issues/5107 +if TYPE_CHECKING: + _LruCacheT = TypeVar("_LruCacheT", bound=Callable) + + def lru_cache(func: _LruCacheT) -> _LruCacheT: + """Stub for lru_cache.""" + +else: + from functools import lru_cache _EnumT = TypeVar("_EnumT", bound=Enum) +@lru_cache def try_parse_enum(cls: type[_EnumT], value: Any) -> _EnumT | None: """Try to parse the value into an Enum.