Browse Source

🔥 Remove additional usages of Pydantic v1

pull/14575/head
Sebastián Ramírez 7 months ago
parent
commit
1586ee9cd6
  1. 16
      fastapi/datastructures.py
  2. 11
      fastapi/params.py
  3. 11
      fastapi/temp_pydantic_v1_params.py

16
fastapi/datastructures.py

@ -1,4 +1,3 @@
from collections.abc import Iterable
from typing import (
Annotated,
Any,
@ -135,27 +134,12 @@ class UploadFile(StarletteUploadFile):
"""
return await super().close()
@classmethod
def __get_validators__(cls: type["UploadFile"]) -> Iterable[Callable[..., Any]]:
yield cls.validate
@classmethod
def validate(cls: type["UploadFile"], v: Any) -> Any:
if not isinstance(v, StarletteUploadFile):
raise ValueError(f"Expected UploadFile, received: {type(v)}")
return v
@classmethod
def _validate(cls, __input_value: Any, _: Any) -> "UploadFile":
if not isinstance(__input_value, StarletteUploadFile):
raise ValueError(f"Expected UploadFile, received: {type(__input_value)}")
return cast(UploadFile, __input_value)
# TODO: remove when deprecating Pydantic v1
@classmethod
def __modify_schema__(cls, field_schema: dict[str, Any]) -> None:
field_schema.update({"type": "string", "format": "binary"})
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler

11
fastapi/params.py

@ -9,7 +9,6 @@ from pydantic.fields import FieldInfo
from typing_extensions import Literal, deprecated
from ._compat import (
PYDANTIC_VERSION_MINOR_TUPLE,
Undefined,
)
@ -110,10 +109,7 @@ class Param(FieldInfo): # type: ignore[misc]
stacklevel=4,
)
current_json_schema_extra = json_schema_extra or extra
if PYDANTIC_VERSION_MINOR_TUPLE < (2, 7):
self.deprecated = deprecated
else:
kwargs["deprecated"] = deprecated
kwargs["deprecated"] = deprecated
if serialization_alias in (_Unset, None) and isinstance(alias, str):
serialization_alias = alias
@ -568,10 +564,7 @@ class Body(FieldInfo): # type: ignore[misc]
stacklevel=4,
)
current_json_schema_extra = json_schema_extra or extra
if PYDANTIC_VERSION_MINOR_TUPLE < (2, 7):
self.deprecated = deprecated
else:
kwargs["deprecated"] = deprecated
kwargs["deprecated"] = deprecated
if serialization_alias in (_Unset, None) and isinstance(alias, str):
serialization_alias = alias
if validation_alias in (_Unset, None):

11
fastapi/temp_pydantic_v1_params.py

@ -6,7 +6,6 @@ from fastapi.params import ParamTypes
from typing_extensions import deprecated
from ._compat.may_v1 import FieldInfo, Undefined
from ._compat.shared import PYDANTIC_VERSION_MINOR_TUPLE
_Unset: Any = Undefined
@ -98,10 +97,7 @@ class Param(FieldInfo): # type: ignore[misc]
stacklevel=4,
)
current_json_schema_extra = json_schema_extra or extra
if PYDANTIC_VERSION_MINOR_TUPLE < (2, 7):
self.deprecated = deprecated
else:
kwargs["deprecated"] = deprecated
kwargs["deprecated"] = deprecated
kwargs["regex"] = pattern or regex
kwargs.update(**current_json_schema_extra)
use_kwargs = {k: v for k, v in kwargs.items() if v is not _Unset}
@ -541,10 +537,7 @@ class Body(FieldInfo): # type: ignore[misc]
stacklevel=4,
)
current_json_schema_extra = json_schema_extra or extra
if PYDANTIC_VERSION_MINOR_TUPLE < (2, 7):
self.deprecated = deprecated
else:
kwargs["deprecated"] = deprecated
kwargs["deprecated"] = deprecated
kwargs["regex"] = pattern or regex
kwargs.update(**current_json_schema_extra)

Loading…
Cancel
Save