mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-13 19:38:34 -04:00
* ✨ Add Default and DefaultPlaceholder data structures to handle defaults and overrides * ✨ Add utils to get values by priority handling DefaultPlaceholders * ✨ Add support for top-level parameters in FastAPI, APIRouter, include_router including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks * ♻️ Update openapi utils to handle DefaultPlaceholder for response_class * 📝 Update bigger-application example code to use top-level params and showcase them in APIRouter, FastAPI, include_router * 📝 Update docs for Bigger Applications, include diagrams, top-level params * 🔥 Simplify code and docs for callbacks as default_response_class is no longer required * 📝 Add docs for top-level dependencies, in FastAPI() * 📝 Add docs reference to top-level dependencies in docs for decorator * ✅ Update/increase tests for Bigger Applications including shared parameters * ✅ Add tests for top-level dependencies in FastAPI() * ✅ Add tests for internal DefaultPlaceholder * ✅ Update/increase tests for callbacks with top-level parameters * ✅ Add LOTS of tests covering branches and cases for shared parameters in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations * 🎨 Format/reorder parameters for consistency in FastAPI, APIRouter, include_router
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from typing import Any, Callable, Iterable, Type, TypeVar
|
|
|
|
from starlette.datastructures import UploadFile as StarletteUploadFile
|
|
|
|
|
|
class UploadFile(StarletteUploadFile):
|
|
@classmethod
|
|
def __get_validators__(cls: Type["UploadFile"]) -> Iterable[Callable]:
|
|
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
|
|
|
|
|
|
class DefaultPlaceholder:
|
|
"""
|
|
You shouldn't use this class directly.
|
|
|
|
It's used internally to recognize when a default value has been overwritten, even
|
|
if the overriden default value was truthy.
|
|
"""
|
|
|
|
def __init__(self, value: Any):
|
|
self.value = value
|
|
|
|
def __bool__(self) -> bool:
|
|
return bool(self.value)
|
|
|
|
def __eq__(self, o: object) -> bool:
|
|
return isinstance(o, DefaultPlaceholder) and o.value == self.value
|
|
|
|
|
|
DefaultType = TypeVar("DefaultType")
|
|
|
|
|
|
def Default(value: DefaultType) -> DefaultType:
|
|
"""
|
|
You shouldn't use this function directly.
|
|
|
|
It's used internally to recognize when a default value has been overwritten, even
|
|
if the overriden default value was truthy.
|
|
"""
|
|
return DefaultPlaceholder(value) # type: ignore
|