https://github.com/python/cpython/commit/f6481925d80080f09c3bb264da7f5eedf0f86068
commit: f6481925d80080f09c3bb264da7f5eedf0f86068
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: cjw296 <[email protected]>
date: 2024-06-11T06:07:25Z
summary:

[3.12] gh-65454: avoid triggering call to a PropertyMock in 
NonCallableMock.__setattr__ (GH-120019) (#120337)

gh-65454: avoid triggering call to a PropertyMock in 
NonCallableMock.__setattr__ (GH-120019)
(cherry picked from commit 9e9ee50421c857b443e2060274f17fb884d54473)

Co-authored-by: blhsing <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst
M Lib/test/test_unittest/testmock/testhelpers.py
M Lib/unittest/mock.py

diff --git a/Lib/test/test_unittest/testmock/testhelpers.py 
b/Lib/test/test_unittest/testmock/testhelpers.py
index 74785a83757a92..c9c20f008ca5a2 100644
--- a/Lib/test/test_unittest/testmock/testhelpers.py
+++ b/Lib/test/test_unittest/testmock/testhelpers.py
@@ -1127,6 +1127,14 @@ def test_propertymock_side_effect(self):
         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):
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 854010c7fb893e..ad4b4ebcd42a4f 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -800,6 +800,9 @@ def __setattr__(self, name, value):
             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
index 00000000000000..0b232cf8ca1baf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst
@@ -0,0 +1 @@
+:func:`unittest.mock.Mock.attach_mock` no longer triggers a call to a 
``PropertyMock`` being attached.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to