]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11629)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 21 Jan 2019 09:37:54 +0000 (01:37 -0800)
committerChris Withers <chris@withers.org>
Mon, 21 Jan 2019 09:37:54 +0000 (09:37 +0000)
* 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 222d303ade8aadf0adcae5190fac603bdcafe3f0)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst [new file with mode: 0644]

index 955af5d2b85dc04a7b989bc43265661c9b6fc467..42fbc22e748cbafa1d6aa1c60e801d191d40dc4b 100644 (file)
@@ -728,11 +728,10 @@ class NonCallableMock(Base):
                 # 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]
index 49ecbb446629c523373020aa4ec59b089cc10b88..cf0ee8fbb2344f8f3b9d9003a921c49f1e16404f 100644 (file)
@@ -1739,6 +1739,33 @@ class MockTest(unittest.TestCase):
             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 (file)
index 0000000..fe9c69d
--- /dev/null
@@ -0,0 +1,2 @@
+Allow repeated assignment deletion of :class:`unittest.mock.Mock` attributes.
+Patch by Pablo Galindo.