diff options
| author | robot-piglet <[email protected]> | 2025-09-19 19:22:05 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-09-19 19:33:20 +0300 |
| commit | 092f5ded19ef842075ccebbc2900afb5cf009ac5 (patch) | |
| tree | 826e14c44cbe0ef758dc963ccd54842d07428688 /contrib/python/pytest-mock/py3/pytest_mock/plugin.py | |
| parent | 1f2977c48482a39e97894637815ddb3579d0d44e (diff) | |
Intermediate changes
commit_hash:77545ccbe0cf9f22f5ee56187fc1fc43fe1bfe1c
Diffstat (limited to 'contrib/python/pytest-mock/py3/pytest_mock/plugin.py')
| -rw-r--r-- | contrib/python/pytest-mock/py3/pytest_mock/plugin.py | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/contrib/python/pytest-mock/py3/pytest_mock/plugin.py b/contrib/python/pytest-mock/py3/pytest_mock/plugin.py index 50dc06ab7cc..f4dbfc3ec6e 100644 --- a/contrib/python/pytest-mock/py3/pytest_mock/plugin.py +++ b/contrib/python/pytest-mock/py3/pytest_mock/plugin.py @@ -1,21 +1,18 @@ import builtins import functools import inspect +import itertools import unittest.mock import warnings +from collections.abc import Generator +from collections.abc import Iterable +from collections.abc import Iterator +from collections.abc import Mapping from dataclasses import dataclass from dataclasses import field from typing import Any from typing import Callable -from typing import Dict -from typing import Generator -from typing import Iterable -from typing import Iterator -from typing import List -from typing import Mapping from typing import Optional -from typing import Tuple -from typing import Type from typing import TypeVar from typing import Union from typing import cast @@ -52,7 +49,7 @@ class MockCache: Cache MagicMock and Patcher instances so we can undo them later. """ - cache: List[MockCacheItem] = field(default_factory=list) + cache: list[MockCacheItem] = field(default_factory=list) def _find(self, mock: MockType) -> MockCacheItem: for mock_item in self.cache: @@ -124,7 +121,7 @@ class MockerFixture: :param bool return_value: Reset the return_value of mocks. :param bool side_effect: Reset the side_effect of mocks. """ - supports_reset_mock_with_args: Tuple[Type[Any], ...] + supports_reset_mock_with_args: tuple[type[Any], ...] if hasattr(self, "AsyncMock"): supports_reset_mock_with_args = (self.Mock, self.AsyncMock) else: @@ -137,6 +134,8 @@ class MockerFixture: # NOTE: The mock may be a dictionary if hasattr(mock_item.mock, "spy_return_list"): mock_item.mock.spy_return_list = [] + if hasattr(mock_item.mock, "spy_return_iter"): + mock_item.mock.spy_return_iter = None if isinstance(mock_item.mock, supports_reset_mock_with_args): mock_item.mock.reset_mock( return_value=return_value, side_effect=side_effect @@ -178,6 +177,12 @@ class MockerFixture: spy_obj.spy_exception = e raise else: + if isinstance(r, Iterator): + r, duplicated_iterator = itertools.tee(r, 2) + spy_obj.spy_return_iter = duplicated_iterator + else: + spy_obj.spy_return_iter = None + spy_obj.spy_return = r spy_obj.spy_return_list.append(r) return r @@ -204,6 +209,7 @@ class MockerFixture: spy_obj = self.patch.object(obj, name, side_effect=wrapped, autospec=autospec) spy_obj.spy_return = None + spy_obj.spy_return_iter = None spy_obj.spy_return_list = [] spy_obj.spy_exception = None return spy_obj @@ -338,7 +344,7 @@ class MockerFixture: autospec: Optional[builtins.object] = None, new_callable: Optional[builtins.object] = None, **kwargs: Any, - ) -> Dict[str, MockType]: + ) -> dict[str, MockType]: """API to mock.patch.multiple""" return self._start_patch( self.mock_module.patch.multiple, @@ -355,7 +361,7 @@ class MockerFixture: def dict( self, in_dict: Union[Mapping[Any, Any], str], - values: Union[Mapping[Any, Any], Iterable[Tuple[Any, Any]]] = (), + values: Union[Mapping[Any, Any], Iterable[tuple[Any, Any]]] = (), clear: bool = False, **kwargs: Any, ) -> Any: @@ -467,8 +473,8 @@ package_mocker = pytest.fixture(scope="package")(_mocker) session_mocker = pytest.fixture(scope="session")(_mocker) -_mock_module_patches = [] # type: List[Any] -_mock_module_originals = {} # type: Dict[str, Any] +_mock_module_patches: list[Any] = [] +_mock_module_originals: dict[str, Any] = {} def assert_wrapper( |
