Browse Source

♻️ Error out when using pydantic.v1

pull/14609/head
Sebastián Ramírez 7 months ago
parent
commit
4b0fca4cbd
  1. 11
      fastapi/dependencies/utils.py
  2. 6
      fastapi/exceptions.py
  3. 18
      fastapi/routing.py

11
fastapi/dependencies/utils.py

@ -1,7 +1,6 @@
import dataclasses
import inspect
import sys
import warnings
from collections.abc import Coroutine, Mapping, Sequence
from contextlib import AsyncExitStack, contextmanager
from copy import copy, deepcopy
@ -51,7 +50,7 @@ from fastapi.concurrency import (
contextmanager_in_threadpool,
)
from fastapi.dependencies.models import Dependant
from fastapi.exceptions import DependencyScopeError
from fastapi.exceptions import DependencyScopeError, PydanticV1NotSupportedError
from fastapi.logger import logger
from fastapi.security.oauth2 import SecurityScopes
from fastapi.types import DependencyCacheKey
@ -324,11 +323,9 @@ def get_dependant(
continue
assert param_details.field is not None
if isinstance(param_details.field, may_v1.ModelField):
warnings.warn(
"pydantic.v1 is deprecated and will soon stop being supported by FastAPI."
f" Please update the param {param_name}: {param_details.type_annotation!r}.",
category=DeprecationWarning,
stacklevel=5,
raise PydanticV1NotSupportedError(
"pydantic.v1 models are no longer supported by FastAPI."
f" Please update the param {param_name}: {param_details.type_annotation!r}."
)
if isinstance(
param_details.field.field_info, (params.Body, temp_pydantic_v1_params.Body)

6
fastapi/exceptions.py

@ -231,3 +231,9 @@ class ResponseValidationError(ValidationException):
) -> None:
super().__init__(errors, endpoint_ctx=endpoint_ctx)
self.body = body
class PydanticV1NotSupportedError(FastAPIError):
"""
A pydantic.v1 model is used, which is no longer supported.
"""

18
fastapi/routing.py

@ -2,7 +2,6 @@ import email.message
import functools
import inspect
import json
import warnings
from collections.abc import (
AsyncIterator,
Awaitable,
@ -48,6 +47,7 @@ from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import (
EndpointContext,
FastAPIError,
PydanticV1NotSupportedError,
RequestValidationError,
ResponseValidationError,
WebSocketRequestValidationError,
@ -637,11 +637,9 @@ class APIRoute(routing.Route):
)
response_name = "Response_" + self.unique_id
if annotation_is_pydantic_v1(self.response_model):
warnings.warn(
"pydantic.v1 is deprecated and will soon stop being supported by FastAPI."
f" Please update the response model {self.response_model!r}.",
category=DeprecationWarning,
stacklevel=4,
raise PydanticV1NotSupportedError(
"pydantic.v1 models are no longer supported by FastAPI."
f" Please update the response model {self.response_model!r}."
)
self.response_field = create_model_field(
name=response_name,
@ -677,11 +675,9 @@ class APIRoute(routing.Route):
)
response_name = f"Response_{additional_status_code}_{self.unique_id}"
if annotation_is_pydantic_v1(model):
warnings.warn(
"pydantic.v1 is deprecated and will soon stop being supported by FastAPI."
f" In responses={{}}, please update {model}.",
category=DeprecationWarning,
stacklevel=4,
raise PydanticV1NotSupportedError(
"pydantic.v1 models are no longer supported by FastAPI."
f" In responses={{}}, please update {model}."
)
response_field = create_model_field(
name=response_name, type_=model, mode="serialization"

Loading…
Cancel
Save