Start with the smallest failing input
Write down the input, the expected behavior, and the actual result before changing the code. If the program raises an exception, read the exception type and message, then trace back to the relevant line in your own code. That line is the first place to investigate; the incorrect value may have been created earlier.
Remove unrelated inputs and setup while preserving the failure. For a stateful bug, keep the sequence of calls that causes it. A test that passes with a fresh object may miss a failure that only appears after reuse.
Inspect values instead of assuming their meaning
Suppose a request allows a timeout of zero, meaning “do not wait,” and uses 30 seconds when the timeout is unspecified. The expression timeout or 30 silently replaces zero with 30, because zero is falsy in Python. The requirement distinguishes None from zero, so the condition must distinguish them too.
This standalone example captures the contract. The first assertion is the regression case; the others protect the existing default and ordinary input.
def effective_timeout(timeout):
return 30 if timeout is None else timeout
assert effective_timeout(0) == 0
assert effective_timeout(None) == 30
assert effective_timeout(5) == 5Use the right inspection tool for the environment
In a local Python process, breakpoint() normally opens the pdb debugger. Use p expression to inspect a value, n to execute the next line, s to step into a call, and c to continue. Place the breakpoint before the decision you suspect, then compare the actual state with your hypothesis.
Bugger runs code in a non-interactive remote environment. Use the reported outputs, visible tests, and custom reproduction cases there; an interactive pdb prompt cannot be answered through the runner. Keep diagnostics small, avoid printing secrets, and remove temporary output before submitting.
Test the assumption around the failure
A fix should explain every part of the failing example without weakening the documented behavior. Avoid broad exception handling that converts an error into an apparently valid result. If a function receives a mutable object, check whether a previous call changed it. For asynchronous failures, record which event happens first and what state each event observes.
- Check None, zero, false, and empty input when they are valid inputs.
- Repeat the same operation to expose retained state.
- Keep the original failing input as a regression test.
- Run nearby successful cases after the change.
Frequently asked questions
- How do you debug a Python function that does not raise an exception?
- Write a small assertion with the input and expected output, inspect the intermediate values right before the failing branch using breakpoint() or focused logging, and check for falsy values (0, "", []) or mutated shared state.
- Why is `if value is None` safer than `if not value` in Python?
- In Python, 0, False, "", [], and {} are all falsy. Using `if not value` treats valid zero or empty inputs the same as missing None values, which causes silent boundary bugs.
Further reading
Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.