From 88f56bb64d652020f1d67fa98663d99a41fd905e Mon Sep 17 00:00:00 2001 From: Correl Date: Thu, 18 Mar 2021 16:37:34 -0400 Subject: [PATCH] Annotate formatter and deserializer types --- pyproject.toml | 1 + tornado_openapi3/handler.py | 6 ++++-- tornado_openapi3/types.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tornado_openapi3/types.py diff --git a/pyproject.toml b/pyproject.toml index ba4a6cb..7db8875 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ python = "^3.7" tornado = "^4 || ^5 || ^6" openapi-core = "^0.13.4" ietfparse = "^1.7.0" +typing-extensions = "^3.7.4" [tool.poetry.dev-dependencies] black = { version = "*", allow-prereleases = true } diff --git a/tornado_openapi3/handler.py b/tornado_openapi3/handler.py index e9be265..d29ebb0 100644 --- a/tornado_openapi3/handler.py +++ b/tornado_openapi3/handler.py @@ -1,5 +1,6 @@ import asyncio import logging +import typing from openapi_core import create_spec # 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 from tornado_openapi3.requests import RequestValidator +from tornado_openapi3.types import Deserializer, Formatter logger = logging.getLogger(__name__) @@ -62,7 +64,7 @@ class OpenAPIRequestHandler(tornado.web.RequestHandler): return create_spec(self.spec_dict, validate_spec=False) @property - def custom_formatters(self) -> dict: + def custom_formatters(self) -> typing.Mapping[str, Formatter]: """A dictionary mapping value formats to formatter objects. A formatter object must provide: @@ -73,7 +75,7 @@ class OpenAPIRequestHandler(tornado.web.RequestHandler): return dict() @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. If your endpoints make use of content types beyond ``application/json``, diff --git a/tornado_openapi3/types.py b/tornado_openapi3/types.py new file mode 100644 index 0000000..b1dd147 --- /dev/null +++ b/tornado_openapi3/types.py @@ -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 + ...