Skip to content

False-positive PLW1641 when setting __hash__ = <ParentClass>.__hash__ #10557

@janosh

Description

@janosh

with v0.3.4, ruff check t.py --select PLW1641 on

class A:
    def __eq__(self, other):
        return False

    def __hash__(self):
        return 7

class B(A):
    def __eq__(self, other):
        return True

    __hash__ = A.__hash__

issues

PLW1641 Object does not implement `__hash__` method
   |
12 | class B(A):
   |       ^ PLW1641
13 |     def __eq__(self, other):
14 |         return True

even though the docs explicitly recommend this:

If a class that overrides eq() needs to retain the implementation of hash() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions