Skip to content

Commit e16ac57

Browse files
fix(perf): optimize some hot paths
1 parent 78745d0 commit e16ac57

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/cloudflare/_utils/_transform.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import pathlib
66
from typing import Any, Mapping, TypeVar, cast
77
from datetime import date, datetime
8-
from typing_extensions import Literal, get_args, override, get_type_hints
8+
from typing_extensions import Literal, get_args, override, get_type_hints as _get_type_hints
99

1010
import anyio
1111
import pydantic
1212

1313
from ._utils import (
1414
is_list,
1515
is_given,
16+
lru_cache,
1617
is_mapping,
1718
is_iterable,
1819
)
@@ -109,6 +110,7 @@ class Params(TypedDict, total=False):
109110
return cast(_T, transformed)
110111

111112

113+
@lru_cache(maxsize=8096)
112114
def _get_annotated_type(type_: type) -> type | None:
113115
"""If the given type is an `Annotated` type then it is returned, if not `None` is returned.
114116
@@ -433,3 +435,13 @@ async def _async_transform_typeddict(
433435
else:
434436
result[_maybe_transform_key(key, type_)] = await _async_transform_recursive(value, annotation=type_)
435437
return result
438+
439+
440+
@lru_cache(maxsize=8096)
441+
def get_type_hints(
442+
obj: Any,
443+
globalns: dict[str, Any] | None = None,
444+
localns: Mapping[str, Any] | None = None,
445+
include_extras: bool = False,
446+
) -> dict[str, Any]:
447+
return _get_type_hints(obj, globalns=globalns, localns=localns, include_extras=include_extras)

src/cloudflare/_utils/_typing.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
get_origin,
1414
)
1515

16+
from ._utils import lru_cache
1617
from .._types import InheritsGeneric
1718
from .._compat import is_union as _is_union
1819

@@ -66,6 +67,7 @@ def is_type_alias_type(tp: Any, /) -> TypeIs[typing_extensions.TypeAliasType]:
6667

6768

6869
# Extracts T from Annotated[T, ...] or from Required[Annotated[T, ...]]
70+
@lru_cache(maxsize=8096)
6971
def strip_annotated_type(typ: type) -> type:
7072
if is_required_type(typ) or is_annotated_type(typ):
7173
return strip_annotated_type(cast(type, get_args(typ)[0]))

0 commit comments

Comments
 (0)