11# mypy: ignore-errors
22# flake8: noqa
3+ from __future__ import annotations
4+
35from collections import deque
46from copy import copy
57
810
911from dataclasses import dataclass , is_dataclass
1012from enum import Enum
11- from typing import Any , Dict , List , Set , Tuple , Type , Union , FrozenSet , Deque , Sequence , Mapping
13+ from typing import Any , Deque , FrozenSet , List , Mapping , Sequence , Set , Tuple , Union
1214
1315from typing_extensions import Annotated , Literal , get_origin , get_args
1416
5658
5759sequence_types = tuple (sequence_annotation_to_type .keys ())
5860
59- RequestErrorModel : Type [BaseModel ] = create_model ("Request" )
61+ RequestErrorModel : type [BaseModel ] = create_model ("Request" )
6062
6163
6264class ErrorWrapper (Exception ):
@@ -101,8 +103,8 @@ def serialize(
101103 value : Any ,
102104 * ,
103105 mode : Literal ["json" , "python" ] = "json" ,
104- include : Union [ IncEx , None ] = None ,
105- exclude : Union [ IncEx , None ] = None ,
106+ include : IncEx | None = None ,
107+ exclude : IncEx | None = None ,
106108 by_alias : bool = True ,
107109 exclude_unset : bool = False ,
108110 exclude_defaults : bool = False ,
@@ -120,8 +122,8 @@ def serialize(
120122 )
121123
122124 def validate (
123- self , value : Any , values : Dict [str , Any ] = {}, * , loc : Tuple [ Union [ int , str ] , ...] = ()
124- ) -> Tuple [Any , Union [ List [ Dict [ str , Any ]], None ] ]:
125+ self , value : Any , values : dict [str , Any ] = {}, * , loc : tuple [ int | str , ...] = ()
126+ ) -> tuple [Any , list [ dict [ str , Any ]] | None ]:
125127 try :
126128 return (self ._type_adapter .validate_python (value , from_attributes = True ), None )
127129 except ValidationError as exc :
@@ -136,11 +138,11 @@ def get_schema_from_model_field(
136138 * ,
137139 field : ModelField ,
138140 model_name_map : ModelNameMap ,
139- field_mapping : Dict [
140- Tuple [ModelField , Literal ["validation" , "serialization" ]],
141+ field_mapping : dict [
142+ tuple [ModelField , Literal ["validation" , "serialization" ]],
141143 JsonSchemaValue ,
142144 ],
143- ) -> Dict [str , Any ]:
145+ ) -> dict [str , Any ]:
144146 json_schema = field_mapping [(field , field .mode )]
145147 if "$ref" not in json_schema :
146148 # MAINTENANCE: remove when deprecating Pydantic v1
@@ -151,39 +153,39 @@ def get_schema_from_model_field(
151153
152154def get_definitions (
153155 * ,
154- fields : List [ModelField ],
156+ fields : list [ModelField ],
155157 schema_generator : GenerateJsonSchema ,
156158 model_name_map : ModelNameMap ,
157- ) -> Tuple [
158- Dict [
159- Tuple [ModelField , Literal ["validation" , "serialization" ]],
160- Dict [str , Any ],
159+ ) -> tuple [
160+ dict [
161+ tuple [ModelField , Literal ["validation" , "serialization" ]],
162+ dict [str , Any ],
161163 ],
162- Dict [str , Dict [str , Any ]],
164+ dict [str , dict [str , Any ]],
163165]:
164166 inputs = [(field , field .mode , field ._type_adapter .core_schema ) for field in fields ]
165167 field_mapping , definitions = schema_generator .generate_definitions (inputs = inputs )
166168
167169 return field_mapping , definitions
168170
169171
170- def get_compat_model_name_map (fields : List [ModelField ]) -> ModelNameMap :
172+ def get_compat_model_name_map (fields : list [ModelField ]) -> ModelNameMap :
171173 return {}
172174
173175
174176def get_annotation_from_field_info (annotation : Any , field_info : FieldInfo , field_name : str ) -> Any :
175177 return annotation
176178
177179
178- def model_rebuild (model : Type [BaseModel ]) -> None :
180+ def model_rebuild (model : type [BaseModel ]) -> None :
179181 model .model_rebuild ()
180182
181183
182184def copy_field_info (* , field_info : FieldInfo , annotation : Any ) -> FieldInfo :
183185 return type (field_info ).from_annotation (annotation )
184186
185187
186- def get_missing_field_error (loc : Tuple [str , ...]) -> Dict [str , Any ]:
188+ def get_missing_field_error (loc : tuple [str , ...]) -> dict [str , Any ]:
187189 error = ValidationError .from_exception_data (
188190 "Field required" , [{"type" : "missing" , "loc" : loc , "input" : {}}]
189191 ).errors ()[0 ]
@@ -220,13 +222,13 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]:
220222 return sequence_annotation_to_type [origin_type ](value ) # type: ignore[no-any-return]
221223
222224
223- def _normalize_errors (errors : Sequence [Any ]) -> List [ Dict [str , Any ]]:
225+ def _normalize_errors (errors : Sequence [Any ]) -> list [ dict [str , Any ]]:
224226 return errors # type: ignore[return-value]
225227
226228
227- def create_body_model (* , fields : Sequence [ModelField ], model_name : str ) -> Type [BaseModel ]:
229+ def create_body_model (* , fields : Sequence [ModelField ], model_name : str ) -> type [BaseModel ]:
228230 field_params = {f .name : (f .field_info .annotation , f .field_info ) for f in fields }
229- model : Type [BaseModel ] = create_model (model_name , ** field_params )
231+ model : type [BaseModel ] = create_model (model_name , ** field_params )
230232 return model
231233
232234
@@ -241,7 +243,7 @@ def model_json(model: BaseModel, **kwargs: Any) -> Any:
241243# Common code for both versions
242244
243245
244- def field_annotation_is_complex (annotation : Union [ Type [ Any ], None ] ) -> bool :
246+ def field_annotation_is_complex (annotation : type [ Any ] | None ) -> bool :
245247 origin = get_origin (annotation )
246248 if origin is Union or origin is UnionType :
247249 return any (field_annotation_is_complex (arg ) for arg in get_args (annotation ))
@@ -258,11 +260,11 @@ def field_annotation_is_scalar(annotation: Any) -> bool:
258260 return annotation is Ellipsis or not field_annotation_is_complex (annotation )
259261
260262
261- def field_annotation_is_sequence (annotation : Union [ Type [ Any ], None ] ) -> bool :
263+ def field_annotation_is_sequence (annotation : type [ Any ] | None ) -> bool :
262264 return _annotation_is_sequence (annotation ) or _annotation_is_sequence (get_origin (annotation ))
263265
264266
265- def field_annotation_is_scalar_sequence (annotation : Union [ Type [ Any ], None ] ) -> bool :
267+ def field_annotation_is_scalar_sequence (annotation : type [ Any ] | None ) -> bool :
266268 origin = get_origin (annotation )
267269 if origin is Union or origin is UnionType :
268270 at_least_one_scalar_sequence = False
@@ -307,24 +309,22 @@ def value_is_sequence(value: Any) -> bool:
307309 return isinstance (value , sequence_types ) and not isinstance (value , (str , bytes )) # type: ignore[arg-type]
308310
309311
310- def _annotation_is_complex (annotation : Union [ Type [ Any ], None ] ) -> bool :
312+ def _annotation_is_complex (annotation : type [ Any ] | None ) -> bool :
311313 return (
312314 lenient_issubclass (annotation , (BaseModel , Mapping )) # TODO: UploadFile
313315 or _annotation_is_sequence (annotation )
314316 or is_dataclass (annotation )
315317 )
316318
317319
318- def _annotation_is_sequence (annotation : Union [ Type [ Any ], None ] ) -> bool :
320+ def _annotation_is_sequence (annotation : type [ Any ] | None ) -> bool :
319321 if lenient_issubclass (annotation , (str , bytes )):
320322 return False
321323 return lenient_issubclass (annotation , sequence_types )
322324
323325
324- def _regenerate_error_with_loc (
325- * , errors : Sequence [Any ], loc_prefix : Tuple [Union [str , int ], ...]
326- ) -> List [Dict [str , Any ]]:
327- updated_loc_errors : List [Any ] = [
326+ def _regenerate_error_with_loc (* , errors : Sequence [Any ], loc_prefix : tuple [str | int , ...]) -> list [dict [str , Any ]]:
327+ updated_loc_errors : list [Any ] = [
328328 {** err , "loc" : loc_prefix + err .get ("loc" , ())} for err in _normalize_errors (errors )
329329 ]
330330
0 commit comments