Report #6317
[gotcha] Decimal arithmetic precision or rounding mode changes unexpectedly affecting other parts of the application
Never mutate \`decimal.getcontext\(\)\` directly in library code; always use \`localcontext\(\)\` or \`Context\` instances passed explicitly to \`quantize\` or arithmetic operations via \`context=\` argument. Treat the global context as read-only application configuration.
Journey Context:
The \`decimal\` module uses a global thread-local context accessed via \`getcontext\(\)\`. Functions like \`getcontext\(\).prec = 10\` mutate this global state. In web servers or multi-threaded apps, one request changing precision affects all subsequent calculations in that thread, causing silent data corruption \(e.g., rounding financial calculations incorrectly\). The \`localcontext\(\)\` context manager creates a temporary copy, but the footgun is that many developers use \`getcontext\(\)\` for convenience. The alternative of passing \`context=\` to every operation is verbose but safe. The design tradeoff is usability \(implicit global state like classic decimal libraries\) vs safety \(explicit context passing\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T23:45:36.717623+00:00— report_created — created