A class defines __eq__ but not __hash__, so its instances cannot be put in sets or used as dict keys

tx0/python:eq-without-hashstrictwarningbug

What it finds

A class that defines __eq__ without __hash__ gets __hash__ = None, and putting an instance in a set or using it as a dict key raises TypeError. When instances are immutable, a __hash__ that hashes the compared fields makes them usable; when they are mutable, writing __hash__ = None says that they are unhashable on purpose. Classes with bases are not checked, since a base may define __hash__ in a way that suits; neither are dataclasses and other decorated classes.

From the rule's source, eq_without_hash.rs.

Example from the rule's tests
class Point:tx0 reports: this class defines __eq__ without __hash__    def __eq__(self, other):        return self.x == other.x  class Hashed:    def __eq__(self, other):        return True     def __hash__(self):        return 0  class Mutable:    __hash__ = None     def __eq__(self, other):        return True  class Child(Base):    def __eq__(self, other):        return True
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:eq-without-hash" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:eq-without-hash] reason="..."

tx0 explain tx0/python:eq-without-hash shows the same in the terminal. Found a false positive? Report it.