summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_unittest/testmock/testhelpers.py8
-rw-r--r--Lib/unittest/mock.py3
-rw-r--r--Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst1
3 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_unittest/testmock/testhelpers.py b/Lib/test/test_unittest/testmock/testhelpers.py
index 74785a8..c9c20f0 100644
--- a/Lib/test/test_unittest/testmock/testhelpers.py
+++ b/Lib/test/test_unittest/testmock/testhelpers.py
@@ -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):
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index edabb45..08975e0 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -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
index 0000000..0b232cf
--- /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.