From 945985719e38f064bb154a0dc2a9efb3bc1fbcd7 Mon Sep 17 00:00:00 2001 From: Ben Hsing Date: Tue, 4 Jun 2024 08:46:24 +0000 Subject: [PATCH 1/3] gh-65454: avoid triggering call to a PropertyMock in __setattr__ --- Lib/test/test_unittest/testmock/testhelpers.py | 8 ++++++++ Lib/unittest/mock.py | 4 ++++ 2 files changed, 12 insertions(+) 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 3ef83e263f53b7..2ac873f19e4074 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -830,6 +830,10 @@ def __setattr__(self, name, value): mock_name = f'{self._extract_mock_name()}.{name}' raise AttributeError(f'Cannot set {mock_name}') + if isinstance(value, PropertyMock): + print(name) + self.__dict__[name] = value + return return object.__setattr__(self, name, value) From b9494fe344e6a63b86a7d75d25133d6e9945d43e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:57:05 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-06-04-08-57-02.gh-issue-65454.o9j4wF.rst 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. From 8e160c8bd9510183a326397ab1b30e5713dbc9ee Mon Sep 17 00:00:00 2001 From: blhsing Date: Tue, 4 Jun 2024 17:04:08 +0800 Subject: [PATCH 3/3] Update mock.py --- Lib/unittest/mock.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 2ac873f19e4074..5876865a32705b 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -831,7 +831,6 @@ def __setattr__(self, name, value): raise AttributeError(f'Cannot set {mock_name}') if isinstance(value, PropertyMock): - print(name) self.__dict__[name] = value return return object.__setattr__(self, name, value)