Skip to content

Keep the return value of __enter__ on patched mocks - #650

Open
mehdimirzafaruk wants to merge 2 commits into
pytest-dev:mainfrom
mehdimirzafaruk:fix/enter-warning-return-value
Open

mehdimirzafaruk wants to merge 2 commits into
pytest-dev:mainfrom
mehdimirzafaruk:fix/enter-warning-return-value

Conversation

@mehdimirzafaruk

@mehdimirzafaruk mehdimirzafaruk commented Oct 21, 2026 •

Copy link
Copy Markdown

When a mock created by mocker.patch (or patch.object) has __enter__, pytest-mock sets a side effect on it that warns about using the mock as a context manager. That side effect is a lambda returning the result of warnings.warn(), which is None, and since a side effect that returns something other than DEFAULT replaces the return value, entering the patched object always gives None:

class dummy_module:
    pool = None

def test_pool(mocker):
    pool = mocker.patch.object(dummy_module, "pool")
    pool.__enter__.return_value = connection = object()
    with dummy_module.pool as entered:   # PytestMockWarning, which the message says can be ignored
        pass
    assert entered is connection         # fails: entered is None

With unittest.mock.patch.object the same code gives connection. This shows up when the patched object is itself the context manager (a lock, a pool, a session), where the warning message says it "can be ignored", but the configured __enter__.return_value is silently lost too.

The fix makes the warning side effect return DEFAULT, so __enter__ still warns and then returns its return_value as usual.

Tests: added TestContextManagerWarnings::test_patching_a_context_manager_object, which fails on main (assert None is <MagicMock name='pool.__enter__()'>) and passes with the fix. tox -e py314,norewrite passes and pre-commit is clean.

The side effect that warns about using a mock from mocker.patch as a
context manager returned None, so entering the patched object always
gave None, ignoring the mock's __enter__.return_value. Return DEFAULT
from it so the mock returns its return_value as usual.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 06:11

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.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

2 participants