Skip to content

__hash__ with custom __eq__ #1462

@AdrianSosic

Description

@AdrianSosic

Hi, I know that hashing is a complicated topic and I've seen the sections in the docs / API reference + the resources linked therein. However, I'm still somewhat unclear about what is expected to happen in the situation described at the end.

Is this:

  • expected?
  • an attrs issue?
  • a ruff issue?

And regardless of which it is, how to properly resolve it? The attrs docs say the following:

Image

My understanding is that we are facing the highlighted case here, but I don't see the fallback to id-based hashing (regardless of whether I explicitly declare eq=True/False in the decorator) unless I add the __hash__ = object.__hash__ line.

Issue

from attrs import define


@define(frozen=True)
class C:
    x: int

    def __eq__(self, value):
        return self.x in (0, value)

Gives the following ruff error:

PLW1641 Object does not implement `__hash__` method
 --> dbg.py:5:7
  |
4 | @define(frozen=True)
5 | class C:
  |       ^
6 |     x: int
  |

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions