Catch format-string arguments passed to exception constructors - #73076
Draft
rjgoyln wants to merge 1 commit into
Draft
Catch format-string arguments passed to exception constructors#73076rjgoyln wants to merge 1 commit into
rjgoyln wants to merge 1 commit into
Conversation
Exception constructors do not interpolate their arguments the way logger calls do, so a logging-style copy-paste leaves the placeholders in the message and the values stranded in args. Where the exception forwards only the message to super(), as google.api_core's does, the values are dropped outright and the identifier the message exists to carry never reaches the user. Nothing in ruff covers this shape, and the existing 44 occurrences make it easy for a reviewer to read as house style.
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
Exception constructors do not interpolate.
raise AirflowException("TaskInstance %s is not found", ti.task_id)reads like a logger call, butException.__init__only stores its arguments — the message keeps its literal%s, and the identifier it exists to carry sits beside it inargs. Where the exception forwards just the message tosuper(), as google.api_core'sGoogleAPICallErrorfamily does, that identifier is dropped from the message entirely.There are 44 of these on main across seven distributions, enough for the shape to read as house style to anyone reviewing a new one. Nothing in ruff covers it:
PLE1205/PLE1206andG001/G002are logging-only, andEM101does fire on these lines but objects to the string literal, so taking its suggested fix leaves the bug in place.Change
check-no-new-exception-format-args, subclassingAllowlistManagerbesidecheck-no-new-airflow-exceptions.%operator would consume them.Detection walks the AST because the pattern routinely spans several lines. The exact-count rule is what keeps the check quiet: ten or so call sites already pass a literal message alongside unrelated positional parameters —
TypeError("Could not parse hits.", response), SQLAlchemy'sOperationalError(statement, params, orig)— and anything looser flags all of them.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 5) following the guidelines