Skip to content

Register each patcher from mocker.patch.multiple individually - #648

Open
patrickswedish wants to merge 1 commit into
pytest-dev:mainfrom
patrickswedish:fix/patch-multiple-registration-638
Open

patrickswedish wants to merge 1 commit into
pytest-dev:mainfrom
patrickswedish:fix/patch-multiple-registration-638

Conversation

@patrickswedish

@patrickswedish patrickswedish commented Oct 17, 2026 •

Copy link
Copy Markdown

Fixes #638.

Summary

mocker.patch.multiple() previously registered the returned dictionary as a single entry in _mock_cache. As a result:

  1. The PytestMockWarning context-manager hook was checked on the dictionary rather than on each patched mock.
  2. mocker.stop() could not stop an individual mock returned by (or passed to) mocker.patch.multiple().

Changes

  • In MockerFixture._Patcher.multiple(), split patcher and patcher.additional_patchers into individual single-attribute patchers (p.attribute_name = None, patcher.additional_patchers.clear()) and start each one through self._start_patch(...).
  • If starting any attribute patcher fails midway through multiple(), stop and unregister the previously started patchers from that call before re-raising.
  • Remove the MockCacheItem.reset_mocks indirection, since each patched value from patch.multiple is now directly registered in _mock_cache like patch and patch.object.
  • Add regression tests in TestStop and TestContextManagerWarnings.

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.

Mocks created by mocker.patch.multiple are not handled like other patches

1 participant