Skip to content

[ai misuse] fix(doctest): unwrap mock objects safely during collection - #15338

Closed
Rohan-Dedhia wants to merge 1 commit into
pytest-dev:mainfrom
Rohan-Dedhia:fix/doctest-unwrap-mock-aware
Closed

Rohan-Dedhia wants to merge 1 commit into
pytest-dev:mainfrom
Rohan-Dedhia:fix/doctest-unwrap-mock-aware

Conversation

@Rohan-Dedhia

Copy link
Copy Markdown

Fixes #15333.

Problem

When collecting doctests on modules that import unittest.mock.call (or objects with dynamic attribute resolution that simulate mock wrapping), inspect.unwrap traverses __wrapped__ attributes up to sys.getrecursionlimit(). Because call.__wrapped__ generates a new _Call instance on each access, inspect.unwrap builds a memo dictionary containing thousands of distinct objects, allocating gigabytes of memory and potentially causing OOM when the recursion limit is raised.

MockAwareDocTestFinder in src/_pytest/doctest.py previously overrode _find to run under _patch_unwrap_mock_aware() and skip _is_mocked objects (#3456). That override was previously conditioned on sys.version_info < (3, 10) and was removed during cleanup of Python 3.9 support. However, inspect.unwrap in stdlib across Python 3.10+ still encounters unbounded recursion on unittest.mock.call.

Solution

  • Restore _find on MockAwareDocTestFinder to run under with _patch_unwrap_mock_aware(): and skip _is_mocked objects.
  • Add a regression test with an elevated recursion limit (sys.setrecursionlimit(5000)).
  • Add towncrier newsfragment changelog/15333.bugfix.rst.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 5, 2026
@RonnyPfannschmidt

Copy link
Copy Markdown
Member

ai missuse/missatribution

@RonnyPfannschmidt RonnyPfannschmidt changed the title fix(doctest): unwrap mock objects safely during collection [ai misuse] fix(doctest): unwrap mock objects safely during collection Nov 5, 2026
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