From 9eb6deffbe7383b540e36e6d2bde7e4ca3f415e8 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Sun, 9 Dec 2018 21:36:04 +0000 Subject: [PATCH 1/3] Allow repeated deletion of unittest.mock.Mock attributes --- Lib/unittest/mock.py | 7 +++---- Lib/unittest/test/testmock/testmock.py | 21 +++++++++++++++++++ .../2018-12-09-21-35-49.bpo-20239.V4mWBL.rst | 2 ++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 61ed80b976a7b7c..416cdb513a4ed02 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -727,11 +727,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 193ae9f9acbf3ce..38a49a5899bb480 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1769,6 +1769,27 @@ 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')) + self.assertRaises(AttributeError, getattr, mock, 'foo') + + mock.foo = 4 + self.assertTrue(hasattr(mock, 'foo')) + self.assertEqual(mock.foo, 4) + + del mock.foo + self.assertFalse(hasattr(mock, 'foo')) + self.assertRaises(AttributeError, getattr, 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 000000000000000..94e5be77b2fe83f --- /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 ``unittest.mock.Mock`` attributes. +Patch by Pablo Galindo. From 0c3dde00a62ae18533b1f160d54918d2f41d2c9d Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 10 Dec 2018 23:09:05 +0000 Subject: [PATCH 2/3] fixup! Allow repeated deletion of unittest.mock.Mock attributes --- Lib/unittest/test/testmock/testmock.py | 8 ++++++++ .../next/Library/2018-12-09-21-35-49.bpo-20239.V4mWBL.rst | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 38a49a5899bb480..bc062a73eac2ea4 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1790,6 +1790,14 @@ def test_mock_does_not_raise_on_repeated_attribute_deletion(self): self.assertRaises(AttributeError, getattr, mock, 'foo') + def test_mock_raises_when_deleting_inexistent_attribute(self): + for mock in (Mock(), MagicMock(), NonCallableMagicMock(), + NonCallableMock()): + with self.assertRaises(AttributeError): + del mock.foo + 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 index 94e5be77b2fe83f..fe9c69d234cf5e6 100644 --- 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 @@ -1,2 +1,2 @@ -Allow repeated assignment deletion of ``unittest.mock.Mock`` attributes. +Allow repeated assignment deletion of :class:`unittest.mock.Mock` attributes. Patch by Pablo Galindo. From e01c333de11e99554b2bd2ee726c8924e53deb09 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 11 Dec 2018 09:43:46 +0000 Subject: [PATCH 3/3] fixup! fixup! Allow repeated deletion of unittest.mock.Mock attributes --- Lib/unittest/test/testmock/testmock.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index bc062a73eac2ea4..64e2fcf61c12333 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1779,7 +1779,6 @@ def test_mock_does_not_raise_on_repeated_attribute_deletion(self): del mock.foo self.assertFalse(hasattr(mock, 'foo')) - self.assertRaises(AttributeError, getattr, mock, 'foo') mock.foo = 4 self.assertTrue(hasattr(mock, 'foo')) @@ -1787,15 +1786,14 @@ def test_mock_does_not_raise_on_repeated_attribute_deletion(self): del mock.foo self.assertFalse(hasattr(mock, 'foo')) - self.assertRaises(AttributeError, getattr, mock, 'foo') - def test_mock_raises_when_deleting_inexistent_attribute(self): + 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 - del mock.foo def test_reset_mock_does_not_raise_on_attr_deletion(self):