`__hash__` with custom `__eq__`

Open
#1,462 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start with the minimal Python example and the PLW1641 diagnostic shown in the issue, then compare the attrs hashing documentation with Python's custom __eq__ and __hash__ rules. Done means establishing whether the behavior is expected and identifying a clear, agreed resolution or documentation change.

Written by the indexing model from the issue text.

Description

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
  |
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.