Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseParser should receive the callbacks dict #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Replace Mapping with a Base TypedDict
  • Loading branch information
eltbus committed Feb 17, 2024
commit e0ad7509027b7c39066cc19e706fa7ba7a05be77
20 changes: 10 additions & 10 deletions multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
from .exceptions import FileError, FormParserError, MultipartParseError, QuerystringParseError

if TYPE_CHECKING: # pragma: no cover
from typing import Callable, Mapping, Protocol, TypedDict
from typing import Callable, Protocol, TypedDict

class QuerystringCallbacks(TypedDict, total=False):
class BaseCallbacks(TypedDict, total=False):
on_end: Callable[[], None]

class QuerystringCallbacks(BaseCallbacks, total=False):
on_field_start: Callable[[], None]
on_field_name: Callable[[bytes, int, int], None]
on_field_data: Callable[[bytes, int, int], None]
on_field_end: Callable[[], None]
on_end: Callable[[], None]

class OctetStreamCallbacks(TypedDict, total=False):
class OctetStreamCallbacks(BaseCallbacks, total=False):
on_start: Callable[[], None]
on_data: Callable[[bytes, int, int], None]
on_end: Callable[[], None]

class MultipartCallbacks(TypedDict, total=False):
class MultipartCallbacks(BaseCallbacks, total=False):
on_part_begin: Callable[[], None]
on_part_data: Callable[[bytes, int, int], None]
on_part_end: Callable[[], None]
Expand All @@ -39,7 +40,6 @@ class MultipartCallbacks(TypedDict, total=False):
on_header_value: Callable[[bytes, int, int], None]
on_header_end: Callable[[], None]
on_headers_finished: Callable[[], None]
on_end: Callable[[], None]

class FormParserConfig(TypedDict):
UPLOAD_DIR: str | None
Expand Down Expand Up @@ -608,7 +608,7 @@ class BaseParser:
performance.
"""

def __init__(self, callbacks: Mapping) -> None:
def __init__(self, callbacks: BaseCallbacks) -> None:
self.logger = logging.getLogger(__name__)
self.callbacks = callbacks

Expand Down Expand Up @@ -654,9 +654,9 @@ def set_callback(self, name: str, new_func: Callable[..., Any] | None) -> None:
exist).
"""
if new_func is None:
self.callbacks.pop("on_" + name, None) # TODO: MutableMapping breaks compatibility with TypedDict
self.callbacks.pop("on_" + name, None)
else:
self.callbacks["on_" + name] = new_func # TODO: MutableMapping breaks compatibility with TypedDict
self.callbacks["on_" + name] = new_func

def close(self):
pass # pragma: no cover
Expand Down