Python traps 2 · Chapter 1 of 16

__eq__ without __hash__

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Names
  • 3 tasks
  • Python
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

The lesson

By the end of this case

You can tell why a class that defines __eq__ stops working in sets and dicts, make the crash happen on purpose, give it a __hash__ that keeps the rule "equal objects hash the same", and pin it down with a pytest test that checks both sides.

The ticket

DD-512: Morning dispatch crashes, riders get no route sheets. Assigned to you by Kunal. Priority: high. Since: Thursday's release.

Bhavna, in the riders group at 6:40 am: "The route sheets did not come out again. Second day running. I sent every rider his stops on WhatsApp from the order list, by hand, in the rain. Before Thursday the same customer kept showing up twice on the sheets when they ordered from the app and the website, but at least the sheets came."

The dispatch job's log, with the last lines of its traceback:

06:30:02  dispatch  building route sheets for 212 orders
06:30:02  dispatch    File "/srv/dabbadrop/customers.py", line 28, in unique_customers
06:30:02  dispatch      if customer not in seen:
06:30:02  dispatch  TypeError: unhashable type: 'Customer'

Kunal: "Thursday's release fixed the double customers: the website and the app each build their own Customer, so Customer now says two customers are equal when their ids match. That part works. And since then, the one function that puts customers in a set falls over. Make it crash on purpose first, then fix customers.py, then make sure it never comes back."

Equal, but not hashable

A set or a dict finds things by hash first: a number worked out from the object that says which bucket to look in. Only inside that bucket does it ask ==. Every class gets a hash for free, based on the object's identity. But when a class defines __eq__, Python sets its __hash__ to None, because the free hash would now break the rules. Here is a rider class of the same kind:

class Rider:
    def __init__(self, phone, name):
        self.phone = phone
        self.name = name

    def __eq__(self, other):
        return self.phone == other.phone


a = Rider("98200 11001", "Santosh Kamble")
b = Rider("98200 11001", "Santosh K.")
print(a == b)
print(Rider.__hash__)
on_duty = {a}
True
None
Traceback (most recent call last):
  File "/tmp/ex1.py", line 14, in <module>
    on_duty = {a}
              ^^^
TypeError: unhashable type: 'Rider'

The rule a hash must keep

The rule is one line: objects that are equal must have equal hashes. If they did not, a set would look in the wrong bucket and never ask ==. A hash of every field breaks it as soon as two equal objects differ in a field __eq__ ignores:

    def __hash__(self):
        return hash((self.phone, self.name))
True False
2

a == b, yet the set holds both riders: the first line is a == b and hash(a) == hash(b), the second len({a, b}). Hash only what __eq__ compares, and only things that never change while the object sits in a set:

    def __hash__(self):
        return hash(self.phone)
True True
1
Andheri

The last line is {a: "Andheri"}[b]: a dict finds b under a's key. A frozen dataclass, @dataclass(frozen=True), writes both methods for you from its fields. Ruff knows the trap too (ruff check --select PLW1641): "Object does not implement __hash__ method".

Making it happen on purpose

A bug you can only see in production is a bug you cannot fix with confidence. So you reproduce it first: a function, reproduce(), that runs the ticket's steps and returns what the code gave. Here the code raises, so your repro catches the TypeError and returns its message; once the code is fixed, it returns the count instead. It never asserts and never decides the answer itself. The check runs it in a fresh python3 twice: beside the original customers.py it must give the bug's value, and beside a fixed one the healthy value.

A test that would have caught it

A guard test runs before every release. A pytest test is a function whose name starts with test_; assert checks a value, and a test also fails when the code raises. For the rider class:

# test_routes.py
from route import Rider


def test_one_rider_on_duty_once():
    shift = {Rider("98200 11001", "Santosh Kamble"), Rider("98200 11001", "Santosh K.")}
    assert len(shift) == 1

Beside the class with no hash it fails (trimmed):

$ python3 -m pytest test_routes.py
E       TypeError: unhashable type: 'Rider'
FAILED test_routes.py::test_one_rider_on_duty_once - TypeError: unhashable ty...
============================== 1 failed in 0.02s ===============================

A good guard fails on the bug and passes on the fix, and it looks at both sides: the hash that uses every field stops the crash, and still counts Santosh twice. This test catches both.

Your turn

Three tasks, one file each. In repro.py, make the dispatch crash on purpose with three orders, and run it with python3 repro.py in the case's folder. In customers.py, fix Customer. In test_customers.py, write the guard: the check runs it beside the original code, the fix and a wrong fix that hashes every field. Then answer Kunal's interview question at the bottom of the case, in your own words.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: the same customer, twice, and a crash

    Write reproduce() in repro.py. Make three orders, each a dict with "id" and "customer", for Customer("C014", "Dhruv Gupta", "Borivali") (the website), Customer("C014", "Dhruv G.", "Borivali") (the app) and Customer("C022", "Myra Parekh", "Dadar"), all from customers.py. Call unique_customers with the three orders and return how many customers it gave, as an int. If it raises TypeError, catch it and return the error's message, str(error), instead.

    The check runs your reproduce() twice: beside the original customers.py it must return "unhashable type: 'Customer'", and beside a fixed customers.py it must return 2 (Dhruv once, Myra once). So it must really call unique_customers with those three orders. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    customers.py: one customer, counted once

    Fix Customer in customers.py so customers can go in a set and be dict keys. Two customers with the same id are equal, whatever their names and areas, and must count once in a set, find each other as dict keys and have the same hash(); customers with different ids stay different. unique_customers gives each customer once, in the order of their first order, keeping the first object it saw.

    Keep __eq__ comparing the customer id only (a Customer compared with anything else is simply unequal), and keep unique_customers, count_by_area and __repr__ as they are. The check imports a fresh copy of your customers.py and uses its own customers, often the same customer with a different name: a fix that stops the crash but still counts those twice is as wrong as the bug.

  3. Task 3

    test_customers.py: a test that would have caught it

    Write a pytest file, test_customers.py, that would have caught this bug before the dispatch run. The check runs your file three times: beside the original customers.py, where at least one test must fail; beside the fixed customers.py, where every test must pass; and beside a wrong fix that hashes the id, the name and the area together, where at least one test must fail.

    So test both sides: customers go through unique_customers (or into a set) without an error, and the same customer with two different names counts once. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: the same customer, twice, and a crash
  • customers.py: one customer, counted once
  • test_customers.py: a test that would have caught it

More in Names

Build "__eq__ without __hash__" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.