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

6
fastapi/exceptions.py

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

Loading…
Cancel
Save