]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 11 Jun 2024 06:12:16 +0000 (08:12 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Jun 2024 06:12:16 +0000 (06:12 +0000)
gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ (GH-120019)
(cherry picked from commit 9e9ee50421c857b443e2060274f17fb884d54473)

Co-authored-by: blhsing <blhsing@gmail.com>
Lib/test/test_unittest/testmock/testhelpers.py
Lib/unittest/mock.py
Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst [new file with mode: 0644]

index 74785a83757a92d7c1e77e660c570b74b2bcbe72..c9c20f008ca5a24d71e18389f488dc745d31aca2 100644 (file)
@@ -1127,6 +1127,14 @@ class TestCallList(unittest.TestCase):
         p.assert_called_once_with()
 
 
+    def test_propertymock_attach(self):
+        m = Mock()
+        p = PropertyMock()
+        type(m).foo = p
+        m.attach_mock(p, 'foo')
+        self.assertEqual(m.mock_calls, [])
+
+
 class TestCallablePredicate(unittest.TestCase):
 
     def test_type(self):
index d0f2b8b7761c536b65749b06f3d6f129144b2eaf..b701ec33f3b9bb58d647fccd4b9ef41b97faab3c 100644 (file)
@@ -830,6 +830,9 @@ class NonCallableMock(Base):
             mock_name = f'{self._extract_mock_name()}.{name}'
             raise AttributeError(f'Cannot set {mock_name}')
 
+        if isinstance(value, PropertyMock):
+            self.__dict__[name] = value
+            return
         return object.__setattr__(self, name, value)
 
 
diff --git a/Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst b/Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst
new file mode 100644 (file)
index 0000000..0b232cf
--- /dev/null
@@ -0,0 +1 @@
+:func:`unittest.mock.Mock.attach_mock` no longer triggers a call to a ``PropertyMock`` being attached.