Spot the bug in the report context manager

from Context managers
Python 3.14 intermediate 5 min 1 issue to find

Find why this report file remains open after the block fails.

Python
from contextlib import contextmanager

@contextmanager
def open_report(path):
    handle = open(path, "w", encoding="utf-8")
    yield handle
    handle.close()

try:
    with open_report("report.txt") as report:
        report.write("draft")
        raise ValueError("invalid report")
except ValueError:
    print(report.closed)
Open in playground
Report an error