Product case study · November 25, 2026

Ask Claude Code to stop replacing zero with a default

Fix a Python configuration helper that overwrites 0, False and empty values. Give Claude Code a precise missing-value policy and six regression tests.

Reading time
5 min
Checked
Nov 25, 2026
Ochre, blue and charcoal paper tokens pass through a slotted gate into three separate trays on a warm paper background.
Fill the missing setting without replacing the deliberate one.
Bottom line

Decide what counts as missing before asking an agent to simplify a configuration helper. Zero can be the setting you meant.

You set retries to 0. The program retries three times anyway. A helpful-looking fallback has quietly overwritten your decision.

This is a good small task for Claude Code because the difficult part is deciding what “missing” means. Once you write that policy down, the repair is tiny. Without it, an agent can produce a tidy one-liner that keeps the bug.

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.

Reproduce the unwanted fallback

In this fictional application, zero means “do not retry.” Create a disposable practice folder:

config_lab=$(mktemp -d /tmp/claude-config-defaults.XXXXXX)
cd "$config_lab"
cat > settings.py <<'PY'
def setting(config, key, default):
    return config.get(key) or default
PY
python3 - <<'PY'
from settings import setting
print(setting({"retries": 0}, "retries", 3))
print(setting({"notifications": False}, "notifications", True))
PY

The broken helper prints 3 and True. Both supplied settings disappear.

Python’s truth-value rules treat zero, False, None and empty containers as false. The or operator returns its second operand when the first is false. That makes it convenient when all those values should trigger a fallback. Our application needs a narrower rule.

I would fix the policy before the syntax. An empty recipient list might intentionally mean “send to nobody”; turning it into a default mailing list would be a much bigger mistake than an extra retry.

Write the policy the agent must preserve

Save this as SPEC.md:

setting(config, key, default) reads a plain Python dictionary.
Use default when the key is absent or its value is None.
Return every other supplied value unchanged, including 0, False,
an empty string, an empty list and an empty dictionary.
Return the selected object itself; do not copy or coerce it.
Do not mutate config or default.
This helper selects values; it does not validate their types or ranges.
Change only settings.py. Keep this specification and tests unchanged.
Use only the Python standard library.

This contract deliberately gives absent keys and explicit None the same meaning. A different application may need to distinguish them. Do not transplant this policy into a configuration loader without checking what its callers expect.

Save these tests as test_settings.py:

import unittest
from settings import setting


class SettingTests(unittest.TestCase):
    def test_missing_uses_default_without_inserting_key(self):
        config = {}
        fallback = []
        self.assertIs(setting(config, "value", fallback), fallback)
        self.assertEqual(config, {})
        self.assertEqual(fallback, [])

    def test_none_uses_default_without_replacing_none(self):
        config = {"value": None}
        fallback = {"mode": "standard"}
        self.assertIs(setting(config, "value", fallback), fallback)
        self.assertEqual(config, {"value": None})
        self.assertEqual(fallback, {"mode": "standard"})

    def test_zero_remains_an_integer_zero(self):
        result = setting({"retries": 0}, "retries", 3)
        self.assertEqual(result, 0)
        self.assertIs(type(result), int)

    def test_false_remains_false(self):
        self.assertIs(setting({"enabled": False}, "enabled", True), False)

    def test_empty_values_are_preserved(self):
        for value in ("", [], {}):
            with self.subTest(value=value):
                config = {"value": value}
                self.assertIs(setting(config, "value", "fallback"), value)
                self.assertIs(config["value"], value)
                self.assertEqual(value, type(value)())

    def test_populated_value_is_returned_without_copying(self):
        value = ["audit"]
        config = {"value": value}
        self.assertIs(setting(config, "value", []), value)
        self.assertEqual(config, {"value": ["audit"]})

Run the baseline:

python3 -m unittest -v test_settings

Locally, three test methods failed: zero, false, and empty values. The empty-values method reports three failing subtests. The other three methods passed.

The identity checks are intentional. Python’s assertIs checks that two references point to the same object. This helper promises to select a value without converting or copying it. The integer type check also prevents a boolean from passing as zero merely because it compares equal.

Ask Claude Code for a bounded repair

Start claude inside the practice folder and paste:

Read SPEC.md, settings.py and test_settings.py.
Run python3 -m unittest -v test_settings and report the baseline.
Explain which supplied values the current fallback overwrites.
Change only settings.py to satisfy the specification and fixed tests.
Do not edit tests, install packages, commit, delegate or touch files
outside this folder. Do not add type validation or copy values.
Run the same test command after the change and report its actual output.
Explain what happens for an absent key, None, 0 and False.

Anthropic’s common workflows recommends giving Claude reproduction steps when debugging. Here, the specification also makes the product decision explicit: None means missing, while zero does not.

If Claude changes the expected values to make the tests green, restore the tests from this page and ask it to repair the helper. If it reports success without running the command, run it yourself. A plausible explanation is useful; the unchanged tests are your acceptance check.

Compare the repair with the contract

This reference implementation passes the six tests:

def setting(config, key, default):
    value = config.get(key)
    return default if value is None else value

dict.get() yields None for an absent key when no default argument is supplied. Checking is None then handles both cases our policy groups together. Zero, false and empty containers take the other branch unchanged. See Python’s dictionary documentation.

A tempting alternative is config.get(key, default). That preserves zero and false, but it also preserves explicit None, which fails this contract. Another is setdefault; that can insert a missing key into the input dictionary, violating the no-mutation requirement.

If your real application treats explicit None as a meaningful value, config.get(key, default) may be exactly right. Change the specification and the corresponding test deliberately. The best repair is the one that expresses your missing-value policy clearly enough that the next agent will leave it alone.

Sources

Checked November 25, 2026:

Put this to work

Why Python's or operator is too broad for this configuration policy.

Try

Give Claude Code one broken helper and a six-test acceptance contract.

Prove it worked

Run the same tests before and after the change, including identity checks.

Keep in view

  • Reproduce a fallback that turns zero retries into three retries.
  • Specify whether absent keys and explicit None use the default.
  • Keep fixed tests for zero, False and empty values during the repair.
Learn the workflow: Set up a scoped Claude Code project session