镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Honor resetall options for autospecced functions - #626

Open
xiehuanyi wants to merge 1 commit into
pytest-dev:mainfrom
xiehuanyi:fix-autospec-function-reset-options
Open

xiehuanyi wants to merge 1 commit into
pytest-dev:mainfrom
xiehuanyi:fix-autospec-function-reset-options

Conversation

@xiehuanyi

@xiehuanyi xiehuanyi commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

mocker.resetall(return_value=True, side_effect=True) leaves both attributes unchanged for autospecced functions because their reset_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:

  • Regression controls fail before the autospec fix (4 failures).
  • Isolated tox py312: 163 passed.
  • Isolated tox norewrite: 148 passed, 15 expected skips.
  • All pre-commit hooks pass, including mypy; git diff --check passes.

Prepared with OpenAI Codex.

Copilot AI balanced review requested due to automatic review settings October 11, 2026 21:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)  # fails

Before 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) and mocker.patch(..., autospec=True) with resetall(return_value=True, side_effect=True). The current test only covers mocker.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 as new= that happens to have reset_mock and a .mock attribute. Checking underlying_mock._mock_delegate is mock_item.mock would only match real autospecced functions.
  • return_value: reading underlying_mock.return_value after setting it to DEFAULT already 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: main has moved on since this PR was opened (#621 and #623 were merged), so please rebase onto main.

@xiehuanyi
xiehuanyi force-pushed the fix-autospec-function-reset-options branch from 341bd4a to 26584d9 Compare October 17, 2026 15:09
@xiehuanyi

Copy link
Copy Markdown
Contributor Author

Rebased onto current main in 26584d9. The fix now checks _mock_delegate identity and preserves main's spy call-through and state handling. Added patched-autospec and custom new= regression cases, strengthened the existing function/method spy option matrix, removed the redundant return-value assignment, and linked the changelog entry.

Local Python 3.12 validation: tox py312 passed 163 tests; norewrite passed 148 with 15 expected skips. All pre-commit hooks pass, including mypy. Hosted CI is still pending.

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.

3 participants