Skip to content

Commit 6bd8712

Browse files
committed
Drop pre-Python 3.8 support code
pytest-mock only supports Python 3.8+ since version 3.12.
1 parent 366966b commit 6bd8712

File tree

2 files changed

+15
-48
lines changed

2 files changed

+15
-48
lines changed

src/pytest_mock/plugin.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import builtins
33
import functools
44
import inspect
5-
import sys
65
import unittest.mock
76
import warnings
87
from dataclasses import dataclass
@@ -30,16 +29,12 @@
3029

3130
_T = TypeVar("_T")
3231

33-
if sys.version_info >= (3, 8):
34-
AsyncMockType = unittest.mock.AsyncMock
35-
MockType = Union[
36-
unittest.mock.MagicMock,
37-
unittest.mock.AsyncMock,
38-
unittest.mock.NonCallableMagicMock,
39-
]
40-
else:
41-
AsyncMockType = Any
42-
MockType = Union[unittest.mock.MagicMock, unittest.mock.NonCallableMagicMock]
32+
AsyncMockType = unittest.mock.AsyncMock
33+
MockType = Union[
34+
unittest.mock.MagicMock,
35+
unittest.mock.AsyncMock,
36+
unittest.mock.NonCallableMagicMock,
37+
]
4338

4439

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

tests/test_pytest_mock.py

+8-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Generator
1010
from typing import Tuple
1111
from typing import Type
12+
from unittest.mock import AsyncMock
1213
from unittest.mock import MagicMock
1314

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

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

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

3430
@pytest.fixture
3531
def needs_assert_rewrite(pytestconfig):
@@ -173,12 +169,7 @@ def test_mock_patch_dict_resetall(mocker: MockerFixture) -> None:
173169
"NonCallableMock",
174170
"PropertyMock",
175171
"sentinel",
176-
pytest.param(
177-
"seal",
178-
marks=pytest.mark.skipif(
179-
sys.version_info < (3, 7), reason="seal is present on 3.7 and above"
180-
),
181-
),
172+
"seal",
182173
],
183174
)
184175
def test_mocker_aliases(name: str, pytestconfig: Any) -> None:
@@ -243,10 +234,8 @@ def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
243234
expected_name = kwargs.get("name") or "mock"
244235
if NEWEST_FORMATTING:
245236
msg = "expected call not found.\nExpected: {0}()\n Actual: not called."
246-
elif NEW_FORMATTING:
247-
msg = "expected call not found.\nExpected: {0}()\nActual: not called."
248237
else:
249-
msg = "Expected call: {0}()\nNot called"
238+
msg = "expected call not found.\nExpected: {0}()\nActual: not called."
250239
expected_message = msg.format(expected_name)
251240
stub = mocker.stub(**kwargs)
252241
with pytest.raises(AssertionError, match=re.escape(expected_message)):
@@ -259,10 +248,6 @@ def test_failure_message_with_no_name(self, mocker: MagicMock) -> None:
259248
def test_failure_message_with_name(self, mocker: MagicMock, name: str) -> None:
260249
self.__test_failure_message(mocker, name=name)
261250

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

@@ -892,17 +877,11 @@ def test(mocker):
892877
"""
893878
)
894879
result = testdir.runpytest("-s")
895-
if NEW_FORMATTING:
896-
expected_lines = [
897-
"*AssertionError: expected call not found.",
898-
"*Expected: mock('', bar=4)",
899-
"*Actual: mock('fo')",
900-
]
901-
else:
902-
expected_lines = [
903-
"*AssertionError: Expected call: mock('', bar=4)*",
904-
"*Actual call: mock('fo')*",
905-
]
880+
expected_lines = [
881+
"*AssertionError: expected call not found.",
882+
"*Expected: mock('', bar=4)",
883+
"*Actual: mock('fo')",
884+
]
906885
expected_lines += [
907886
"*pytest introspection follows:*",
908887
"*Args:",
@@ -918,9 +897,6 @@ def test(mocker):
918897
result.stdout.fnmatch_lines(expected_lines)
919898

920899

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

0 commit comments

Comments
 (0)