Skip to content

Commit 422c165

Browse files
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
(cherry picked from commit edeca92) Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
1 parent 38c06d9 commit 422c165

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
541541
self._mock_side_effect = None
542542

543543
for child in self._mock_children.values():
544-
if isinstance(child, _SpecState):
544+
if isinstance(child, _SpecState) or child is _deleted:
545545
continue
546546
child.reset_mock(visited)
547547

Lib/unittest/test/testmock/testmock.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,16 @@ def test_attribute_deletion(self):
15661566
self.assertRaises(AttributeError, getattr, mock, 'f')
15671567

15681568

1569+
def test_reset_mock_does_not_raise_on_attr_deletion(self):
1570+
# bpo-31177: reset_mock should not raise AttributeError when attributes
1571+
# were deleted in a mock instance
1572+
mock = Mock()
1573+
mock.child = True
1574+
del mock.child
1575+
mock.reset_mock()
1576+
self.assertFalse(hasattr(mock, 'child'))
1577+
1578+
15691579
def test_class_assignable(self):
15701580
for mock in Mock(), MagicMock():
15711581
self.assertNotIsInstance(mock, int)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug that prevented using :meth:`reset_mock <unittest.mock.Mock.reset_mock>`
2+
on mock instances with deleted attributes

0 commit comments

Comments
 (0)