Skip to content

Commit

Permalink
Replace Serializer and MediaHandler with protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
copalco committed Oct 17, 2023
1 parent 43b98ec commit eea7818
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
5 changes: 3 additions & 2 deletions falcon/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

"""Falcon App class."""

import typing
from functools import wraps
from inspect import iscoroutinefunction
import pathlib
Expand All @@ -36,11 +36,12 @@
from falcon.response import Response
from falcon.response import ResponseOptions
import falcon.status_codes as status
from falcon.typing import ErrorHandler, ErrorSerializer, SinkPrefix
from falcon.util import deprecation
from falcon.util import misc
from falcon.util.misc import code_to_http_status

if typing.TYPE_CHECKING:
from falcon.typing import ErrorHandler, ErrorSerializer, SinkPrefix

# PERF(vytas): On Python 3.5+ (including cythonized modules),
# reference via module global is faster than going via self
Expand Down
3 changes: 1 addition & 2 deletions falcon/media/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from typing import IO, Optional, Union

from falcon.constants import MEDIA_JSON
from falcon.typing import Serializer


class BaseHandler(Serializer, metaclass=abc.ABCMeta):
class BaseHandler(metaclass=abc.ABCMeta):
"""Abstract Base Class for an internet media type handler."""

# NOTE(kgriffs): The following special methods are used to enable an
Expand Down
10 changes: 7 additions & 3 deletions falcon/media/handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from collections import UserDict
import functools

Expand All @@ -10,11 +11,14 @@
from falcon.media.multipart import MultipartFormHandler
from falcon.media.multipart import MultipartParseOptions
from falcon.media.urlencoded import URLEncodedFormHandler
from falcon.typing import MediaHandlers
from falcon.util import deprecation
from falcon.util import misc
from falcon.vendor import mimeparse

if typing.TYPE_CHECKING:
from typing import Mapping
from falcon.typing import Serializer


class MissingDependencyHandler:
"""Placeholder handler that always raises an error.
Expand All @@ -35,13 +39,13 @@ def _raise(self, *args, **kwargs):
serialize = deserialize = _raise


class Handlers(MediaHandlers, UserDict):
class Handlers(UserDict):
"""A :class:`dict`-like object that manages Internet media type handlers."""

def __init__(self, initial=None):
self._resolve = self._create_resolver()

handlers = initial or {
handlers: Mapping[str, Serializer] = initial or {
MEDIA_JSON: JSONHandler(),
MEDIA_MULTIPART: MultipartFormHandler(),
MEDIA_URLENCODED: URLEncodedFormHandler(),
Expand Down
25 changes: 14 additions & 11 deletions falcon/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@
from falcon.request import Request
from falcon.response import Response

Link = Dict[str, str]
from typing import Protocol

class Serializer(Protocol):
def serialize(
self,
media: MutableMapping[str, Union[str, int, None, Link]],
content_type: str,
) -> bytes:
...

class Serializer:
def serialize(
self, media: MutableMapping[str, Union[str, int, None, Link]], content_type: str
) -> bytes:
raise NotImplementedError()
class MediaHandlers(Protocol):
def _resolve(
self, media_type: str, default: str, raise_not_found: bool = False
) -> Tuple[Serializer, Optional[Callable], Optional[Callable]]:
...


class MediaHandlers:
def _resolve(
self, media_type: str, default: str, raise_not_found: bool = False
) -> Tuple[Serializer, Optional[Callable], Optional[Callable]]:
raise NotImplementedError()
Link = Dict[str, str]


# Error handlers
Expand Down
16 changes: 0 additions & 16 deletions tests/test_typing.py

This file was deleted.

0 comments on commit eea7818

Please sign in to comment.