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

(cherry picked from commit edeca92c84)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2018-12-01 02:24:47 -08:00 committed by GitHub
parent 38c06d9193
commit 422c1658b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

View File

@ -541,7 +541,7 @@ class NonCallableMock(Base):
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)

View File

@ -1566,6 +1566,16 @@ class MockTest(unittest.TestCase):
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):
for mock in Mock(), MagicMock():
self.assertNotIsInstance(mock, int)

View File

@ -0,0 +1,2 @@
Fix bug that prevented using :meth:`reset_mock <unittest.mock.Mock.reset_mock>`
on mock instances with deleted attributes