From 4ac800c7ffdea8a90ad418de5117b7a805e887dc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 24 Nov 2024 16:26:11 -0600 Subject: [PATCH] Refactor mixins to avoid having to call __init__ The downside is we have to drop `__slots__` but the trade-off might be worth it. --- aiohttp/helpers.py | 23 ++++++----------------- aiohttp/web_response.py | 1 - 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 9eddd829916..f27c3da0ea1 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -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 @@ -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: diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index 9ea4bb3809a..85b3e76ff8e 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -93,7 +93,6 @@ class StreamResponse(BaseClass, HeadersMixin, CookieMixin): "_headers", "_status", "_reason", - "_cookies", "__weakref__", )