Skip to content

Commit

Permalink
feat: reuse pydantic v2 TypeAdapter instance (#284)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Kirilin <[email protected]>
  • Loading branch information
unights and s3rius authored Feb 5, 2024
1 parent d708468 commit 74467d0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion taskiq/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# flake8: noqa
from functools import lru_cache
from typing import Any, Dict, Optional, Type, TypeVar, Union

import pydantic
Expand All @@ -13,8 +14,12 @@
if IS_PYDANTIC2:
T = TypeVar("T")

@lru_cache()
def create_type_adapter(annot: T) -> pydantic.TypeAdapter[T]:
return pydantic.TypeAdapter(annot)

def parse_obj_as(annot: T, obj: Any) -> T:
return pydantic.TypeAdapter(annot).validate_python(obj)
return create_type_adapter(annot).validate_python(obj)

def model_validate(
model_class: Type[Model],
Expand Down

0 comments on commit 74467d0

Please sign in to comment.