eq-without-hash / W1641ΒΆ

Message emitted:

Implementing __eq__ without also implementing __hash__

Description:

Used when a class implements __eq__ but not __hash__. Objects get None as their default __hash__ implementation if they also implement __eq__.

Problematic code:

class Fruit:  # [eq-without-hash]
    def __init__(self) -> None:
        self.name = "apple"

    def __eq__(self, other: object) -> bool:
        return isinstance(other, Fruit) and other.name == self.name

Correct code:

class Fruit:
    def __init__(self) -> None:
        self.name = "apple"

    def __eq__(self, other: object) -> bool:
        return isinstance(other, Fruit) and other.name == self.name

    def __hash__(self) -> int:
        return hash(self.name)

Configuration file:

[MAIN]
load-plugins=pylint.extensions.eq_without_hash,

Note

This message is emitted by the optional 'eq-without-hash' checker, which requires the pylint.extensions.eq_without_hash plugin to be loaded.

Created by the eq-without-hash checker.