Repository navigation
Conversation
nicoddemus
left a comment
There was a problem hiding this comment.
Thanks @xiehuanyi! Making resetall() honor its options for autospecced functions is a good fix.
Unfortunately it breaks mocker.spy() on functions and methods. Spies are created with patch.object(..., side_effect=wrapped, autospec=True), so a function spy is also an autospecced function, and clearing its side_effect stops it from calling the real code:
import os.path
def test_spy(mocker):
spy = mocker.spy(os.path, "join")
mocker.resetall(side_effect=True)
result = os.path.join("a", "b")
# result is <MagicMock name='join()' ...>, spy.spy_return is None
assert isinstance(result, str) # failsBefore this PR, the spy kept working after resetall(side_effect=True). Spies should keep their side_effect; for example, skip resetting it when the item is a spy (it has spy_return_list).
A few more things:
- Tests: please add cases for
mocker.spy()(a function and an instance method) andmocker.patch(..., autospec=True)withresetall(return_value=True, side_effect=True). The current test only coversmocker.create_autospec, which is why the suite didn't catch the spy breakage. - Detection:
isinstance(getattr(mock_item.mock, "mock", None), NonCallableMock)also matches any user object passed asnew=that happens to havereset_mockand a.mockattribute. Checkingunderlying_mock._mock_delegate is mock_item.mockwould only match real autospecced functions. return_value: readingunderlying_mock.return_valueafter setting it toDEFAULTalready stores the new child mock on the function, so the second assignment does nothing and can be removed.- Changelog: please start the entry with the PR link like the others:
`#626 <https://github.com/pytest-dev/pytest-mock/pull/626>`_:. - Rebase:
mainhas moved on since this PR was opened (#621 and #623 were merged), so please rebase ontomain.
341bd4a to
26584d9
Compare
|
Rebased onto current main in 26584d9. The fix now checks Local Python 3.12 validation: tox |
mocker.resetall(return_value=True, side_effect=True)leaves both attributes unchanged for autospecced functions because theirreset_mock()closure accepts no arguments.Reset the delegated attributes only for real autospecced functions. Preserve the function signature and default flag behavior, and retain the current main branch's spy tracking and call-through restoration. Add coverage for the four option combinations, patched autospecced functions, and custom
new=objects; strengthen the existing function and method spy checks. Include a linked changelog entry.Validation on Python 3.12:
py312: 163 passed.norewrite: 148 passed, 15 expected skips.git diff --checkpasses.Prepared with OpenAI Codex.