Skip to content

reset the __eq__ and __hash__ of HTTPConnection to allow WebSockets to be added to …#1039

Merged
graingert merged 3 commits intoKludex:masterfrom
graingert:patch-1
Jun 28, 2021
Merged

reset the __eq__ and __hash__ of HTTPConnection to allow WebSockets to be added to …#1039
graingert merged 3 commits intoKludex:masterfrom
graingert:patch-1

Conversation

@graingert
Copy link
Contributor

@graingert graingert changed the title reset the __eq__ and __hash__ to allow WebSockets to be added to … reset the __eq__ and __hash__ of HTTPConnection to allow WebSockets to be added to … Aug 31, 2020
@JayH5 JayH5 added the websocket WebSocket-related label Sep 11, 2020
@graingert
Copy link
Contributor Author

@JayH5 is this a good change?

@lovelydinosaur
Copy link
Contributor

Could you explain the motivation a bit more clearly here?

@graingert
Copy link
Contributor Author

@tomchristie for a number of reasons it's really useful to be able to put connections in a set. Especially websocket pubsub, see fastapi/fastapi#1991

I can see no use for comparing non-identical connections for equality

@graingert graingert requested a review from lovelydinosaur June 26, 2021 21:24
return len(self.scope)

__eq__ = object.__eq__
__hash__ = object.__hash__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the hash method on here already inherit object.__hash__? What am I missing?

Copy link
Contributor Author

@graingert graingert Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping overrides __eq__ which disables the default __hash__

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping overrides eq

Ah gotcha, thanks.
Does that mean we actually only need to override __eq__ then?

What do we think to something like the following?...

    # Don't use the `abc.Mapping.__eq__` implementation.
    # Connection instances should never be considered equal
    # unless `self is other`.
    __eq__ = object.__eq__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to re-enable __hash__ but I'll double check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomchristie yep you need both:

from __future__ import annotations

import collections.abc
import pytest

def test_default():
    class HTTPConnection(collections.abc.Mapping):
        def __getitem__(self, *args, **kwargs):
            return None

        def __iter__(self, *args, **kwargs):
            return self

        def __next__(self, *args, **kwargs):
            raise StopIteration

        def __len__(self, *args, **kwargs):
            return 0


    assert HTTPConnection() == HTTPConnection()
    with pytest.raises(TypeError, match=r"unhashable type: 'HTTPConnection'"):
        connections = {HTTPConnection()}


def test_eq():
    class HTTPConnection(collections.abc.Mapping):

        __eq__ = object.__eq__

        def __getitem__(self, *args, **kwargs):
            return None

        def __iter__(self, *args, **kwargs):
            return self

        def __next__(self, *args, **kwargs):
            raise StopIteration

        def __len__(self, *args, **kwargs):
            return 0


    assert HTTPConnection() != HTTPConnection()
    h = HTTPConnection()
    assert h == h

    with pytest.raises(TypeError, match=r"unhashable type: 'HTTPConnection'"):
        connections = {HTTPConnection()}



def test_eq_and_hash():
    class HTTPConnection(collections.abc.Mapping):

        __eq__ = object.__eq__
        __hash__ = object.__hash__

        def __getitem__(self, *args, **kwargs):
            return None

        def __iter__(self, *args, **kwargs):
            return self

        def __next__(self, *args, **kwargs):
            raise StopIteration

        def __len__(self, *args, **kwargs):
            return 0


    assert HTTPConnection() != HTTPConnection()
    h = HTTPConnection()
    assert h == h

    assert {HTTPConnection()} != {HTTPConnection()}
    assert h in {h}
    assert {h} == {h}

Copy link
Contributor

@lovelydinosaur lovelydinosaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, fair enough. 😄

@graingert graingert requested a review from lovelydinosaur June 28, 2021 11:56
)
assert websocket == websocket
assert websocket in {websocket}
assert {websocket} == {websocket}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's all neat enough. 👍

@graingert graingert merged commit 906e907 into Kludex:master Jun 28, 2021
@graingert graingert deleted the patch-1 branch June 28, 2021 12:02
@graingert graingert mentioned this pull request Jul 4, 2021
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

websocket WebSocket-related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants