From ef0b8280efd36e1921efdbcd9696b0191703fa64 Mon Sep 17 00:00:00 2001 From: Mathieu Bressolle-Chataigner Date: Wed, 17 Jul 2024 00:05:13 +0200 Subject: [PATCH 1/2] catch PydanticUndefinedAnnotation in create_response_field this exception may be raised when having some unresolvable forward refs most probably when trying to return a complex SQLAlchemy object --- fastapi/_compat.py | 4 ++++ fastapi/utils.py | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 06b847b4f..4609cbe82 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -47,6 +47,7 @@ sequence_types = tuple(sequence_annotation_to_type.keys()) if PYDANTIC_V2: from pydantic import PydanticSchemaGenerationError as PydanticSchemaGenerationError + from pydantic.errors import PydanticUndefinedAnnotation as PydanticUndefinedAnnotation from pydantic import TypeAdapter from pydantic import ValidationError as ValidationError from pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined] @@ -358,6 +359,9 @@ else: class PydanticSchemaGenerationError(Exception): # type: ignore[no-redef] pass + class PydanticUndefinedAnnotation(Exception): # type: ignore[no-redef] + pass + def with_info_plain_validator_function( # type: ignore[misc] function: Callable[..., Any], *, diff --git a/fastapi/utils.py b/fastapi/utils.py index dfda4e678..eaa332709 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -20,6 +20,7 @@ from fastapi._compat import ( BaseConfig, ModelField, PydanticSchemaGenerationError, + PydanticUndefinedAnnotation, Undefined, UndefinedType, Validator, @@ -97,7 +98,7 @@ def create_response_field( ) try: return ModelField(**kwargs) # type: ignore[arg-type] - except (RuntimeError, PydanticSchemaGenerationError): + except (RuntimeError, PydanticSchemaGenerationError, PydanticUndefinedAnnotation): raise fastapi.exceptions.FastAPIError( "Invalid args for response field! Hint: " f"check that {type_} is a valid Pydantic field type. " From e27c003e5fbd9d292578d076433d4bf72d9df7b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:09:21 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/_compat.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 4609cbe82..8761a5df3 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -47,7 +47,6 @@ sequence_types = tuple(sequence_annotation_to_type.keys()) if PYDANTIC_V2: from pydantic import PydanticSchemaGenerationError as PydanticSchemaGenerationError - from pydantic.errors import PydanticUndefinedAnnotation as PydanticUndefinedAnnotation from pydantic import TypeAdapter from pydantic import ValidationError as ValidationError from pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined] @@ -55,6 +54,9 @@ if PYDANTIC_V2: ) from pydantic._internal._typing_extra import eval_type_lenient from pydantic._internal._utils import lenient_issubclass as lenient_issubclass + from pydantic.errors import ( + PydanticUndefinedAnnotation as PydanticUndefinedAnnotation, + ) from pydantic.fields import FieldInfo from pydantic.json_schema import GenerateJsonSchema as GenerateJsonSchema from pydantic.json_schema import JsonSchemaValue as JsonSchemaValue