Skip to content

doctest: stop unwrapping at mock objects in _is_routine - #15420

Open
RonnyPfannschmidt wants to merge 6 commits into
mainfrom
claude/project-thread-5hvhaq
Open

RonnyPfannschmidt wants to merge 6 commits into
mainfrom
claude/project-thread-5hvhaq

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

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_routine unwraps every module attribute, and mock.call.__wrapped__ is always a new object, so it runs to sys.getrecursionlimit(). This override runs it under _patch_unwrap_mock_aware(); the old _find override's _is_mocked early 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.

  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory.

Generated by Claude Code

claude and others added 5 commits October 28, 2026 15:29
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
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 20, 2026
@RonnyPfannschmidt RonnyPfannschmidt self-assigned this Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014GZsRGEhX1kpYaUtSftXSs

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

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--doctest-modules uses GBs of memory on modules importing unittest.mock.call when the recursion limit is raised

2 participants