Sub topic 2
Reproduce the Failure and Shrink It
This chapter assumes you can run Python code and tests from a terminal, use basic git commands, and read a traceback. You have already read the error. The next step is to make the failure happen on demand, then cut it down to the smallest program that still fails.
Why reproduction comes first
An assistant can only reason about what you show it. If you describe a bug that "sometimes crashes," it will guess. If you hand over a short script that fails every time, it can read the code, follow the traceback, and propose a fix you can check.
Reproduction also gives you a pass/fail signal. You will reuse it after the fix to confirm the bug is gone.
Make the failure repeatable
A failure is reproducible when one command produces the same error each time. Write that command down. Typical examples are python stats.py or pytest tests/test_stats.py::test_empty.
If the failure is intermittent, look for the inputs you are not controlling:
- Random numbers. Set a seed, such as
random.seed(1234), and record it. - Time and dates. Pass a fixed timestamp instead of calling
datetime.now(). - Data from files, databases, or networks. Save one failing input and load it from disk.
- Environment. Note the Python version, the operating system, and the package versions from
pip freeze.
Once you have a command that fails, run it twice. If the output differs, you have not found all the inputs yet.
Shrink it to a minimal example
A minimal example is the smallest piece of code that still shows the same error. Shrinking removes noise, and it often reveals the cause before you ask anyone.
Work by deletion:
- Copy the failing code into a new file.
- Remove one piece at a time: an unused import, a branch, a parameter, a loop.
- Run it after each removal.
- If the same error still appears, keep the removal. If it changes or disappears, put the piece back. That piece matters.
- Stop when nothing more can be removed.
Compare the error type and message each time, not just whether it fails. A different error means you have created a new bug and lost the original.
A worked example
Suppose a report script crashes after loading a CSV of 50,000 rows, calling several helper functions. After deleting the file loading, the formatting, and the grouping, you are left with this:
def average(values):
return sum(values) / len(values)
print(average([]))Running it gives the error from the previous chapter:
Traceback (most recent call last):
File "stats.py", line 5, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroFive lines reproduce the bug. You also learned something the original crash hid: the real trigger is an empty list, which means some group in the CSV had no rows. That fact belongs in your prompt.
Common mistakes
- Shrinking by rewriting. Retyping the code from memory can silently fix or change the bug. Delete from a copy of the real code instead.
- Changing two things per step. If the error disappears, you will not know which change did it.
- Losing the original. Commit or copy the failing version before you edit, so you can return to it.
- Accepting a different error. A shrunk example that fails differently is not a reproduction.
Exercise
You are shrinking a failing script. After you remove the line that sorts the results, the script still crashes, but now with a KeyError instead of the original ZeroDivisionError. What should you do, and why?
Show answer
Restore the sorting line. The error changed, so the removal altered the behavior and the script no longer reproduces the original bug. Try removing a different piece, and keep only removals that leave the same error type and message.