[Python-checkins] bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (#11057)

Chris Withers webhook-mailer at python.org
Mon Jan 21 03:57:59 EST 2019


https://github.com/python/cpython/commit/222d303ade8aadf0adcae5190fac603bdcafe3f0
commit: 222d303ade8aadf0adcae5190fac603bdcafe3f0
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: Chris Withers <chris at withers.org>
date: 2019-01-21T08:57:46Z
summary:

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

files:
A Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 3a22a48c997f..ef5c55d6a165 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -729,11 +729,10 @@ def __delattr__(self, name):
                 # not set on the instance itself
                 return
 
-        if name in self.__dict__:
-            object.__delattr__(self, name)
-
         obj = self._mock_children.get(name, _missing)
-        if obj is _deleted:
+        if name in self.__dict__:
+            super().__delattr__(name)
+        elif obj is _deleted:
             raise AttributeError(name)
         if obj is not _missing:
             del self._mock_children[name]
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 193ae9f9acbf..64e2fcf61c12 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1769,6 +1769,33 @@ def test_attribute_deletion(self):
             self.assertRaises(AttributeError, getattr, mock, 'f')
 
 
+    def test_mock_does_not_raise_on_repeated_attribute_deletion(self):
+        # bpo-20239: Assigning and deleting twice an attribute raises.
+        for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
+                     NonCallableMock()):
+            mock.foo = 3
+            self.assertTrue(hasattr(mock, 'foo'))
+            self.assertEqual(mock.foo, 3)
+
+            del mock.foo
+            self.assertFalse(hasattr(mock, 'foo'))
+
+            mock.foo = 4
+            self.assertTrue(hasattr(mock, 'foo'))
+            self.assertEqual(mock.foo, 4)
+
+            del mock.foo
+            self.assertFalse(hasattr(mock, 'foo'))
+
+
+    def test_mock_raises_when_deleting_nonexistent_attribute(self):
+        for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
+                     NonCallableMock()):
+            del mock.foo
+            with self.assertRaises(AttributeError):
+                del mock.foo
+
+
     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
diff --git a/Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst b/Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst
new file mode 100644
index 000000000000..fe9c69d234cf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst
@@ -0,0 +1,2 @@
+Allow repeated assignment deletion of :class:`unittest.mock.Mock` attributes.
+Patch by Pablo Galindo.



More information about the Python-checkins mailing list