Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Jun 28, 2021

Conversation

graingert
Copy link
Member

@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
Member Author

@JayH5 is this a good change?

@tomchristie
Copy link
Member

Could you explain the motivation a bit more clearly here?

@graingert
Copy link
Member Author

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

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

@@ -73,6 +73,9 @@ def __iter__(self) -> typing.Iterator[str]:
def __len__(self) -> int:
return len(self.scope)

__eq__ = object.__eq__
__hash__ = object.__hash__
Copy link
Member

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
Member 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
Member

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
Member 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
Member 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
Member

@tomchristie tomchristie 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. 😄

)
assert websocket == websocket
assert websocket in {websocket}
assert {websocket} == {websocket}
Copy link
Member

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 encode: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.

None yet

3 participants