]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__...
authorblhsing <blhsing@gmail.com>
Tue, 11 Jun 2024 05:42:49 +0000 (13:42 +0800)
committerGitHub <noreply@github.com>
Tue, 11 Jun 2024 05:42:49 +0000 (05:42 +0000)
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 edabb4520c13cdb3ab695bd1025f6ff1dd5ce2b1..08975e0e1bd1321dbb72729e149b03b0f52701a3 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.