Skip to content

Commit d7e4e61

Browse files
[PR #10172/e45c3b8e backport][3.12] Ensure Response is True even when map is empty (#10177)
**This is a backport of PR #10172 as merged into master (e45c3b8).** Fixes #10119 Technically a breaking change, but I can't imagine anyone depending on this. Co-authored-by: Robin <[email protected]>
1 parent 86e2140 commit d7e4e61

File tree

4 files changed

+6
-0
lines changed

4 files changed

+6
-0
lines changed

CHANGES/10119.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Response is now always True, instead of using MutableMapping behaviour (False when map is empty)

aiohttp/web_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ def __hash__(self) -> int:
609609
def __eq__(self, other: object) -> bool:
610610
return self is other
611611

612+
def __bool__(self) -> bool:
613+
return True
614+
612615

613616
class Response(StreamResponse):
614617

tests/test_web_request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def test_match_info() -> None:
311311
def test_request_is_mutable_mapping() -> None:
312312
req = make_mocked_request("GET", "/")
313313
assert isinstance(req, MutableMapping)
314+
assert req # even when the MutableMapping is empty, request should always be True
314315
req["key"] = "value"
315316
assert "value" == req["key"]
316317

tests/test_web_response.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def test_stream_response_eq() -> None:
106106
def test_stream_response_is_mutable_mapping() -> None:
107107
resp = StreamResponse()
108108
assert isinstance(resp, collections.abc.MutableMapping)
109+
assert resp # even when the MutableMapping is empty, response should always be True
109110
resp["key"] = "value"
110111
assert "value" == resp["key"]
111112

0 commit comments

Comments
 (0)