mirror of
https://github.com/fastapi/fastapi.git
synced 2026-02-27 04:06:42 -05:00
* Make compatible with pydantic v1 * Remove unused import * Remove unused ignores * Update pydantic version * Fix minor formatting issue * ⏪ Revert removing iterate_in_threadpool * ✨ Add backwards compatibility with Pydantic 0.32.2 with deprecation warnings * ✅ Update tests to not break when using Pydantic < 1.0.0 * 📝 Update docs for Pydantic version 1.0.0 * 📌 Update Pydantic range version to support from 0.32.2 * 🎨 Format test imports * ✨ Add support for Pydantic < 1.2 for populate_validators * ✨ Add backwards compatibility for Pydantic < 1.2.0 with required fields * 📌 Relax requirement for Pydantic to < 2.0.0 🎉 🚀 * 💚 Update pragma coverage for older Pydantic versions
330 lines
7.5 KiB
Python
330 lines
7.5 KiB
Python
from enum import Enum
|
|
from typing import Any, Callable, Sequence
|
|
|
|
try:
|
|
from pydantic.fields import FieldInfo
|
|
except ImportError: # pragma: nocover
|
|
# TODO: remove when removing support for Pydantic < 1.0.0
|
|
from pydantic import Schema as FieldInfo # type: ignore
|
|
|
|
|
|
class ParamTypes(Enum):
|
|
query = "query"
|
|
header = "header"
|
|
path = "path"
|
|
cookie = "cookie"
|
|
|
|
|
|
class Param(FieldInfo):
|
|
in_: ParamTypes
|
|
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
deprecated: bool = None,
|
|
**extra: Any,
|
|
):
|
|
self.deprecated = deprecated
|
|
super().__init__(
|
|
default,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Path(Param):
|
|
in_ = ParamTypes.path
|
|
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
deprecated: bool = None,
|
|
**extra: Any,
|
|
):
|
|
self.in_ = self.in_
|
|
super().__init__(
|
|
...,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
deprecated=deprecated,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Query(Param):
|
|
in_ = ParamTypes.query
|
|
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
deprecated: bool = None,
|
|
**extra: Any,
|
|
):
|
|
super().__init__(
|
|
default,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
deprecated=deprecated,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Header(Param):
|
|
in_ = ParamTypes.header
|
|
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
alias: str = None,
|
|
convert_underscores: bool = True,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
deprecated: bool = None,
|
|
**extra: Any,
|
|
):
|
|
self.convert_underscores = convert_underscores
|
|
super().__init__(
|
|
default,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
deprecated=deprecated,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Cookie(Param):
|
|
in_ = ParamTypes.cookie
|
|
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
deprecated: bool = None,
|
|
**extra: Any,
|
|
):
|
|
super().__init__(
|
|
default,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
deprecated=deprecated,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Body(FieldInfo):
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
embed: bool = False,
|
|
media_type: str = "application/json",
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
**extra: Any,
|
|
):
|
|
self.embed = embed
|
|
self.media_type = media_type
|
|
super().__init__(
|
|
default,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Form(Body):
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
media_type: str = "application/x-www-form-urlencoded",
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
**extra: Any,
|
|
):
|
|
super().__init__(
|
|
default,
|
|
embed=True,
|
|
media_type=media_type,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class File(Form):
|
|
def __init__(
|
|
self,
|
|
default: Any,
|
|
*,
|
|
media_type: str = "multipart/form-data",
|
|
alias: str = None,
|
|
title: str = None,
|
|
description: str = None,
|
|
gt: float = None,
|
|
ge: float = None,
|
|
lt: float = None,
|
|
le: float = None,
|
|
min_length: int = None,
|
|
max_length: int = None,
|
|
regex: str = None,
|
|
**extra: Any,
|
|
):
|
|
super().__init__(
|
|
default,
|
|
media_type=media_type,
|
|
alias=alias,
|
|
title=title,
|
|
description=description,
|
|
gt=gt,
|
|
ge=ge,
|
|
lt=lt,
|
|
le=le,
|
|
min_length=min_length,
|
|
max_length=max_length,
|
|
regex=regex,
|
|
**extra,
|
|
)
|
|
|
|
|
|
class Depends:
|
|
def __init__(self, dependency: Callable = None, *, use_cache: bool = True):
|
|
self.dependency = dependency
|
|
self.use_cache = use_cache
|
|
|
|
|
|
class Security(Depends):
|
|
def __init__(
|
|
self,
|
|
dependency: Callable = None,
|
|
*,
|
|
scopes: Sequence[str] = None,
|
|
use_cache: bool = True,
|
|
):
|
|
super().__init__(dependency=dependency, use_cache=use_cache)
|
|
self.scopes = scopes or []
|