Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
self._mock_side_effect = None

for child in self._mock_children.values():
if isinstance(child, _SpecState):
if isinstance(child, _SpecState) or child is _deleted:
continue
child.reset_mock(visited)

Expand Down
10 changes: 10 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,16 @@ def test_attribute_deletion(self):
self.assertRaises(AttributeError, getattr, mock, 'f')


def test_reset_mock_does_not_raise_on_attr_deletion(self):
Comment thread
vstinner marked this conversation as resolved.
# bpo-31177: reset_mock should not raise AttributeError when attributes
# were deleted in a mock instance
mock = Mock()
Comment thread
vstinner marked this conversation as resolved.
Outdated
mock.child = True
del mock.child
mock.reset_mock()
Comment thread
vstinner marked this conversation as resolved.
self.assertFalse(hasattr(mock, 'child'))


def test_class_assignable(self):
for mock in Mock(), MagicMock():
self.assertNotIsInstance(mock, int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug that prevented using :meth:`reset_mock <unittest.mock.Mock.reset_mock>`
on mock instances with deleted attributes