gh-123934: Fix `MagicMock` not to reset magic method return values (#124038)

This commit is contained in:
sobolevn 2024-09-19 10:55:47 +03:00 committed by GitHub
parent 43cd7aa8cd
commit 7628f67d55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 1 deletions

View File

@ -331,6 +331,45 @@ class TestMockingMagicMethods(unittest.TestCase):
self.assertEqual(os.fspath(mock), expected_path)
mock.__fspath__.assert_called_once()
def test_magic_mock_does_not_reset_magic_returns(self):
# https://github.com/python/cpython/issues/123934
for reset in (True, False):
with self.subTest(reset=reset):
mm = MagicMock()
self.assertIs(type(mm.__str__()), str)
mm.__str__.assert_called_once()
self.assertIs(type(mm.__hash__()), int)
mm.__hash__.assert_called_once()
for _ in range(3):
# Repeat reset several times to be sure:
mm.reset_mock(return_value=reset)
self.assertIs(type(mm.__str__()), str)
mm.__str__.assert_called_once()
self.assertIs(type(mm.__hash__()), int)
mm.__hash__.assert_called_once()
def test_magic_mock_resets_manual_mocks(self):
mm = MagicMock()
mm.__iter__ = MagicMock(return_value=iter([1]))
mm.custom = MagicMock(return_value=2)
self.assertEqual(list(iter(mm)), [1])
self.assertEqual(mm.custom(), 2)
mm.reset_mock(return_value=True)
self.assertEqual(list(iter(mm)), [])
self.assertIsInstance(mm.custom(), MagicMock)
def test_magic_mock_resets_manual_mocks_empty_iter(self):
mm = MagicMock()
mm.__iter__.return_value = []
self.assertEqual(list(iter(mm)), [])
mm.reset_mock(return_value=True)
self.assertEqual(list(iter(mm)), [])
def test_magic_methods_and_spec(self):
class Iterable(object):

View File

@ -628,7 +628,7 @@ class NonCallableMock(Base):
side_effect = property(__get_side_effect, __set_side_effect)
def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
def reset_mock(self, visited=None, *, return_value=False, side_effect=False):
"Restore the mock object to its initial state."
if visited is None:
visited = []
@ -2218,6 +2218,17 @@ class MagicMock(MagicMixin, Mock):
self._mock_add_spec(spec, spec_set)
self._mock_set_magics()
def reset_mock(self, /, *args, return_value=False, **kwargs):
if (
return_value
and self._mock_name
and _is_magic(self._mock_name)
):
# Don't reset return values for magic methods,
# otherwise `m.__str__` will start
# to return `MagicMock` instances, instead of `str` instances.
return_value = False
super().reset_mock(*args, return_value=return_value, **kwargs)
class MagicProxy(Base):

View File

@ -0,0 +1,2 @@
Fix :class:`unittest.mock.MagicMock` reseting magic methods return values
after ``.reset_mock(return_value=True)`` was called.