Home / Function/ analyze_param() — fastapi Function Reference

analyze_param() — fastapi Function Reference

Architecture documentation for the analyze_param() function in utils.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  3a586946_6748_d24d_8ee8_d3c9dbd7e696["analyze_param()"]
  9e602cbf_3139_86ae_5666_97b8806942de["utils.py"]
  3a586946_6748_d24d_8ee8_d3c9dbd7e696 -->|defined in| 9e602cbf_3139_86ae_5666_97b8806942de
  097a7e41_8095_d61f_b849_128514c58040["get_dependant()"]
  097a7e41_8095_d61f_b849_128514c58040 -->|calls| 3a586946_6748_d24d_8ee8_d3c9dbd7e696
  60ff2195_480d_a353_ee3d_37b198c2b01c["ensure_multipart_is_installed()"]
  3a586946_6748_d24d_8ee8_d3c9dbd7e696 -->|calls| 60ff2195_480d_a353_ee3d_37b198c2b01c
  8e7d845c_a4e7_ee84_5295_5f3b995b05b4["create_model_field()"]
  3a586946_6748_d24d_8ee8_d3c9dbd7e696 -->|calls| 8e7d845c_a4e7_ee84_5295_5f3b995b05b4
  style 3a586946_6748_d24d_8ee8_d3c9dbd7e696 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/dependencies/utils.py lines 363–529

def analyze_param(
    *,
    param_name: str,
    annotation: Any,
    value: Any,
    is_path_param: bool,
) -> ParamDetails:
    field_info = None
    depends = None
    type_annotation: Any = Any
    use_annotation: Any = Any
    if is_typealiastype(annotation):
        # unpack in case PEP 695 type syntax is used
        annotation = annotation.__value__
    if annotation is not inspect.Signature.empty:
        use_annotation = annotation
        type_annotation = annotation
    # Extract Annotated info
    if get_origin(use_annotation) is Annotated:
        annotated_args = get_args(annotation)
        type_annotation = annotated_args[0]
        fastapi_annotations = [
            arg
            for arg in annotated_args[1:]
            if isinstance(arg, (FieldInfo, params.Depends))
        ]
        fastapi_specific_annotations = [
            arg
            for arg in fastapi_annotations
            if isinstance(
                arg,
                (
                    params.Param,
                    params.Body,
                    params.Depends,
                ),
            )
        ]
        if fastapi_specific_annotations:
            fastapi_annotation: Union[FieldInfo, params.Depends, None] = (
                fastapi_specific_annotations[-1]
            )
        else:
            fastapi_annotation = None
        # Set default for Annotated FieldInfo
        if isinstance(fastapi_annotation, FieldInfo):
            # Copy `field_info` because we mutate `field_info.default` below.
            field_info = copy_field_info(
                field_info=fastapi_annotation,
                annotation=use_annotation,
            )
            assert (
                field_info.default == Undefined or field_info.default == RequiredParam
            ), (
                f"`{field_info.__class__.__name__}` default value cannot be set in"
                f" `Annotated` for {param_name!r}. Set the default value with `=` instead."
            )
            if value is not inspect.Signature.empty:
                assert not is_path_param, "Path parameters cannot have default values"
                field_info.default = value
            else:
                field_info.default = RequiredParam
        # Get Annotated Depends
        elif isinstance(fastapi_annotation, params.Depends):
            depends = fastapi_annotation
    # Get Depends from default value
    if isinstance(value, params.Depends):
        assert depends is None, (
            "Cannot specify `Depends` in `Annotated` and default value"
            f" together for {param_name!r}"
        )
        assert field_info is None, (
            "Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a"
            f" default value together for {param_name!r}"
        )
        depends = value
    # Get FieldInfo from default value
    elif isinstance(value, FieldInfo):
        assert field_info is None, (
            "Cannot specify FastAPI annotations in `Annotated` and default value"
            f" together for {param_name!r}"

Subdomains

Called By

Frequently Asked Questions

What does analyze_param() do?
analyze_param() is a function in the fastapi codebase, defined in fastapi/dependencies/utils.py.
Where is analyze_param() defined?
analyze_param() is defined in fastapi/dependencies/utils.py at line 363.
What does analyze_param() call?
analyze_param() calls 2 function(s): create_model_field, ensure_multipart_is_installed.
What calls analyze_param()?
analyze_param() is called by 1 function(s): get_dependant.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free