Cache try_parse_enum (#87911)

This commit is contained in:
J. Nick Koston 2023-02-12 03:57:36 -06:00 committed by GitHub
parent 6680e819e5
commit 7aa1359c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,23 @@
"""Helpers for working with enums.""" """Helpers for working with enums."""
from collections.abc import Callable
import contextlib import contextlib
from enum import Enum 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) _EnumT = TypeVar("_EnumT", bound=Enum)
@lru_cache
def try_parse_enum(cls: type[_EnumT], value: Any) -> _EnumT | None: def try_parse_enum(cls: type[_EnumT], value: Any) -> _EnumT | None:
"""Try to parse the value into an Enum. """Try to parse the value into an Enum.