build(flake8): Add S019 rule banning reserved LogRecord keys in logging extra=#117562
Merged
Merged
Conversation
Passing a reserved LogRecord attribute (name, message, args, module, etc.) as a key in a logging extra= dict raises KeyError: "Attempt to overwrite 'X' in LogRecord" at runtime. Nothing static caught this, and it recently caused a silent production incident. Add a custom S019 flake8 rule that flags literal extra= dict keys colliding with reserved LogRecord attributes on logging calls. Only literal dicts with constant string keys are checked; dynamic dicts cannot be detected statically.
Two existing logging calls passed reserved LogRecord keys in extra=, which raise KeyError when the log line fires: - apigateway/proxy.py: "message" -> "error" (breaker-config warning) - preprod/snapshots/tasks.py: "name" -> "image_name" (also fixed in #117550) These are the only repo-wide violations the new S019 rule surfaces.
joshuarli
approved these changes
Jun 12, 2026
markstory
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Passing a reserved
LogRecordattribute (name,message,args,module, …) as a key in a loggingextra=dict raisesKeyError: "Attempt to overwrite 'X' in LogRecord"at runtime, when the log line actually fires. Nothing static catches it — it passes ruff, mypy, and a human skim — and it recently caused a silent production incident in preprod snapshots (the log call meant to aid debugging was the thing that crashed; see #117550).This adds a custom S019 flake8 rule (Sentry's
S###family intools/flake8_plugin.py) that flags this class before runtime, and fixes the only two existing violations it surfaces repo-wide.This follows up a review question on #117550 ("can this class of problem be prevented before runtime?").
Changes
tools/flake8_plugin.py): flagslogger.<level>(..., extra={...})calls whoseextradict has a literal string key colliding with a reservedLogRecordattribute. Auto-registered via the existingS=prefix plugin (no config change).test_S019(tests/tools/test_flake8_plugin.py): coversname/messagehits, a non-reserved key (ok), and a dynamicextra=var(not flagged).hybridcloud/apigateway/proxy.py:"message"→"error"in a breaker-config warning (would have crashed when invalid config was hit).preprod/snapshots/tasks.py:"name"→"image_name"(same fix as fix(preprod): Stop chunk crash from reserved LogRecord key #117550; identical change, no conflict whichever merges first).Known limitation
Only literal
extra={...}dicts with constant string keys are checked. Dynamic dicts (extra=some_var,extra={**spread}, computed keys) can't be detected statically — but the literal case is the one that actually bites.Test Plan
test_S019passes; fulltests/tools/test_flake8_plugin.pysuite green (18 tests)flake8 --select=S019 src tests toolsreports clean after the two fixes (was 2 hits before)