Skip to content

Commit

Permalink
reset the __eq__ and __hash__ of HTTPConnection to allow WebSocke…
Browse files Browse the repository at this point in the history
…ts to be added to … (#1039)
  • Loading branch information
graingert committed Jun 28, 2021
1 parent 0ef4418 commit 906e907
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions starlette/requests.py
Expand Up @@ -74,6 +74,12 @@ 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__

@property
def app(self) -> typing.Any:
return self.scope["app"]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_websockets.py
Expand Up @@ -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}

0 comments on commit 906e907

Please sign in to comment.