Reduce Bugs to Minimal Reproductions
Core idea
A minimal reproduction is a small standalone program that preserves the same failure. Record the input, environment and expected versus actual behavior. Remove unrelated dependencies one at a time, checking that the failure remains. Once the cause is understood, turn the reproduction into a regression test.
def add(label, items=[]): items.append(label) return itemsprint(add('A'))print(add('B'))The minimal two-call sequence shows that the mutable default is reused.
Expected output:
['A']['A', 'B']Capture the corrected contract
def add(label, items=None): if items is None: items = [] items.append(label) return itemsassert add('A') == ['A']assert add('B') == ['B']print('independent calls')The assertions would reject the earlier shared-default implementation.
Expected output:
independent callsCommon mistake
Do not remove the second call merely to make the failure disappear. A reproduction must preserve the defect, not just become shorter. Use synthetic data rather than copying private records.
Practice
Reduce a function with an off-by-one error to one boundary input. Write a failing assertion, then fix the function without weakening that assertion.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Minimal reproductions
Check your understanding of minimal reproductions.
What is printed?
def f(x=[]): x.append(1); return len(x)print(f(),f())What is printed?
def f(x=None): return [] if x is None else xprint(f() is f())What must survive reduction?
What follows a verified fix?