Product case study · November 25, 2026
Ask Claude Code why true counts as one item
Repair a Python quantity validator that accepts JSON true as an integer. Give Claude Code fixed tests for booleans, numeric strings, floats and valid counts.

A quantity needs a type policy as well as a range check. Reject booleans before they become plausible counts.
Your order importer asks for a quantity. Someone sends true, and the integer validator lets it through. The range check looks sensible; the surprise is what Python considers an integer.
This is a useful Claude Code exercise because “validate the number” leaves the important decision unstated. Should true, 1.0 and "1" count as one item? For this fictional importer, the answer is no. Write that down before asking the agent to fix anything.
You need Bash, Python 3 and an installed, signed-in Claude Code. The Python baseline and reference repair below were tested locally. The Claude prompt is an authored exercise, not a recorded agent run. Official sources were checked November 25, 2026.
Make the bug visible
Create a disposable practice folder:
quantity_lab=$(mktemp -d /tmp/claude-quantity.XXXXXX)
cd "$quantity_lab"
cat > quantity.py <<'PY'
def validate_quantity(value):
if not isinstance(value, int):
raise TypeError("quantity must be an integer")
if not 1 <= value <= 100:
raise ValueError("quantity must be between 1 and 100")
return value
PY
python3 - <<'PY'
import json
from quantity import validate_quantity
value = json.loads('{"quantity": true}')["quantity"]
result = validate_quantity(value)
print(repr(result), type(result).__name__)
print(result * 12)
PY
The baseline prints True bool, followed by 12. The validator has preserved a boolean, and arithmetic has made it look like a one-item calculation.
Python documents bool as a subclass of int. That is why isinstance(True, int) succeeds. The standard JSON decoder maps JSON true to Python True; parsing correctly does not establish that the value belongs in a quantity field.
I would reject this input at the boundary. Quietly interpreting a switch as a count makes the bad payload harder to trace once it reaches totals and reports.
Give the agent a type policy
Save this as SPEC.md:
validate_quantity(value) validates one already-decoded field.
Accept only values whose exact built-in Python type is int.
Accept the inclusive range 1 through 100 and return the integer unchanged.
Raise TypeError for every other type, including bool, float, str and None.
Raise ValueError for an integer outside the allowed range.
Check type before range. Do not coerce inputs or substitute a default.
Change only quantity.py. Leave this specification and tests unchanged.
Use only the Python standard library.
The exact-type rule is deliberate. This small boundary accepts values produced by the default JSON decoder; it does not need custom integer subclasses. A library that supports other numeric types needs its own policy.
Save this as test_quantity.py:
import json
import unittest
from quantity import validate_quantity
class QuantityTests(unittest.TestCase):
def test_valid_integer_boundaries_and_middle(self):
for value in (1, 25, 100):
with self.subTest(value=value):
result = validate_quantity(value)
self.assertEqual(result, value)
self.assertIs(type(result), int)
def test_booleans_are_type_errors(self):
for value in (True, False):
with self.subTest(value=value):
with self.assertRaises(TypeError):
validate_quantity(value)
def test_other_types_are_not_coerced(self):
for value in (1.0, "1", None, [], {}):
with self.subTest(value=value):
with self.assertRaises(TypeError):
validate_quantity(value)
def test_out_of_range_integers(self):
for value in (-1, 0, 101):
with self.subTest(value=value):
with self.assertRaises(ValueError):
validate_quantity(value)
def test_json_boolean_is_rejected(self):
payload = json.loads('{"quantity": true}')
with self.assertRaises(TypeError):
validate_quantity(payload["quantity"])
def test_json_integer_is_accepted(self):
payload = json.loads('{"quantity": 1}')
result = validate_quantity(payload["quantity"])
self.assertEqual(result, 1)
self.assertIs(type(result), int)
Run:
python3 -m unittest -v test_quantity
The baseline reports two failures and one error across six test methods. True produces no exception in two checks. False fails the range check and raises ValueError, while the contract requires TypeError. The other cases pass.
That error distinction earns its place: a caller can tell “wrong kind of value” from “valid kind, unacceptable amount.” The exact return-type assertion also matters because True == 1; an equality check alone would miss the distinction. Python’s unittest reference documents the exception, equality and identity assertions used here.
Ask Claude Code for the repair
Start claude inside the practice folder and paste:
Read SPEC.md, quantity.py and test_quantity.py.
Run python3 -m unittest -v test_quantity and report the actual baseline.
Explain why True passes the existing integer and range checks, and why
False raises the wrong exception for this contract.
Change only quantity.py. Keep the tests and specification unchanged.
Do not install packages, delegate, commit or touch files outside this folder.
Do not convert values with int(), accept numeric strings or add defaults.
Run the same tests after the change and report the actual result.
Explain which accepted inputs the repair preserves.
Anthropic’s debugging workflow recommends supplying reproduction steps and verifying the fix. Here the fixed tests also protect the policy from a tempting “helpful” conversion.
If the agent uses int(value), ask it to revisit the specification: that would convert True, 1.0 and "1" into an accepted integer. If it edits the tests, restore them from this page before accepting the repair. If it claims success without test output, run the command yourself.
Check the small repair
This reference implementation passes all six test methods:
def validate_quantity(value):
if type(value) is not int:
raise TypeError("quantity must be an integer")
if not 1 <= value <= 100:
raise ValueError("quantity must be between 1 and 100")
return value
The range logic stays the same. The type check now expresses the narrow contract directly, so booleans never reach arithmetic or range validation.
This is field validation, not a complete order parser. Missing keys, malformed JSON and additional fields need separate handling in a real importer. Keep those concerns outside this exercise so the agent has one clear repair to make.
For an input form that intentionally accepts numeric strings, write a separate conversion rule and test it. For a decoded quantity field, I prefer the boring boundary: accept a count, reject a switch, and make the sender correct the payload.
Sources
Checked November 25, 2026:
- Python built-in types: bool — boolean inheritance and numeric behavior.
- Python JSON decoder — JSON-to-Python type conversion.
- Python unittest — test execution, subtests and assertions.
- Anthropic: Common workflows — reproduction-led debugging and verification with Claude Code.
Put this to work
Why isinstance(value, int) includes Python booleans.
Try
Ask Claude Code to repair one validator in a disposable folder.
Prove it worked
Run unchanged tests before and after the edit, including exact return-type checks.
Keep in view
- Reproduce an integer validator accepting JSON true as a quantity.
- Specify accepted types separately from the allowed numeric range.
- Keep fixed tests for booleans, floats, strings and boundary counts.