|
1 | 1 | from collections.abc import Hashable |
2 | 2 | from functools import lru_cache |
3 | | -from importlib.metadata import version |
4 | 3 | from typing import Any, TypeVar |
5 | 4 |
|
6 | 5 | import pydantic |
7 | | -from packaging.version import Version, parse |
8 | 6 |
|
9 | | -PYDANTIC_VER = parse(version("pydantic")) |
| 7 | +T = TypeVar("T", bound=Hashable) |
10 | 8 |
|
11 | | -Model = TypeVar("Model", bound="pydantic.BaseModel") |
12 | | -IS_PYDANTIC2 = Version("2.0") <= PYDANTIC_VER |
13 | 9 |
|
14 | | -if IS_PYDANTIC2: |
15 | | - T = TypeVar("T", bound=Hashable) |
| 10 | +@lru_cache |
| 11 | +def create_type_adapter(annot: type[T]) -> pydantic.TypeAdapter[T]: |
| 12 | + return pydantic.TypeAdapter(annot) |
16 | 13 |
|
17 | | - @lru_cache |
18 | | - def create_type_adapter(annot: type[T]) -> pydantic.TypeAdapter[T]: |
19 | | - return pydantic.TypeAdapter(annot) |
20 | 14 |
|
21 | | - def parse_obj_as(annot: type[T], obj: Any) -> T: |
22 | | - return create_type_adapter(annot).validate_python(obj) |
23 | | - |
24 | | - def model_validate( |
25 | | - model_class: type[Model], |
26 | | - message: dict[str, Any], |
27 | | - ) -> Model: |
28 | | - return model_class.model_validate(message) |
29 | | - |
30 | | - def model_dump(instance: Model) -> dict[str, Any]: |
31 | | - return instance.model_dump(mode="json") |
32 | | - |
33 | | - def model_validate_json( |
34 | | - model_class: type[Model], |
35 | | - message: str | bytes | bytearray, |
36 | | - ) -> Model: |
37 | | - return model_class.model_validate_json(message) |
38 | | - |
39 | | - def model_dump_json(instance: Model) -> str: |
40 | | - return instance.model_dump_json() |
41 | | - |
42 | | - def model_copy( |
43 | | - instance: Model, |
44 | | - update: dict[str, Any] | None = None, |
45 | | - deep: bool = False, |
46 | | - ) -> Model: |
47 | | - return instance.model_copy(update=update, deep=deep) |
48 | | - |
49 | | - validate_call = pydantic.validate_call |
50 | | - |
51 | | -else: |
52 | | - parse_obj_as = pydantic.parse_obj_as # type: ignore |
53 | | - |
54 | | - def model_validate( |
55 | | - model_class: type[Model], |
56 | | - message: dict[str, Any], |
57 | | - ) -> Model: |
58 | | - return model_class.parse_obj(message) |
59 | | - |
60 | | - def model_dump(instance: Model) -> dict[str, Any]: |
61 | | - return instance.dict() |
62 | | - |
63 | | - def model_validate_json( |
64 | | - model_class: type[Model], |
65 | | - message: str | bytes | bytearray, |
66 | | - ) -> Model: |
67 | | - return model_class.parse_raw(message) # type: ignore[arg-type] |
68 | | - |
69 | | - def model_dump_json(instance: Model) -> str: |
70 | | - return instance.json() |
71 | | - |
72 | | - def model_copy( |
73 | | - instance: Model, |
74 | | - update: dict[str, Any] | None = None, |
75 | | - deep: bool = False, |
76 | | - ) -> Model: |
77 | | - return instance.copy(update=update, deep=deep) |
78 | | - |
79 | | - validate_call = pydantic.validate_arguments # type: ignore |
| 15 | +def parse_obj_as(annot: type[T], obj: Any) -> T: |
| 16 | + return create_type_adapter(annot).validate_python(obj) |
0 commit comments