Skip to content

StreamResponse instances are all equal #3100

Closed
@NotSqrt

Description

Long story short

Since #2494 , StreamResponse inherits collections.MutableMapping.__eq__, which makes them basically all equal.
The implementation in Mapping looks like return dict(self.items()) == dict(other.items()).

This is especially the case for WebSocketResponse : when following https://aiohttp.readthedocs.io/en/stable/faq.html#how-do-i-programmatically-close-a-websocket-server-side, if you use a list instead of a set, it's impossible to remove the correct websocket.

Expected behaviour

>>> from aiohttp.web_ws import WebSocketResponse
>>> r1 = WebSocketResponse()
>>> r2 = WebSocketResponse()
>>> r1 == r2
False
>>> id(r1) == id(r2)
False
>>> r1 is r2
False
>>> hash(r1) == hash(r2)
False

As a rule, a == b implies hash(a) == hash(b). But it's now broken.

Actual behaviour

Since v3.0:

>>> r1 == r2
True
>>> id(r1) == id(r2)
False
>>> r1 is r2
False
>>> hash(r1) == hash(r2)
False

Steps to reproduce

Described above

Your environment

  • aiohttp >= 3.0

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions