Python traps · Chapter 3 of 16

is versus ==

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

  • Part: Everyday traps
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

Why you need this

The tests used small orders, and small orders pass. Nothing raises: is always gives a True or a False. Python does warn, once, when it compiles a file that uses is with a literal. In area.py:

import json

area = json.loads('{"area": "Andheri"}')["area"]
print(area == "Andheri", area is "Andheri")
$ python3 area.py
/workspace/practice/area.py:4: SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
  print(area == "Andheri", area is "Andheri")
True False

That warning lands in a deploy log nobody reads. It only fires for a literal, so x is y between two variables gets no warning at all.

is has one everyday job: asking whether something is the one object you mean, above all None, which there is only one of. For numbers, strings and amounts, the question is always ==.

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: a right payment marked as a mismatch

    Write reproduce() in repro.py. Make order D2047 with two lines, one paneer-butter-masala at 220 and two butter-naan at 40 each, as {"id": "D2047", "lines": [{"dish": ..., "qty": ..., "price": ...}, ...]}, so ₹300 in all. Make the gateway's payment by parsing this JSON with json.loads: {"order_id": "D2047", "amount": 300, "status": "SUCCESS"}. Return what reconcile(order, payment) from payments.py gives.

    The check runs your reproduce() twice: beside the original payments.py it must return "mismatch" (a right payment marked wrong), and beside a fixed payments.py it must return "ok". So the payment must really come from json.loads and the total from the order's lines. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    payments.py: a ₹300 payment matches a ₹300 order

    Fix reconcile(order, payment) in payments.py. A payment whose status is "SUCCESS" and whose amount equals the order's total gives "ok", whatever the size: ₹150, ₹256, ₹257, ₹300, ₹1,000. Any other amount (a rupee short or over, or 0) gives "mismatch", and so does a payment whose status is not "SUCCESS". Python must print no SyntaxWarning when it compiles payments.py.

    Keep order_total and the mismatch log line (payment <id>: amount mismatch (paid <amount>, order <total>)) as they are. The check imports a fresh copy of your payments.py and uses its own orders, with amounts built at run time by sum and json.loads, as in production. A fix that lets a payment within a rupee of the total through hides the bug and accepts short payments.

  3. Task 3

    test_payments.py: a test that would have caught it

    Write a pytest file, test_payments.py, that would have caught this bug before it held a tiffin. The check runs your file three times: beside the original payments.py, where at least one test must fail; beside the fixed payments.py, where every test must pass; and beside a wrong fix that lets a payment within ₹1 of the total through, where at least one test must fail.

    So test both sides: a right payment above ₹256, parsed with json.loads, with the total from the order's lines, is "ok", and a payment one rupee short of the total is "mismatch". A test with a small order cannot see this bug. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: a right payment marked as a mismatch
  • payments.py: a ₹300 payment matches a ₹300 order
  • test_payments.py: a test that would have caught it

More in Everyday traps

Build "is versus ==" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. 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.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.