28
28
from fastapi .security .base import SecurityBase
29
29
from fastapi .security .oauth2 import OAuth2 , SecurityScopes
30
30
from fastapi .security .open_id_connect_url import OpenIdConnect
31
- from fastapi .utils import (
32
- PYDANTIC_1 ,
33
- create_response_field ,
34
- get_field_info ,
35
- get_path_param_names ,
36
- )
37
- from pydantic import BaseConfig , BaseModel , create_model
31
+ from fastapi .utils import create_response_field , get_path_param_names
32
+ from pydantic import BaseModel , create_model
38
33
from pydantic .error_wrappers import ErrorWrapper
39
34
from pydantic .errors import MissingError
35
+ from pydantic .fields import (
36
+ SHAPE_LIST ,
37
+ SHAPE_SEQUENCE ,
38
+ SHAPE_SET ,
39
+ SHAPE_SINGLETON ,
40
+ SHAPE_TUPLE ,
41
+ SHAPE_TUPLE_ELLIPSIS ,
42
+ FieldInfo ,
43
+ ModelField ,
44
+ Required ,
45
+ )
46
+ from pydantic .schema import get_annotation_from_field_info
47
+ from pydantic .typing import ForwardRef , evaluate_forwardref
40
48
from pydantic .utils import lenient_issubclass
41
49
from starlette .background import BackgroundTasks
42
50
from starlette .concurrency import run_in_threadpool
45
53
from starlette .responses import Response
46
54
from starlette .websockets import WebSocket
47
55
48
- try :
49
- from pydantic .fields import (
50
- SHAPE_LIST ,
51
- SHAPE_SEQUENCE ,
52
- SHAPE_SET ,
53
- SHAPE_SINGLETON ,
54
- SHAPE_TUPLE ,
55
- SHAPE_TUPLE_ELLIPSIS ,
56
- FieldInfo ,
57
- ModelField ,
58
- Required ,
59
- )
60
- from pydantic .schema import get_annotation_from_field_info
61
- from pydantic .typing import ForwardRef , evaluate_forwardref
62
- except ImportError : # pragma: nocover
63
- # TODO: remove when removing support for Pydantic < 1.0.0
64
- from pydantic import Schema as FieldInfo # type: ignore
65
- from pydantic .fields import Field as ModelField # type: ignore
66
- from pydantic .fields import Required , Shape # type: ignore
67
- from pydantic .schema import get_annotation_from_schema # type: ignore
68
- from pydantic .utils import ForwardRef , evaluate_forwardref # type: ignore
69
-
70
- SHAPE_LIST = Shape .LIST
71
- SHAPE_SEQUENCE = Shape .SEQUENCE
72
- SHAPE_SET = Shape .SET
73
- SHAPE_SINGLETON = Shape .SINGLETON
74
- SHAPE_TUPLE = Shape .TUPLE
75
- SHAPE_TUPLE_ELLIPSIS = Shape .TUPLE_ELLIPS
76
-
77
- def get_annotation_from_field_info (
78
- annotation : Any , field_info : FieldInfo , field_name : str
79
- ) -> Type [Any ]:
80
- return get_annotation_from_schema (annotation , field_info )
81
-
82
-
83
56
sequence_shapes = {
84
57
SHAPE_LIST ,
85
58
SHAPE_SET ,
@@ -113,7 +86,7 @@ def get_annotation_from_field_info(
113
86
114
87
115
88
def check_file_field (field : ModelField ) -> None :
116
- field_info = get_field_info ( field )
89
+ field_info = field . field_info
117
90
if isinstance (field_info , params .Form ):
118
91
try :
119
92
# __version__ is available in both multiparts, and can be mocked
@@ -239,7 +212,7 @@ def get_flat_params(dependant: Dependant) -> List[ModelField]:
239
212
240
213
241
214
def is_scalar_field (field : ModelField ) -> bool :
242
- field_info = get_field_info ( field )
215
+ field_info = field . field_info
243
216
if not (
244
217
field .shape == SHAPE_SINGLETON
245
218
and not lenient_issubclass (field .type_ , BaseModel )
@@ -354,7 +327,7 @@ def get_dependant(
354
327
) and is_scalar_sequence_field (param_field ):
355
328
add_param_to_fields (field = param_field , dependant = dependant )
356
329
else :
357
- field_info = get_field_info ( param_field )
330
+ field_info = param_field . field_info
358
331
assert isinstance (
359
332
field_info , params .Body
360
333
), f"Param: { param_field .name } can only be a request body, using Body(...)"
@@ -430,16 +403,13 @@ def get_param_field(
430
403
)
431
404
field .required = required
432
405
if not had_schema and not is_scalar_field (field = field ):
433
- if PYDANTIC_1 :
434
- field .field_info = params .Body (field_info .default )
435
- else :
436
- field .schema = params .Body (field_info .default ) # type: ignore # pragma: nocover
406
+ field .field_info = params .Body (field_info .default )
437
407
438
408
return field
439
409
440
410
441
411
def add_param_to_fields (* , field : ModelField , dependant : Dependant ) -> None :
442
- field_info = cast (params .Param , get_field_info ( field ) )
412
+ field_info = cast (params .Param , field . field_info )
443
413
if field_info .in_ == params .ParamTypes .path :
444
414
dependant .path_params .append (field )
445
415
elif field_info .in_ == params .ParamTypes .query :
@@ -642,26 +612,17 @@ def request_params_to_args(
642
612
value = received_params .getlist (field .alias ) or field .default
643
613
else :
644
614
value = received_params .get (field .alias )
645
- field_info = get_field_info ( field )
615
+ field_info = field . field_info
646
616
assert isinstance (
647
617
field_info , params .Param
648
618
), "Params must be subclasses of Param"
649
619
if value is None :
650
620
if field .required :
651
- if PYDANTIC_1 :
652
- errors .append (
653
- ErrorWrapper (
654
- MissingError (), loc = (field_info .in_ .value , field .alias )
655
- )
656
- )
657
- else : # pragma: nocover
658
- errors .append (
659
- ErrorWrapper ( # type: ignore
660
- MissingError (),
661
- loc = (field_info .in_ .value , field .alias ),
662
- config = BaseConfig ,
663
- )
621
+ errors .append (
622
+ ErrorWrapper (
623
+ MissingError (), loc = (field_info .in_ .value , field .alias )
664
624
)
625
+ )
665
626
else :
666
627
values [field .name ] = deepcopy (field .default )
667
628
continue
@@ -685,7 +646,7 @@ async def request_body_to_args(
685
646
errors = []
686
647
if required_params :
687
648
field = required_params [0 ]
688
- field_info = get_field_info ( field )
649
+ field_info = field . field_info
689
650
embed = getattr (field_info , "embed" , None )
690
651
field_alias_omitted = len (required_params ) == 1 and not embed
691
652
if field_alias_omitted :
@@ -752,12 +713,7 @@ async def request_body_to_args(
752
713
753
714
754
715
def get_missing_field_error (loc : Tuple [str , ...]) -> ErrorWrapper :
755
- if PYDANTIC_1 :
756
- missing_field_error = ErrorWrapper (MissingError (), loc = loc )
757
- else : # pragma: no cover
758
- missing_field_error = ErrorWrapper ( # type: ignore
759
- MissingError (), loc = loc , config = BaseConfig ,
760
- )
716
+ missing_field_error = ErrorWrapper (MissingError (), loc = loc )
761
717
return missing_field_error
762
718
763
719
@@ -775,7 +731,7 @@ def get_schema_compatible_field(*, field: ModelField) -> ModelField:
775
731
default = field .default ,
776
732
required = field .required ,
777
733
alias = field .alias ,
778
- field_info = get_field_info ( field ) ,
734
+ field_info = field . field_info ,
779
735
)
780
736
return out_field
781
737
@@ -785,7 +741,7 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
785
741
if not flat_dependant .body_params :
786
742
return None
787
743
first_param = flat_dependant .body_params [0 ]
788
- field_info = get_field_info ( first_param )
744
+ field_info = first_param . field_info
789
745
embed = getattr (field_info , "embed" , None )
790
746
body_param_names_set = {param .name for param in flat_dependant .body_params }
791
747
if len (body_param_names_set ) == 1 and not embed :
@@ -796,29 +752,25 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
796
752
# in case a sub-dependency is evaluated with a single unique body field
797
753
# That is combined (embedded) with other body fields
798
754
for param in flat_dependant .body_params :
799
- setattr (get_field_info ( param ) , "embed" , True )
755
+ setattr (param . field_info , "embed" , True )
800
756
model_name = "Body_" + name
801
757
BodyModel = create_model (model_name )
802
758
for f in flat_dependant .body_params :
803
759
BodyModel .__fields__ [f .name ] = get_schema_compatible_field (field = f )
804
760
required = any (True for f in flat_dependant .body_params if f .required )
805
761
806
762
BodyFieldInfo_kwargs : Dict [str , Any ] = dict (default = None )
807
- if any (
808
- isinstance (get_field_info (f ), params .File ) for f in flat_dependant .body_params
809
- ):
763
+ if any (isinstance (f .field_info , params .File ) for f in flat_dependant .body_params ):
810
764
BodyFieldInfo : Type [params .Body ] = params .File
811
- elif any (
812
- isinstance (get_field_info (f ), params .Form ) for f in flat_dependant .body_params
813
- ):
765
+ elif any (isinstance (f .field_info , params .Form ) for f in flat_dependant .body_params ):
814
766
BodyFieldInfo = params .Form
815
767
else :
816
768
BodyFieldInfo = params .Body
817
769
818
770
body_param_media_types = [
819
- getattr (get_field_info ( f ) , "media_type" )
771
+ getattr (f . field_info , "media_type" )
820
772
for f in flat_dependant .body_params
821
- if isinstance (get_field_info ( f ) , params .Body )
773
+ if isinstance (f . field_info , params .Body )
822
774
]
823
775
if len (set (body_param_media_types )) == 1 :
824
776
BodyFieldInfo_kwargs ["media_type" ] = body_param_media_types [0 ]
0 commit comments