Skip to content

Commit

Permalink
typing: avoid protocols for handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Aug 11, 2024
1 parent af41e56 commit 7bd3ea6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 33 deletions.
6 changes: 3 additions & 3 deletions falcon/http_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
12 changes: 4 additions & 8 deletions falcon/media/handlers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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(),
Expand Down
9 changes: 3 additions & 6 deletions falcon/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = (
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 0 additions & 16 deletions falcon/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,16 @@
Callable,
Dict,
List,
MutableMapping,
Optional,
Pattern,
Tuple,
TYPE_CHECKING,
Union,
)

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]

Expand Down

0 comments on commit 7bd3ea6

Please sign in to comment.