From 7bd3ea614de7ddba1bc50296160455651cb7e09b Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sun, 11 Aug 2024 23:32:25 +0200 Subject: [PATCH] typing: avoid protocols for handlers --- falcon/http_error.py | 6 +++--- falcon/media/handlers.py | 12 ++++-------- falcon/response.py | 9 +++------ falcon/typing.py | 16 ---------------- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/falcon/http_error.py b/falcon/http_error.py index 7f6c36be8..03d51b9da 100644 --- a/falcon/http_error.py +++ b/falcon/http_error.py @@ -26,9 +26,9 @@ from falcon.util.deprecation import deprecated_args if TYPE_CHECKING: + from falcon.media import BaseHandler from falcon.typing import HeaderList from falcon.typing import Link - from falcon.typing import Serializer from falcon.typing import Status @@ -191,7 +191,7 @@ def to_dict( return obj - def to_json(self, handler: Optional[Serializer] = None) -> bytes: + def to_json(self, handler: Optional[BaseHandler] = None) -> bytes: """Return a JSON representation of the error. Args: @@ -241,6 +241,6 @@ def to_xml(self) -> bytes: # NOTE: initialized in falcon.media.json, that is always imported since Request/Response # are imported by falcon init. if TYPE_CHECKING: - _DEFAULT_JSON_HANDLER: Serializer + _DEFAULT_JSON_HANDLER: BaseHandler else: _DEFAULT_JSON_HANDLER = None diff --git a/falcon/media/handlers.py b/falcon/media/handlers.py index 36f38e128..235fd0f1c 100644 --- a/falcon/media/handlers.py +++ b/falcon/media/handlers.py @@ -1,12 +1,13 @@ from collections import UserDict import functools -import typing +from typing import Mapping from falcon import errors from falcon.constants import MEDIA_JSON from falcon.constants import MEDIA_MULTIPART from falcon.constants import MEDIA_URLENCODED from falcon.constants import PYPY +from falcon.media.base import BaseHandler from falcon.media.base import BinaryBaseHandlerWS from falcon.media.json import JSONHandler from falcon.media.multipart import MultipartFormHandler @@ -16,11 +17,6 @@ from falcon.util import misc from falcon.vendor import mimeparse -if typing.TYPE_CHECKING: # pragma: no cover - from typing import Mapping - - from falcon.typing import Serializer - class MissingDependencyHandler(BinaryBaseHandlerWS): """Placeholder handler that always raises an error. @@ -41,13 +37,13 @@ def _raise(self, *args, **kwargs): serialize = deserialize = _raise -class Handlers(UserDict): +class Handlers(UserDict[str, BaseHandler]): """A :class:`dict`-like object that manages Internet media type handlers.""" def __init__(self, initial=None): self._resolve = self._create_resolver() - handlers: Mapping[str, Serializer] = initial or { + handlers: Mapping[str, BaseHandler] = initial or { MEDIA_JSON: JSONHandler(), MEDIA_MULTIPART: MultipartFormHandler(), MEDIA_URLENCODED: URLEncodedFormHandler(), diff --git a/falcon/response.py b/falcon/response.py index 5d29c1c0c..e96f2ba2f 100644 --- a/falcon/response.py +++ b/falcon/response.py @@ -18,7 +18,7 @@ import functools import mimetypes -from typing import Optional, TYPE_CHECKING +from typing import Dict, Optional from falcon.constants import _DEFAULT_STATIC_MEDIA_TYPES from falcon.constants import _UNSET @@ -41,9 +41,6 @@ from falcon.util.uri import encode_check_escaped as uri_encode from falcon.util.uri import encode_value_check_escaped as uri_encode_value -if TYPE_CHECKING: - from falcon.typing import MediaHandlers - GMT_TIMEZONE = TimezoneGMT() _STREAM_LEN_REMOVED_MSG = ( @@ -1256,8 +1253,8 @@ class ResponseOptions: secure_cookies_by_default: bool default_media_type: Optional[str] - media_handlers: MediaHandlers - static_media_types: dict + media_handlers: Handlers + static_media_types: Dict[str, str] __slots__ = ( 'secure_cookies_by_default', diff --git a/falcon/typing.py b/falcon/typing.py index cb345d485..e7877616c 100644 --- a/falcon/typing.py +++ b/falcon/typing.py @@ -21,8 +21,6 @@ Callable, Dict, List, - MutableMapping, - Optional, Pattern, Tuple, TYPE_CHECKING, @@ -30,23 +28,9 @@ ) if TYPE_CHECKING: - from typing import Protocol - from falcon.request import Request from falcon.response import Response - class Serializer(Protocol): - def serialize( - self, - media: MutableMapping[str, Union[str, int, None, Link]], - content_type: str, - ) -> bytes: ... - - class MediaHandlers(Protocol): - def _resolve( - self, media_type: str, default: str, raise_not_found: bool = False - ) -> Tuple[Serializer, Optional[Callable], Optional[Callable]]: ... - Link = Dict[str, str]