mcp(test[middleware]): Fix caplog double-capture on pytest 9.1.0#83
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #83 +/- ##
==========================================
+ Coverage 84.67% 84.73% +0.06%
==========================================
Files 43 43
Lines 3197 3197
Branches 438 438
==========================================
+ Hits 2707 2709 +2
+ Misses 360 359 -1
+ Partials 130 129 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
a1557b3 to
62a72d8
Compare
Member
Author
Code reviewNo issues found. Checked for bugs and AGENTS.md compliance. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
why: pytest 9.1.0 captures non-propagating loggers directly. The test forced fastmcp logger propagation so caplog could see its records; under 9.1.0 that double-captures each emit, so the assertion saw two records instead of one. what: - Drop the propagate=True monkeypatch and the now-unused monkeypatch fixture arg; caplog sees fastmcp natively
why: The middleware error-logging test relies on pytest 9.1.0's direct capture of non-propagating loggers; make the requirement explicit instead of leaving it implied by the lockfile. what: - Raise pytest floor to >=9.1.0 in dev and testing groups - Relock
why: Record the dev-only fix in the unreleased changelog. what: - Add a Development entry linking the PR
why: pytest 9.1.0 captures non-propagating loggers natively, so forcing the fastmcp logger to propagate is dead weight and double-captures each emit. These two tests pre-date the floor and kept the workaround; their content/existential assertions masked the duplication. what: - test_send_keys_batch_schema_validation_redacts_inputs: drop the propagate loop and the now-unused monkeypatch arg - test_expected_tool_error_logs_warning_through_server: drop the monkeypatch call and its stale docstring rationale
62a72d8 to
8d43f6c
Compare
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
test_tool_error_result_logs_at_error_log_level, which fails on pytest 9.1.0 becausecaplogrecords each error-log emit twice (assert [30, 30] == [30]). This redsmainand any branch carrying the dev-dependency bump.propagate=Trueworkaround suite-wide — the failing test plus two sister tests (test_send_keys_batch_schema_validation_redacts_inputs,test_expected_tool_error_logs_warning_through_server) all forcedfastmcplogger propagation socaplogcould see records. pytest 9.1.0 captures non-propagating loggers natively, making the workaround dead weight that double-captures each emit.pytest>=9.1.0in thedevandtestinggroups so the suite's reliance on that capture behavior is a declared contract, not an implicit one.Root cause
fastmcp keeps its
fastmcplogger non-propagating (it owns a Rich handler). The tests compensated by forcingfastmcp.propagate = Truesocaplog— which historically only listened at the root logger — could see the records.pytest 9.1.0 shipped #3697 "Logging capture now works for non-propagating loggers":
catching_logs.__enter__now attaches itsLogCaptureHandlerdirectly to every non-propagating logger at setup, includingfastmcp. A record emitted onfastmcp.errorsreachescaplogby propagating up tofastmcp(where the handler now lives); re-enabling propagation to root captures the same emit a second time.The error is logged once — only the capture was doubled:
caplogseespropagate=Trueworkaround (before)test_tool_error_result_logs_at_error_log_levelasserts an exact record count, so it failed outright. The two sister tests assert on log content, so the duplication was silent — but the workaround was equally obsolete there.Changes by area
tests/test_middleware.py: Drop thepropagate=Trueworkaround (and now-unusedmonkeypatcharg) fromtest_tool_error_result_logs_at_error_log_levelandtest_send_keys_batch_schema_validation_redacts_inputs;caplogcaptures fastmcp's logger natively.tests/test_utils.py: Drop the same workaround fromtest_expected_tool_error_logs_warning_through_server, including the docstring paragraph that described it.pyproject.toml/uv.lock: Floorpytest>=9.1.0indevandtesting; relock.CHANGES: Add a### Developmententry.Verification
The workaround is fully removed from the test suite (the
monkeypatch.setattr(..., "propagate", ...)target):The floor is declared in both dependency groups:
Test plan
test_tool_error_result_logs_at_error_log_level(all parametrizations) passes with--reruns 0test_send_keys_batch_schema_validation_redacts_inputsandtest_expected_tool_error_logs_warning_through_serverpass without the workarounduv run ruff check .anduv run ruff format --check .uv run mypyuv run pytest --reruns 0just build-docs