From 46dd90dec8d4b8a79e23e6f6174188c350c60e18 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 11:13:51 +0100 Subject: [PATCH 1/9] Remove overlapping slots, fix broken slots inheritance --- aiohttp/helpers.py | 8 +++++++- aiohttp/web_protocol.py | 1 - aiohttp/web_response.py | 1 + tests/test_helpers.py | 16 +++++++++++----- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index b7a033795fb..e9d0aef8278 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -831,9 +831,15 @@ 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__ = () + def __init__(self) -> None: super().__init__() - self._cookies: SimpleCookie[str] = SimpleCookie() + self._cookies: SimpleCookie[str] = SimpleCookie() # type: ignore @property def cookies(self) -> "SimpleCookie[str]": diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index 03d0c274719..6d662949d8f 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -170,7 +170,6 @@ class RequestHandler(BaseProtocol): "_upgrade", "_payload_parser", "_request_parser", - "_reading_paused", "logger", "access_log", "access_logger", diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index 42c3fac51ae..6ac280ebefa 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -92,6 +92,7 @@ class StreamResponse(BaseClass, HeadersMixin, CookieMixin): "_headers", "_status", "_reason", + "_cookies", "__weakref__", ) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 9cfebc7b3a6..26afe82ef7e 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -850,8 +850,14 @@ def test_is_expected_content_type_non_json_not_match(): ) +# It's necessary to subclass CookieMixin before using it. +# See the comments on its __slots__. +class CookieImplementation(helpers.CookieMixin): + pass + + def test_cookies_mixin(): - sut = helpers.CookieMixin() + sut = CookieImplementation() assert sut.cookies == {} assert str(sut.cookies) == "" @@ -880,7 +886,7 @@ def test_cookies_mixin(): def test_cookies_mixin_path(): - sut = helpers.CookieMixin() + sut = CookieImplementation() assert sut.cookies == {} @@ -914,7 +920,7 @@ def test_cookies_mixin_path(): def test_sutonse_cookie__issue_del_cookie(): - sut = helpers.CookieMixin() + sut = CookieImplementation() assert sut.cookies == {} assert str(sut.cookies) == "" @@ -928,7 +934,7 @@ def test_sutonse_cookie__issue_del_cookie(): def test_cookie_set_after_del(): - sut = helpers.CookieMixin() + sut = CookieImplementation() sut.del_cookie("name") sut.set_cookie("name", "val") @@ -938,7 +944,7 @@ def test_cookie_set_after_del(): def test_populate_with_cookies(): - cookies_mixin = helpers.CookieMixin() + cookies_mixin = CookieImplementation() cookies_mixin.set_cookie("name", "value") headers = CIMultiDict() From 5c21d0af4a9c9f92aaaf19f2b923a3d3cd706f12 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 11:57:14 +0100 Subject: [PATCH 2/9] added "CHANGES" entry --- CHANGES/6547.bugfix | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 CHANGES/6547.bugfix diff --git a/CHANGES/6547.bugfix b/CHANGES/6547.bugfix new file mode 100644 index 00000000000..7b837d9b696 --- /dev/null +++ b/CHANGES/6547.bugfix @@ -0,0 +1,2 @@ +Remove overlapping slots in :py:class:`~aiohttp.web_protocol.RequestHandler`, +fix broken slots inheritance in :py:class:`~aiohttp.web_response.StreamResponse`. From 9de8d24f7cfe2b3f4877c540365ae5c5052ce7ae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Jan 2022 10:58:03 +0000 Subject: [PATCH 3/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES/6547.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/6547.bugfix b/CHANGES/6547.bugfix index 7b837d9b696..3360e657bd0 100644 --- a/CHANGES/6547.bugfix +++ b/CHANGES/6547.bugfix @@ -1,2 +1,2 @@ -Remove overlapping slots in :py:class:`~aiohttp.web_protocol.RequestHandler`, +Remove overlapping slots in :py:class:`~aiohttp.web_protocol.RequestHandler`, fix broken slots inheritance in :py:class:`~aiohttp.web_response.StreamResponse`. From 2cbd247d74d3088bf9c766941e88b8cbd81bde1c Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 12:14:50 +0100 Subject: [PATCH 4/9] fix refs in CHANGES --- CHANGES/6547.bugfix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES/6547.bugfix b/CHANGES/6547.bugfix index 3360e657bd0..8512c4067f6 100644 --- a/CHANGES/6547.bugfix +++ b/CHANGES/6547.bugfix @@ -1,2 +1,2 @@ -Remove overlapping slots in :py:class:`~aiohttp.web_protocol.RequestHandler`, -fix broken slots inheritance in :py:class:`~aiohttp.web_response.StreamResponse`. +Remove overlapping slots in ``RequestHandler``, +fix broken slots inheritance in :py:class:`~aiohttp.web.StreamResponse`. From 7261bbaee32925e1094c9b49c49db756a1efe140 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 12:16:02 +0100 Subject: [PATCH 5/9] added name to contributors --- CONTRIBUTORS.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d8be9a57a0f..a9f82da88ef 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -46,6 +46,7 @@ Anes Abismail Antoine Pietri Anton Kasyanov Anton Zhdan-Pushkin +Arie Bovenberg Arseny Timoniq Artem Yushkovskiy Arthur Darcet From b160bf58e8435d4c55db9554d6bedbd3e8166e58 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 15:05:01 +0100 Subject: [PATCH 6/9] extra clarification on CookieMixin __slots__ ignore --- aiohttp/helpers.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index e9d0aef8278..febc49647c5 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -833,13 +833,15 @@ 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. + # CookieMixin subclasses with slots should define the `_cookies` + # slot themselves. __slots__ = () def __init__(self) -> None: super().__init__() - self._cookies: SimpleCookie[str] = SimpleCookie() # type: ignore + # 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: SimpleCookie[str] = SimpleCookie() # type: ignore[misc] @property def cookies(self) -> "SimpleCookie[str]": From a11401194debea158123de07c8efe82b9afd5cbe Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 15:07:13 +0100 Subject: [PATCH 7/9] add slotscheck to github CI workflow --- .github/workflows/ci.yml | 5 ++++- requirements/lint.txt | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f0b1621013..3f970180a30 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,9 +51,12 @@ jobs: python -m pip install . env: AIOHTTP_NO_EXTENSIONS: 1 - - name: Run linters + - name: Run mypy run: | make mypy + - name: Run slotscheck + run: | + slotscheck -m aiohttp - name: Install libenchant-dev run: | sudo apt install libenchant-dev diff --git a/requirements/lint.txt b/requirements/lint.txt index c4949763d70..0324cc84401 100644 --- a/requirements/lint.txt +++ b/requirements/lint.txt @@ -2,3 +2,4 @@ mypy==0.931; implementation_name=="cpython" pre-commit==2.17.0 pytest==6.2.5 +slotscheck==0.8.0 From c538389593c9eb61511d5ae64131d9be3ac5374b Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 15:50:45 +0100 Subject: [PATCH 8/9] use "python -m" in slotscheck run to ensure aiohttp.worker module can be imported --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f970180a30..28322c9b104 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: make mypy - name: Run slotscheck run: | - slotscheck -m aiohttp + python -m slotscheck -m aiohttp - name: Install libenchant-dev run: | sudo apt install libenchant-dev From 32ce39599fdd91c5a3f67af3c54858bd6659e041 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Sat, 22 Jan 2022 16:03:24 +0100 Subject: [PATCH 9/9] install base requirements for slotscheck in CI --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28322c9b104..8599563b60b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,10 @@ jobs: make mypy - name: Run slotscheck run: | - python -m slotscheck -m aiohttp + # Some extra requirements are needed to ensure all modules + # can be scanned by slotscheck. + pip install -r requirements/base.txt -c requirements/constraints.txt + slotscheck -v -m aiohttp - name: Install libenchant-dev run: | sudo apt install libenchant-dev