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 222d303

Browse filesBrowse files
pablogsalcjw296
authored andcommitted
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (#11057)
* 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
1 parent e8239b8 commit 222d303
Copy full SHA for 222d303

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
@@ -729,11 +729,10 @@ def __delattr__(self, name):
729729
# not set on the instance itself
730730
return
731731

732-
if name in self.__dict__:
733-
object.__delattr__(self, name)
734-
735732
obj = self._mock_children.get(name, _missing)
736-
if obj is _deleted:
733+
if name in self.__dict__:
734+
super().__delattr__(name)
735+
elif obj is _deleted:
737736
raise AttributeError(name)
738737
if obj is not _missing:
739738
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
@@ -1769,6 +1769,33 @@ def test_attribute_deletion(self):
17691769
self.assertRaises(AttributeError, getattr, mock, 'f')
17701770

17711771

1772+
def test_mock_does_not_raise_on_repeated_attribute_deletion(self):
1773+
# bpo-20239: Assigning and deleting twice an attribute raises.
1774+
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
1775+
NonCallableMock()):
1776+
mock.foo = 3
1777+
self.assertTrue(hasattr(mock, 'foo'))
1778+
self.assertEqual(mock.foo, 3)
1779+
1780+
del mock.foo
1781+
self.assertFalse(hasattr(mock, 'foo'))
1782+
1783+
mock.foo = 4
1784+
self.assertTrue(hasattr(mock, 'foo'))
1785+
self.assertEqual(mock.foo, 4)
1786+
1787+
del mock.foo
1788+
self.assertFalse(hasattr(mock, 'foo'))
1789+
1790+
1791+
def test_mock_raises_when_deleting_nonexistent_attribute(self):
1792+
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
1793+
NonCallableMock()):
1794+
del mock.foo
1795+
with self.assertRaises(AttributeError):
1796+
del mock.foo
1797+
1798+
17721799
def test_reset_mock_does_not_raise_on_attr_deletion(self):
17731800
# bpo-31177: reset_mock should not raise AttributeError when attributes
17741801
# 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.