1- # flake8: noqa
1+ from collections . abc import Hashable
22from functools import lru_cache
33from importlib .metadata import version
4- from typing import Any , Dict , Hashable , Optional , Type , TypeVar , Union
4+ from typing import Any , TypeVar
55
66import pydantic
77from packaging .version import Version , parse
88
99PYDANTIC_VER = parse (version ("pydantic" ))
1010
1111Model = TypeVar ("Model" , bound = "pydantic.BaseModel" )
12- IS_PYDANTIC2 = PYDANTIC_VER >= Version ("2.0" )
12+ IS_PYDANTIC2 = Version ("2.0" ) <= PYDANTIC_VER
1313
1414if IS_PYDANTIC2 :
1515 T = TypeVar ("T" , bound = Hashable )
1616
17- @lru_cache ()
18- def create_type_adapter (annot : Type [T ]) -> pydantic .TypeAdapter [T ]:
17+ @lru_cache
18+ def create_type_adapter (annot : type [T ]) -> pydantic .TypeAdapter [T ]:
1919 return pydantic .TypeAdapter (annot )
2020
21- def parse_obj_as (annot : Type [T ], obj : Any ) -> T :
21+ def parse_obj_as (annot : type [T ], obj : Any ) -> T :
2222 return create_type_adapter (annot ).validate_python (obj )
2323
2424 def model_validate (
25- model_class : Type [Model ],
26- message : Dict [str , Any ],
25+ model_class : type [Model ],
26+ message : dict [str , Any ],
2727 ) -> Model :
2828 return model_class .model_validate (message )
2929
30- def model_dump (instance : Model ) -> Dict [str , Any ]:
30+ def model_dump (instance : Model ) -> dict [str , Any ]:
3131 return instance .model_dump (mode = "json" )
3232
3333 def model_validate_json (
34- model_class : Type [Model ],
35- message : Union [ str , bytes , bytearray ] ,
34+ model_class : type [Model ],
35+ message : str | bytes | bytearray ,
3636 ) -> Model :
3737 return model_class .model_validate_json (message )
3838
@@ -41,7 +41,7 @@ def model_dump_json(instance: Model) -> str:
4141
4242 def model_copy (
4343 instance : Model ,
44- update : Optional [ Dict [ str , Any ]] = None ,
44+ update : dict [ str , Any ] | None = None ,
4545 deep : bool = False ,
4646 ) -> Model :
4747 return instance .model_copy (update = update , deep = deep )
@@ -52,17 +52,17 @@ def model_copy(
5252 parse_obj_as = pydantic .parse_obj_as # type: ignore
5353
5454 def model_validate (
55- model_class : Type [Model ],
56- message : Dict [str , Any ],
55+ model_class : type [Model ],
56+ message : dict [str , Any ],
5757 ) -> Model :
5858 return model_class .parse_obj (message )
5959
60- def model_dump (instance : Model ) -> Dict [str , Any ]:
60+ def model_dump (instance : Model ) -> dict [str , Any ]:
6161 return instance .dict ()
6262
6363 def model_validate_json (
64- model_class : Type [Model ],
65- message : Union [ str , bytes , bytearray ] ,
64+ model_class : type [Model ],
65+ message : str | bytes | bytearray ,
6666 ) -> Model :
6767 return model_class .parse_raw (message ) # type: ignore[arg-type]
6868
@@ -71,7 +71,7 @@ def model_dump_json(instance: Model) -> str:
7171
7272 def model_copy (
7373 instance : Model ,
74- update : Optional [ Dict [ str , Any ]] = None ,
74+ update : dict [ str , Any ] | None = None ,
7575 deep : bool = False ,
7676 ) -> Model :
7777 return instance .copy (update = update , deep = deep )
0 commit comments