Repository navigation
Fix structlog log assertions never matching a single-field dict - #73112
Eason09053360 wants to merge 1 commit into
Conversation
`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.
|
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 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 |
Why
StructlogCapture.__contains__backs thecap_structlogfixture, whichcaplogis 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 targetzip(got, want)iterated that bare value instead of pairing it up — for a string, comparing its first character against the whole expected string:A single-field assertion therefore never matched, whatever 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.textor a mocked logger, the two patternscontributing-docsasks us to stop using. Thecap_structlogdocstring itself demonstratesassert {"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.pybuilds the actual values withtuple(e[key] for key in target)instead, which keys off the same dict aswantand so keeps the two aligned at any number of keys. A missing key still raisesKeyErrorinside the existingtryand counts as a non-match.An empty dict is now rejected with
ValueError.itemgetter()used to raiseTypeErroron zero keys, and losing that would have turnedassert expected in caploginto a vacuous pass whenever a dynamically builtexpectedcame out empty.StructlogCapturehad no tests;devel-common/tests/unit/tests_common/test_utils/test_logs.pycovers the dict branch — the single-key cases (including the docstring's and are.Patternvalue) 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?
Generated-by: Claude Code (Opus 5) following the guidelines