Skip to content

Commit c0566e0

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 6c3f272 commit c0566e0

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎Lib/unittest/mock.py‎

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

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

‎Lib/unittest/test/testmock/testmock.py‎

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

15581558

1559+
def test_reset_mock_does_not_raise_on_attr_deletion(self):
1560+
# bpo-31177: reset_mock should not raise AttributeError when attributes
1561+
# were deleted in a mock instance
1562+
mock = Mock()
1563+
mock.child = True
1564+
del mock.child
1565+
mock.reset_mock()
1566+
self.assertFalse(hasattr(mock, 'child'))
1567+
1568+
15591569
def test_class_assignable(self):
15601570
for mock in Mock(), MagicMock():
15611571
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)