From 15dd2b67d3f8763d5cd523b79a1c901c05d48bd7 Mon Sep 17 00:00:00 2001 From: Victorien <65306057+Viicos@users.noreply.github.com> Date: Fri, 28 Feb 2025 16:15:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20internal=20annota?= =?UTF-8?q?tion=20usage=20for=20compatibilty=20with=20Pydantic=202.11=20(#?= =?UTF-8?q?13314)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sofie Van Landeghem Co-authored-by: svlandeg --- fastapi/dependencies/utils.py | 8 ++++---- tests/test_analyze_param.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/test_analyze_param.py diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index e2866b488..09dd6f1b9 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -449,15 +449,15 @@ def analyze_param( # We might check here that `default_value is RequiredParam`, but the fact is that the same # parameter might sometimes be a path parameter and sometimes not. See # `tests/test_infer_param_optionality.py` for an example. - field_info = params.Path(annotation=use_annotation) + field_info = params.Path(annotation=type_annotation) elif is_uploadfile_or_nonable_uploadfile_annotation( type_annotation ) or is_uploadfile_sequence_annotation(type_annotation): - field_info = params.File(annotation=use_annotation, default=default_value) + field_info = params.File(annotation=type_annotation, default=default_value) elif not field_annotation_is_scalar(annotation=type_annotation): - field_info = params.Body(annotation=use_annotation, default=default_value) + field_info = params.Body(annotation=type_annotation, default=default_value) else: - field_info = params.Query(annotation=use_annotation, default=default_value) + field_info = params.Query(annotation=type_annotation, default=default_value) field = None # It's a field_info, not a dependency diff --git a/tests/test_analyze_param.py b/tests/test_analyze_param.py new file mode 100644 index 000000000..9fd3fa6d0 --- /dev/null +++ b/tests/test_analyze_param.py @@ -0,0 +1,22 @@ +from inspect import signature + +from fastapi.dependencies.utils import ParamDetails, analyze_param +from pydantic import Field +from typing_extensions import Annotated + +from .utils import needs_pydanticv2 + + +def func(user: Annotated[int, Field(strict=True)]): ... + + +@needs_pydanticv2 +def test_analyze_param(): + result = analyze_param( + param_name="user", + annotation=signature(func).parameters["user"].annotation, + value=object(), + is_path_param=False, + ) + assert isinstance(result, ParamDetails) + assert result.field.field_info.annotation is int