1
1
# mypy: ignore-errors
2
2
# flake8: noqa
3
+ from __future__ import annotations
4
+
3
5
from collections import deque
4
6
from copy import copy
5
7
8
10
9
11
from dataclasses import dataclass , is_dataclass
10
12
from 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
12
14
13
15
from typing_extensions import Annotated , Literal , get_origin , get_args
14
16
56
58
57
59
sequence_types = tuple (sequence_annotation_to_type .keys ())
58
60
59
- RequestErrorModel : Type [BaseModel ] = create_model ("Request" )
61
+ RequestErrorModel : type [BaseModel ] = create_model ("Request" )
60
62
61
63
62
64
class ErrorWrapper (Exception ):
@@ -101,8 +103,8 @@ def serialize(
101
103
value : Any ,
102
104
* ,
103
105
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 ,
106
108
by_alias : bool = True ,
107
109
exclude_unset : bool = False ,
108
110
exclude_defaults : bool = False ,
@@ -120,8 +122,8 @@ def serialize(
120
122
)
121
123
122
124
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 ]:
125
127
try :
126
128
return (self ._type_adapter .validate_python (value , from_attributes = True ), None )
127
129
except ValidationError as exc :
@@ -136,11 +138,11 @@ def get_schema_from_model_field(
136
138
* ,
137
139
field : ModelField ,
138
140
model_name_map : ModelNameMap ,
139
- field_mapping : Dict [
140
- Tuple [ModelField , Literal ["validation" , "serialization" ]],
141
+ field_mapping : dict [
142
+ tuple [ModelField , Literal ["validation" , "serialization" ]],
141
143
JsonSchemaValue ,
142
144
],
143
- ) -> Dict [str , Any ]:
145
+ ) -> dict [str , Any ]:
144
146
json_schema = field_mapping [(field , field .mode )]
145
147
if "$ref" not in json_schema :
146
148
# MAINTENANCE: remove when deprecating Pydantic v1
@@ -151,39 +153,39 @@ def get_schema_from_model_field(
151
153
152
154
def get_definitions (
153
155
* ,
154
- fields : List [ModelField ],
156
+ fields : list [ModelField ],
155
157
schema_generator : GenerateJsonSchema ,
156
158
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 ],
161
163
],
162
- Dict [str , Dict [str , Any ]],
164
+ dict [str , dict [str , Any ]],
163
165
]:
164
166
inputs = [(field , field .mode , field ._type_adapter .core_schema ) for field in fields ]
165
167
field_mapping , definitions = schema_generator .generate_definitions (inputs = inputs )
166
168
167
169
return field_mapping , definitions
168
170
169
171
170
- def get_compat_model_name_map (fields : List [ModelField ]) -> ModelNameMap :
172
+ def get_compat_model_name_map (fields : list [ModelField ]) -> ModelNameMap :
171
173
return {}
172
174
173
175
174
176
def get_annotation_from_field_info (annotation : Any , field_info : FieldInfo , field_name : str ) -> Any :
175
177
return annotation
176
178
177
179
178
- def model_rebuild (model : Type [BaseModel ]) -> None :
180
+ def model_rebuild (model : type [BaseModel ]) -> None :
179
181
model .model_rebuild ()
180
182
181
183
182
184
def copy_field_info (* , field_info : FieldInfo , annotation : Any ) -> FieldInfo :
183
185
return type (field_info ).from_annotation (annotation )
184
186
185
187
186
- def get_missing_field_error (loc : Tuple [str , ...]) -> Dict [str , Any ]:
188
+ def get_missing_field_error (loc : tuple [str , ...]) -> dict [str , Any ]:
187
189
error = ValidationError .from_exception_data (
188
190
"Field required" , [{"type" : "missing" , "loc" : loc , "input" : {}}]
189
191
).errors ()[0 ]
@@ -220,13 +222,13 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]:
220
222
return sequence_annotation_to_type [origin_type ](value ) # type: ignore[no-any-return]
221
223
222
224
223
- def _normalize_errors (errors : Sequence [Any ]) -> List [ Dict [str , Any ]]:
225
+ def _normalize_errors (errors : Sequence [Any ]) -> list [ dict [str , Any ]]:
224
226
return errors # type: ignore[return-value]
225
227
226
228
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 ]:
228
230
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 )
230
232
return model
231
233
232
234
@@ -241,7 +243,7 @@ def model_json(model: BaseModel, **kwargs: Any) -> Any:
241
243
# Common code for both versions
242
244
243
245
244
- def field_annotation_is_complex (annotation : Union [ Type [ Any ], None ] ) -> bool :
246
+ def field_annotation_is_complex (annotation : type [ Any ] | None ) -> bool :
245
247
origin = get_origin (annotation )
246
248
if origin is Union or origin is UnionType :
247
249
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:
258
260
return annotation is Ellipsis or not field_annotation_is_complex (annotation )
259
261
260
262
261
- def field_annotation_is_sequence (annotation : Union [ Type [ Any ], None ] ) -> bool :
263
+ def field_annotation_is_sequence (annotation : type [ Any ] | None ) -> bool :
262
264
return _annotation_is_sequence (annotation ) or _annotation_is_sequence (get_origin (annotation ))
263
265
264
266
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 :
266
268
origin = get_origin (annotation )
267
269
if origin is Union or origin is UnionType :
268
270
at_least_one_scalar_sequence = False
@@ -307,24 +309,22 @@ def value_is_sequence(value: Any) -> bool:
307
309
return isinstance (value , sequence_types ) and not isinstance (value , (str , bytes )) # type: ignore[arg-type]
308
310
309
311
310
- def _annotation_is_complex (annotation : Union [ Type [ Any ], None ] ) -> bool :
312
+ def _annotation_is_complex (annotation : type [ Any ] | None ) -> bool :
311
313
return (
312
314
lenient_issubclass (annotation , (BaseModel , Mapping )) # TODO: UploadFile
313
315
or _annotation_is_sequence (annotation )
314
316
or is_dataclass (annotation )
315
317
)
316
318
317
319
318
- def _annotation_is_sequence (annotation : Union [ Type [ Any ], None ] ) -> bool :
320
+ def _annotation_is_sequence (annotation : type [ Any ] | None ) -> bool :
319
321
if lenient_issubclass (annotation , (str , bytes )):
320
322
return False
321
323
return lenient_issubclass (annotation , sequence_types )
322
324
323
325
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 ] = [
328
328
{** err , "loc" : loc_prefix + err .get ("loc" , ())} for err in _normalize_errors (errors )
329
329
]
330
330
0 commit comments