From 297cda8e7c350f554324aa79daaa66a7013ce6d2 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 31 Aug 2020 19:55:09 +0100 Subject: [PATCH 1/3] reset the `__eq__` and `__hash__` to allow WebSockets to be added to sets --- starlette/requests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/starlette/requests.py b/starlette/requests.py index 54ed8611e..97e8225c9 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -74,6 +74,9 @@ def __iter__(self) -> typing.Iterator[str]: def __len__(self) -> int: return len(self.scope) + __eq__ = object.__eq__ + __hash__ = object.__hash__ + @property def app(self) -> typing.Any: return self.scope["app"] From f276f4318ad921385351f9ab73902e6d967b43be Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 28 Jun 2021 12:55:26 +0100 Subject: [PATCH 2/3] test WebSocket.__eq__ and WebSocket.__hash__ --- tests/test_websockets.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_websockets.py b/tests/test_websockets.py index 63ecd050a..f5d52215b 100644 --- a/tests/test_websockets.py +++ b/tests/test_websockets.py @@ -368,3 +368,13 @@ async def mock_send(message): assert websocket["type"] == "websocket" assert dict(websocket) == {"type": "websocket", "path": "/abc/", "headers": []} assert len(websocket) == 3 + + # check __eq__ and __hash__ + assert websocket != WebSocket( + {"type": "websocket", "path": "/abc/", "headers": []}, + receive=mock_receive, + send=mock_send, + ) + assert websocket == websocket + assert websocket in {websocket} + assert {websocket} == {websocket} From c56d1b01c17a85e878b4f33789170a2b6d216599 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 28 Jun 2021 12:56:34 +0100 Subject: [PATCH 3/3] Update starlette/requests.py --- starlette/requests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/starlette/requests.py b/starlette/requests.py index 97e8225c9..f88021645 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -74,6 +74,9 @@ def __iter__(self) -> typing.Iterator[str]: def __len__(self) -> int: return len(self.scope) + # Don't use the `abc.Mapping.__eq__` implementation. + # Connection instances should never be considered equal + # unless `self is other`. __eq__ = object.__eq__ __hash__ = object.__hash__