Commit edeca92c authored by Xtreak's avatar Xtreak Committed by Victor Stinner

bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)

parent 98905204
...@@ -542,7 +542,7 @@ class NonCallableMock(Base): ...@@ -542,7 +542,7 @@ class NonCallableMock(Base):
self._mock_side_effect = None self._mock_side_effect = None
for child in self._mock_children.values(): for child in self._mock_children.values():
if isinstance(child, _SpecState): if isinstance(child, _SpecState) or child is _deleted:
continue continue
child.reset_mock(visited) child.reset_mock(visited)
......
...@@ -1596,6 +1596,16 @@ class MockTest(unittest.TestCase): ...@@ -1596,6 +1596,16 @@ class MockTest(unittest.TestCase):
self.assertRaises(AttributeError, getattr, mock, 'f') self.assertRaises(AttributeError, getattr, mock, 'f')
def test_reset_mock_does_not_raise_on_attr_deletion(self):
# bpo-31177: reset_mock should not raise AttributeError when attributes
# were deleted in a mock instance
mock = Mock()
mock.child = True
del mock.child
mock.reset_mock()
self.assertFalse(hasattr(mock, 'child'))
def test_class_assignable(self): def test_class_assignable(self):
for mock in Mock(), MagicMock(): for mock in Mock(), MagicMock():
self.assertNotIsInstance(mock, int) self.assertNotIsInstance(mock, int)
......
Fix bug that prevented using :meth:`reset_mock <unittest.mock.Mock.reset_mock>`
on mock instances with deleted attributes
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment