Skip to content

Commit

Permalink
Drop pre-Python 3.8 support code
Browse files Browse the repository at this point in the history
pytest-mock only supports Python 3.8+ since version 3.12.
  • Loading branch information
nicoddemus committed Mar 21, 2024
1 parent 366966b commit 6bd8712
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 48 deletions.
23 changes: 7 additions & 16 deletions src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import builtins
import functools
import inspect
import sys
import unittest.mock
import warnings
from dataclasses import dataclass
Expand Down Expand Up @@ -30,16 +29,12 @@

_T = TypeVar("_T")

if sys.version_info >= (3, 8):
AsyncMockType = unittest.mock.AsyncMock
MockType = Union[
unittest.mock.MagicMock,
unittest.mock.AsyncMock,
unittest.mock.NonCallableMagicMock,
]
else:
AsyncMockType = Any
MockType = Union[unittest.mock.MagicMock, unittest.mock.NonCallableMagicMock]
AsyncMockType = unittest.mock.AsyncMock
MockType = Union[
unittest.mock.MagicMock,
unittest.mock.AsyncMock,
unittest.mock.NonCallableMagicMock,
]


class PytestMockWarning(UserWarning):
Expand Down Expand Up @@ -271,17 +266,13 @@ def _start_patch(
# check if `mocked` is actually a mock object, as depending on autospec or target
# parameters `mocked` can be anything
if hasattr(mocked, "__enter__") and warn_on_mock_enter:
if sys.version_info >= (3, 8):
depth = 5
else:
depth = 4
mocked.__enter__.side_effect = lambda: warnings.warn(
"Mocks returned by pytest-mock do not need to be used as context managers. "
"The mocker fixture automatically undoes mocking at the end of a test. "
"This warning can be ignored if it was triggered by mocking a context manager. "
"https://pytest-mock.readthedocs.io/en/latest/remarks.html#usage-as-context-manager",
PytestMockWarning,
stacklevel=depth,
stacklevel=5,
)
return mocked

Expand Down
40 changes: 8 additions & 32 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Generator
from typing import Tuple
from typing import Type
from unittest.mock import AsyncMock
from unittest.mock import MagicMock

import pytest
Expand All @@ -22,14 +23,9 @@
platform.python_implementation() == "PyPy", reason="could not make it work on pypy"
)

# Python 3.8 changed the output formatting (bpo-35500), which has been ported to mock 3.0
NEW_FORMATTING = sys.version_info >= (3, 8)
# Python 3.11.7 changed the output formatting, https://github.com/python/cpython/issues/111019
NEWEST_FORMATTING = sys.version_info >= (3, 11, 7)

if sys.version_info[:2] >= (3, 8):
from unittest.mock import AsyncMock


@pytest.fixture
def needs_assert_rewrite(pytestconfig):
Expand Down Expand Up @@ -173,12 +169,7 @@ def test_mock_patch_dict_resetall(mocker: MockerFixture) -> None:
"NonCallableMock",
"PropertyMock",
"sentinel",
pytest.param(
"seal",
marks=pytest.mark.skipif(
sys.version_info < (3, 7), reason="seal is present on 3.7 and above"
),
),
"seal",
],
)
def test_mocker_aliases(name: str, pytestconfig: Any) -> None:
Expand Down Expand Up @@ -243,10 +234,8 @@ def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
expected_name = kwargs.get("name") or "mock"
if NEWEST_FORMATTING:
msg = "expected call not found.\nExpected: {0}()\n Actual: not called."
elif NEW_FORMATTING:
msg = "expected call not found.\nExpected: {0}()\nActual: not called."
else:
msg = "Expected call: {0}()\nNot called"
msg = "expected call not found.\nExpected: {0}()\nActual: not called."
expected_message = msg.format(expected_name)
stub = mocker.stub(**kwargs)
with pytest.raises(AssertionError, match=re.escape(expected_message)):
Expand All @@ -259,10 +248,6 @@ def test_failure_message_with_no_name(self, mocker: MagicMock) -> None:
def test_failure_message_with_name(self, mocker: MagicMock, name: str) -> None:
self.__test_failure_message(mocker, name=name)

@pytest.mark.skipif(
sys.version_info[:2] < (3, 8),
reason="This Python version doesn't have `AsyncMock`.",
)
def test_async_stub_type(self, mocker: MockerFixture) -> None:
assert isinstance(mocker.async_stub(), AsyncMock)

Expand Down Expand Up @@ -892,17 +877,11 @@ def test(mocker):
"""
)
result = testdir.runpytest("-s")
if NEW_FORMATTING:
expected_lines = [
"*AssertionError: expected call not found.",
"*Expected: mock('', bar=4)",
"*Actual: mock('fo')",
]
else:
expected_lines = [
"*AssertionError: Expected call: mock('', bar=4)*",
"*Actual call: mock('fo')*",
]
expected_lines = [
"*AssertionError: expected call not found.",
"*Expected: mock('', bar=4)",
"*Actual: mock('fo')",
]
expected_lines += [
"*pytest introspection follows:*",
"*Args:",
Expand All @@ -918,9 +897,6 @@ def test(mocker):
result.stdout.fnmatch_lines(expected_lines)


@pytest.mark.skipif(
sys.version_info < (3, 8), reason="AsyncMock is present on 3.8 and above"
)
@pytest.mark.usefixtures("needs_assert_rewrite")
def test_detailed_introspection_async(testdir: Any) -> None:
"""Check that the "mock_use_standalone" is being used."""
Expand Down

0 comments on commit 6bd8712

Please sign in to comment.