Skip to content

Fix structlog log assertions never matching a single-field dict - #73112

Closed
Eason09053360 wants to merge 1 commit into
apache:mainfrom
Eason09053360:fix-structlog-capture-single-key-dict-match
Closed

Eason09053360 wants to merge 1 commit into
apache:mainfrom
Eason09053360:fix-structlog-capture-single-key-dict-match

Conversation

@Eason09053360

Copy link
Copy Markdown
Contributor

Why

StructlogCapture.__contains__ backs the cap_structlog fixture, which caplog is replaced by on Airflow 3.1+, so it is what every structured log assertion in the repo goes through.

Its dict branch built the lookup with operator.itemgetter(*target.keys()), whose return type depends on how many keys it is given: a tuple for two or more, but a bare value for one. The expected side is always a tuple, so for a single-key target zip(got, want) iterated that bare value instead of pairing it up — for a string, comparing its first character against the whole expected string:

got  = "some event"                 # str, not a tuple
want = ("some event",)
zip(got, want)  # -> [("s", "some event")]  -> False

A single-field assertion therefore never matched, whatever was logged:

assert {"event": "some event", "field1": False} in caplog  # True
assert {"event": "some event"} in caplog                   # False, even though it was logged

This is a false negative, which is the worst failure mode for a test helper — the natural reading of the red test is that the assertion is wrong, and the way out is caplog.text or a mocked logger, the two patterns contributing-docs asks us to stop using. The cap_structlog docstring itself demonstrates assert {"field2": [1, 2]} in cap_structlog, so the documented example was one of the broken cases. Grepping the repo, there is not a single one-key dict assertion today.

What

devel-common/src/tests_common/test_utils/logs.py builds the actual values with tuple(e[key] for key in target) instead, which keys off the same dict as want and so keeps the two aligned at any number of keys. A missing key still raises KeyError inside the existing try and counts as a non-match.

An empty dict is now rejected with ValueError. itemgetter() used to raise TypeError on zero keys, and losing that would have turned assert expected in caplog into a vacuous pass whenever a dynamically built expected came out empty.

StructlogCapture had no tests; devel-common/tests/unit/tests_common/test_utils/test_logs.py covers the dict branch — the single-key cases (including the docstring's and a re.Pattern value) fail without this change, and the negative cases pin down that partial and missing-key targets still do not match.


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

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

`assert {"event": "..."} in caplog` silently reported "not logged" even when
the event was recorded, because the underlying lookup collapsed a one-key
target to a bare value and compared it character by character.

This is a false negative, which is the worst failure mode for a test helper:
the natural reaction is to assume the test is wrong and fall back to
`caplog.text` or a mocked logger — exactly the two patterns the project is
trying to retire. The plugin's own docstring demonstrates a single-field
assertion, so anyone following the documented example hit it.

@Vamsi-klu Vamsi-klu left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

​

@potiuk

potiuk commented Sep 25, 2026

Copy link
Copy Markdown
Member

Hello @Eason09053360 - thank you for your contributions to Apache Airflow!

The Airflow community has introduced a limit of 5 open pull requests at a time for contributors without write access to the repository. You currently have 33 open pull requests, so - as a one-time step of introducing the limit - we closed the ones where maintainers have not engaged yet:

These pull requests stay open because maintainers are already engaged in them - they count towards your limit:

This is not a judgement of you or of your changes. We never told contributors before that opening many pull requests at once was a problem, so there is nothing to feel bad about - and nothing is lost: your branches, commits and the review history stay where they are.

What we ask you to do is to make your first prioritization decision: choose which of the pull requests above matter most to you, and reopen them (up to 5 open at a time, including the ones still open) with the "Reopen pull request" button or gh pr reopen <PR_NUMBER> --repo apache/airflow. Reopen the ones you are ready to follow through - keep them rebased, respond to review comments and fix failing checks.

While your pull requests are waiting for review, the most valuable thing you can do is help in other ways - reviewing other contributors' pull requests, helping with issues, and taking part in the discussions on the devlist and Slack.

Why we introduced the limit, what it means for you and how to reopen or restore a pull request is explained in https://github.com/apache/airflow/blob/main/contributing-docs/32_open_pull_request_limit.rst.


Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting

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

Labels

closed because of open PR limit Closed as a one-time step of introducing the open pull request limit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants