Annotate formatter and deserializer types

This commit is contained in:
Correl Roush 2021-03-18 16:37:34 -04:00
parent 5fe5b75285
commit 88f56bb64d
3 changed files with 19 additions and 2 deletions

View file

@ -16,6 +16,7 @@ python = "^3.7"
tornado = "^4 || ^5 || ^6" tornado = "^4 || ^5 || ^6"
openapi-core = "^0.13.4" openapi-core = "^0.13.4"
ietfparse = "^1.7.0" ietfparse = "^1.7.0"
typing-extensions = "^3.7.4"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
black = { version = "*", allow-prereleases = true } black = { version = "*", allow-prereleases = true }

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import logging import logging
import typing
from openapi_core import create_spec # type: ignore from openapi_core import create_spec # type: ignore
from openapi_core.casting.schemas.exceptions import CastError # type: ignore from openapi_core.casting.schemas.exceptions import CastError # type: ignore
@ -24,6 +25,7 @@ from openapi_core.validation.exceptions import InvalidSecurity # type: ignore
import tornado.web # type: ignore import tornado.web # type: ignore
from tornado_openapi3.requests import RequestValidator from tornado_openapi3.requests import RequestValidator
from tornado_openapi3.types import Deserializer, Formatter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -62,7 +64,7 @@ class OpenAPIRequestHandler(tornado.web.RequestHandler):
return create_spec(self.spec_dict, validate_spec=False) return create_spec(self.spec_dict, validate_spec=False)
@property @property
def custom_formatters(self) -> dict: def custom_formatters(self) -> typing.Mapping[str, Formatter]:
"""A dictionary mapping value formats to formatter objects. """A dictionary mapping value formats to formatter objects.
A formatter object must provide: A formatter object must provide:
@ -73,7 +75,7 @@ class OpenAPIRequestHandler(tornado.web.RequestHandler):
return dict() return dict()
@property @property
def custom_media_type_deserializers(self) -> dict: def custom_media_type_deserializers(self) -> typing.Mapping[str, Deserializer]:
"""A dictionary mapping media types to deserializing functions. """A dictionary mapping media types to deserializing functions.
If your endpoints make use of content types beyond ``application/json``, If your endpoints make use of content types beyond ``application/json``,

14
tornado_openapi3/types.py Normal file
View file

@ -0,0 +1,14 @@
import typing
import typing_extensions
Deserializer = typing.Callable[[typing.Union[bytes, str]], typing.Any]
class Formatter(typing_extensions.Protocol):
"""A type representing an OpenAPI formatter."""
def validate(self, value: str) -> bool: # pragma: no cover
...
def unmarshal(self, value: str) -> typing.Any: # pragma: no cover
...