Browse Source

♻️ Refactor and simplify internal `analyze_param()` to structure data with dataclasses instead of tuple (#12099)

pull/12100/head
Sebastián Ramírez 7 months ago
committed by GitHub
parent
commit
08547e1d57
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 30
      fastapi/dependencies/utils.py

30
fastapi/dependencies/utils.py

@ -1,6 +1,7 @@
import inspect import inspect
from contextlib import AsyncExitStack, contextmanager from contextlib import AsyncExitStack, contextmanager
from copy import copy, deepcopy from copy import copy, deepcopy
from dataclasses import dataclass
from typing import ( from typing import (
Any, Any,
Callable, Callable,
@ -258,16 +259,16 @@ def get_dependant(
) )
for param_name, param in signature_params.items(): for param_name, param in signature_params.items():
is_path_param = param_name in path_param_names is_path_param = param_name in path_param_names
type_annotation, depends, param_field = analyze_param( param_details = analyze_param(
param_name=param_name, param_name=param_name,
annotation=param.annotation, annotation=param.annotation,
value=param.default, value=param.default,
is_path_param=is_path_param, is_path_param=is_path_param,
) )
if depends is not None: if param_details.depends is not None:
sub_dependant = get_param_sub_dependant( sub_dependant = get_param_sub_dependant(
param_name=param_name, param_name=param_name,
depends=depends, depends=param_details.depends,
path=path, path=path,
security_scopes=security_scopes, security_scopes=security_scopes,
) )
@ -275,18 +276,18 @@ def get_dependant(
continue continue
if add_non_field_param_to_dependency( if add_non_field_param_to_dependency(
param_name=param_name, param_name=param_name,
type_annotation=type_annotation, type_annotation=param_details.type_annotation,
dependant=dependant, dependant=dependant,
): ):
assert ( assert (
param_field is None param_details.field is None
), f"Cannot specify multiple FastAPI annotations for {param_name!r}" ), f"Cannot specify multiple FastAPI annotations for {param_name!r}"
continue continue
assert param_field is not None assert param_details.field is not None
if is_body_param(param_field=param_field, is_path_param=is_path_param): if is_body_param(param_field=param_details.field, is_path_param=is_path_param):
dependant.body_params.append(param_field) dependant.body_params.append(param_details.field)
else: else:
add_param_to_fields(field=param_field, dependant=dependant) add_param_to_fields(field=param_details.field, dependant=dependant)
return dependant return dependant
@ -314,13 +315,20 @@ def add_non_field_param_to_dependency(
return None return None
@dataclass
class ParamDetails:
type_annotation: Any
depends: Optional[params.Depends]
field: Optional[ModelField]
def analyze_param( def analyze_param(
*, *,
param_name: str, param_name: str,
annotation: Any, annotation: Any,
value: Any, value: Any,
is_path_param: bool, is_path_param: bool,
) -> Tuple[Any, Optional[params.Depends], Optional[ModelField]]: ) -> ParamDetails:
field_info = None field_info = None
depends = None depends = None
type_annotation: Any = Any type_annotation: Any = Any
@ -450,7 +458,7 @@ def analyze_param(
field_info=field_info, field_info=field_info,
) )
return type_annotation, depends, field return ParamDetails(type_annotation=type_annotation, depends=depends, field=field)
def is_body_param(*, param_field: ModelField, is_path_param: bool) -> bool: def is_body_param(*, param_field: ModelField, is_path_param: bool) -> bool:

Loading…
Cancel
Save