Skip to content

Commit edeca92

Browse files
tirkarthivstinner
authored andcommitted
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
1 parent 9890520 commit edeca92

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
@@ -542,7 +542,7 @@ def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
542542
self._mock_side_effect = None
543543

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

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

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

15981598

1599+
def test_reset_mock_does_not_raise_on_attr_deletion(self):
1600+
# bpo-31177: reset_mock should not raise AttributeError when attributes
1601+
# were deleted in a mock instance
1602+
mock = Mock()
1603+
mock.child = True
1604+
del mock.child
1605+
mock.reset_mock()
1606+
self.assertFalse(hasattr(mock, 'child'))
1607+
1608+
15991609
def test_class_assignable(self):
16001610
for mock in Mock(), MagicMock():
16011611
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)