Skip to content

Catch format-string arguments passed to exception constructors - #73827

Open
rjgoyln wants to merge 4 commits into
apache:mainfrom
rjgoyln:feat/prek-exception-format-args
Open

rjgoyln wants to merge 4 commits into
apache:mainfrom
rjgoyln:feat/prek-exception-format-args

Conversation

@rjgoyln

@rjgoyln rjgoyln commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Replaces #73076, closed under the open pull request limit.

Summary

Exception constructors do not interpolate. raise AirflowException("TaskInstance %s is not found", ti.task_id) reads like a logger call, but str(e) renders as ('TaskInstance %s is not found', 'my_task'). Exceptions that forward only the message to super(), like google.api_core's GoogleAPICallError family, drop the identifier entirely.

Twenty-eight of these were on main, and they keep arriving: eight came in with the Gemini operators in January 2026. Ruff does not catch this shape — its logging rules only inspect logging calls, and EM101's suggested fix leaves the swallowed arguments in place.

Change

  • Interpolate the 28 existing messages with f-strings.
  • Add prek hook check-no-exception-format-args, failing on any raise whose % placeholder count matches its trailing positional arguments.

The exact-count rule trades recall for precision: several call sites pass a literal message alongside unrelated positional parameters, such as TypeError("Could not parse hits.", response), which a looser check would flag.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 5)

Generated-by: Claude Code (Opus 5) following the guidelines

@fat-catTW

Copy link
Copy Markdown
Contributor

Thanks for the work! Look nice to me.

@rjgoyln

rjgoyln commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the work! Look nice to me.

Thanks for the review!

@eladkal eladkal added this to the Airflow 3.4.0 milestone Oct 3, 2026
@eladkal eladkal added the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Oct 3, 2026
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.
Eight of the recorded files no longer contain the pattern, the google.api_core
call sites among them, so leaving their counts in place would let the same
messages come back unnoticed. Twenty-eight occurrences across five
distributions remain.
These raises handed their values to the exception constructor as extra
arguments, the way a logger call takes them, so the message never got
interpolated and rendered as a tuple with a literal placeholder, e.g.
('File %s not found', 'job.yaml'). With none left, the new hook can block
every occurrence rather than carry an allowlist of them.
…them

No occurrences remain, so a per-file allowlist would only serve as an
escape hatch, and its net-count ratchet let a new occurrence slip in
whenever an old one in the same file was fixed.
@vatsrahul1001 vatsrahul1001 added this to the Airflow 3.4.1 milestone Oct 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:dev-tools changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants