Skip to content

Add CopyingMock for mutable call arguments - #656

Open
ada-wh525 wants to merge 2 commits into
pytest-dev:mainfrom
ada-wh525:ada-wh525/copying-mock
Open

ada-wh525 wants to merge 2 commits into
pytest-dev:mainfrom
ada-wh525:ada-wh525/copying-mock

Conversation

@ada-wh525

Copy link
Copy Markdown

Ordinary mocks store references to call arguments, so later mutations to a list or dictionary also change the recorded arguments.

Add mocker.CopyingMock to deep-copy arguments before each call. It can be used directly or with mocker.patch(..., new_callable=mocker.CopyingMock).

Positional and keyword arguments are copied together to preserve shared references and cycles. Both mock backends, child mocks, and the existing patch reset/cleanup behavior are supported.

Includes documentation and tests for mutable arguments, shared references, cycles, side effects, and objects that cannot be copied. Ordinary mocks remain unchanged.

Validation:

  • Python 3.10 and 3.14: 186 tests passed on each.
  • Assertion rewriting disabled: 169 passed, 17 skipped.
  • All pre-commit checks passed, including Ruff, mypy, and RST lint.
  • Strict Sphinx documentation build passed.

Closes #37

ada-wh525 added 2 commits October 31, 2026 21:53
Expose a MagicMock subclass that deep-copies positional and keyword
arguments together before recording calls. Support both mock backends,
child mocks, and the existing patch lifecycle.

Closes pytest-dev#37

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a helper to copy args

1 participant