Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d358a8c

Browse filesBrowse files
miss-islingtonpablogsal
authored andcommitted
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11629)
* Allow repeated deletion of unittest.mock.Mock attributes * fixup! Allow repeated deletion of unittest.mock.Mock attributes * fixup! fixup! Allow repeated deletion of unittest.mock.Mock attributes (cherry picked from commit 222d303) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
1 parent 2fa53cf commit d358a8c
Copy full SHA for d358a8c

File tree

Expand file treeCollapse file tree

3 files changed

+32
-4
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+32
-4
lines changed

‎Lib/unittest/mock.py

Copy file name to clipboardExpand all lines: Lib/unittest/mock.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,10 @@ def __delattr__(self, name):
728728
# not set on the instance itself
729729
return
730730

731-
if name in self.__dict__:
732-
object.__delattr__(self, name)
733-
734731
obj = self._mock_children.get(name, _missing)
735-
if obj is _deleted:
732+
if name in self.__dict__:
733+
super().__delattr__(name)
734+
elif obj is _deleted:
736735
raise AttributeError(name)
737736
if obj is not _missing:
738737
del self._mock_children[name]

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

Copy file name to clipboardExpand all lines: Lib/unittest/test/testmock/testmock.py
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,33 @@ def test_attribute_deletion(self):
17391739
self.assertRaises(AttributeError, getattr, mock, 'f')
17401740

17411741

1742+
def test_mock_does_not_raise_on_repeated_attribute_deletion(self):
1743+
# bpo-20239: Assigning and deleting twice an attribute raises.
1744+
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
1745+
NonCallableMock()):
1746+
mock.foo = 3
1747+
self.assertTrue(hasattr(mock, 'foo'))
1748+
self.assertEqual(mock.foo, 3)
1749+
1750+
del mock.foo
1751+
self.assertFalse(hasattr(mock, 'foo'))
1752+
1753+
mock.foo = 4
1754+
self.assertTrue(hasattr(mock, 'foo'))
1755+
self.assertEqual(mock.foo, 4)
1756+
1757+
del mock.foo
1758+
self.assertFalse(hasattr(mock, 'foo'))
1759+
1760+
1761+
def test_mock_raises_when_deleting_nonexistent_attribute(self):
1762+
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
1763+
NonCallableMock()):
1764+
del mock.foo
1765+
with self.assertRaises(AttributeError):
1766+
del mock.foo
1767+
1768+
17421769
def test_reset_mock_does_not_raise_on_attr_deletion(self):
17431770
# bpo-31177: reset_mock should not raise AttributeError when attributes
17441771
# were deleted in a mock instance
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow repeated assignment deletion of :class:`unittest.mock.Mock` attributes.
2+
Patch by Pablo Galindo.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.