Repository navigation
doctest: stop unwrapping at mock objects in _is_routine - #15420
Open
RonnyPfannschmidt wants to merge 6 commits into
Open
RonnyPfannschmidt wants to merge 6 commits into
RonnyPfannschmidt wants to merge 6 commits into
Conversation
Fixes #15281. `_find_wrapped_fixture_def` used `inspect.unwrap`, whose depth limit is `sys.getrecursionlimit()`. `unittest.mock.call` returns a new `_Call` for every attribute, including `__wrapped__`, and slipped through the mock guard because its `_mock_name` is `None`. With a raised recursion limit, collection walked that chain until it exhausted memory. - Treat any `_mock_name` attribute as a mock by using `NOTSET` as the sentinel instead of `None`. - Walk `__wrapped__` with a local loop capped at 100 steps, with id-based loop detection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRF9tm1CrxGUNz1r4UC
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRF9tm1CrxGUNz1r4UC
Review feedback: the hand-rolled __wrapped__ walk added complexity on top of #13745. Go back to inspect.unwrap and stop it at mock-like objects instead, reusing the _is_mocked check that doctest already uses for the same unittest.mock.call problem (moved to compat so fixtures can share it). This also replaces the _mock_name guard, which missed mock.call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRF9tm1CrxGUNz1r4UC
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRF9tm1CrxGUNz1r4UC
The stdlib DocTestFinder._is_routine unwraps every module attribute. mock.call.__wrapped__ is always a new _Call, so unwrapping only stops at sys.getrecursionlimit(), which costs gigabytes when the limit is raised. Run it under the mock-aware unwrap. The _find override that used to do this was dropped with Python 3.9 support. closes #15333 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRGEhX1kpYaUtSftXSs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014GZsRGEhX1kpYaUtSftXSs
RonnyPfannschmidt
changed the base branch from
claude/project-thread-twy9z6
to
main
November 20, 2026 05:29
This branch has not been deployed
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.
Requested by Ronny · project thread
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
closes #15333. Stacked on #15283, merge that first.
The stdlib
DocTestFinder._is_routineunwraps every module attribute, andmock.call.__wrapped__is always a new object, so it runs tosys.getrecursionlimit(). This override runs it under_patch_unwrap_mock_aware(); the old_findoverride's_is_mockedearly return was never reached, since mocks are filtered out before_find. The new test counts__wrapped__accesses and fails on main. Together with #15283 the issue's reproducer at limit 100 000 drops to 0.09 s / 37 MB.Co-authored-bycommit trailers.changelogdirectory.Generated by Claude Code