Skip to content

Commit

Permalink
Refactor mixins to avoid having to call __init__
Browse files Browse the repository at this point in the history
The downside is we have to drop `__slots__` but the trade-off
might be worth it.
  • Loading branch information
bdraco committed Nov 24, 2024
1 parent 5f5729d commit 4ac800c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
23 changes: 6 additions & 17 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,15 +719,12 @@ def ceil_timeout(


class HeadersMixin:
__slots__ = ("_content_type", "_content_dict", "_stored_content_type")
"""Mixin for handling headers."""

_headers: MultiMapping[str]

def __init__(self) -> None:
super().__init__()
self._content_type: Optional[str] = None
self._content_dict: Optional[Dict[str, str]] = None
self._stored_content_type: Union[str, None, _SENTINEL] = sentinel
_content_type: Optional[str] = None
_content_dict: Optional[Dict[str, str]] = None
_stored_content_type: Union[str, None, _SENTINEL] = sentinel

def _parse_content_type(self, raw: Optional[str]) -> None:
self._stored_content_type = raw
Expand Down Expand Up @@ -921,17 +918,9 @@ def __repr__(self) -> str:


class CookieMixin:
# The `_cookies` slots is not defined here because non-empty slots cannot
# be combined with an Exception base class, as is done in HTTPException.
# CookieMixin subclasses with slots should define the `_cookies`
# slot themselves.
__slots__ = ()
"""Mixin for handling cookies."""

def __init__(self) -> None:
super().__init__()
# Mypy doesn't like that _cookies isn't in __slots__.
# See the comment on this class's __slots__ for why this is OK.
self._cookies: Optional[SimpleCookie] = None # type: ignore[misc]
_cookies: Optional[SimpleCookie] = None

@property
def cookies(self) -> SimpleCookie:
Expand Down
1 change: 0 additions & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class StreamResponse(BaseClass, HeadersMixin, CookieMixin):
"_headers",
"_status",
"_reason",
"_cookies",
"__weakref__",
)

Expand Down

0 comments on commit 4ac800c

Please sign in to comment.