48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from typing import Any, Callable, Iterable, Type, TypeVar
|
|
|
|
from starlette.datastructures import State as State # noqa: F401
|
|
from starlette.datastructures import UploadFile as StarletteUploadFile
|
|
|
|
|
|
class UploadFile(StarletteUploadFile):
|
|
@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
|
|
|
|
|
|
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
|