Debian Codegraph @

ninja/files.py

raw symbols SCIP
1 from typing import Any, Callable, Dict
2
3 from django.core.files.uploadedfile import UploadedFile as DjangoUploadedFile
4 from pydantic_core import core_schema
5
6 __all__ = ["UploadedFile"]
7
8
9 class UploadedFile(DjangoUploadedFile):
10 @classmethod
11 def __get_pydantic_json_schema__(
12 cls, core_schema: Any, handler: Callable[..., Any]
13 ) -> Dict:
14 # calling handler(core_schema) here raises an exception
15 json_schema: Dict[str, str] = {}
16 json_schema.update(type="string", format="binary")
17 return json_schema
18
19 @classmethod
20 def _validate(cls, v: Any, _: Any) -> Any:
21 if not isinstance(v, DjangoUploadedFile):
22 raise ValueError(f"Expected UploadFile, received: {type(v)}")
23 return v
24
25 @classmethod
26 def __get_pydantic_core_schema__(
27 cls, source: Any, handler: Callable[..., Any]
28 ) -> Any:
29 return core_schema.with_info_plain_validator_function(cls._validate)