Skip to content

Commit 74467d0

Browse files
unightss3rius
andauthored
feat: reuse pydantic v2 TypeAdapter instance (#284)
Co-authored-by: Pavel Kirilin <[email protected]>
1 parent d708468 commit 74467d0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

taskiq/compat.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# flake8: noqa
2+
from functools import lru_cache
23
from typing import Any, Dict, Optional, Type, TypeVar, Union
34

45
import pydantic
@@ -13,8 +14,12 @@
1314
if IS_PYDANTIC2:
1415
T = TypeVar("T")
1516

17+
@lru_cache()
18+
def create_type_adapter(annot: T) -> pydantic.TypeAdapter[T]:
19+
return pydantic.TypeAdapter(annot)
20+
1621
def parse_obj_as(annot: T, obj: Any) -> T:
17-
return pydantic.TypeAdapter(annot).validate_python(obj)
22+
return create_type_adapter(annot).validate_python(obj)
1823

1924
def model_validate(
2025
model_class: Type[Model],

0 commit comments

Comments
 (0)