Skip to content

Commit f648192

Browse files
[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 9e9ee50) Co-authored-by: blhsing <[email protected]>
1 parent fa291a3 commit f648192

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Lib/test/test_unittest/testmock/testhelpers.py

+8
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,14 @@ def test_propertymock_side_effect(self):
11271127
p.assert_called_once_with()
11281128

11291129

1130+
def test_propertymock_attach(self):
1131+
m = Mock()
1132+
p = PropertyMock()
1133+
type(m).foo = p
1134+
m.attach_mock(p, 'foo')
1135+
self.assertEqual(m.mock_calls, [])
1136+
1137+
11301138
class TestCallablePredicate(unittest.TestCase):
11311139

11321140
def test_type(self):

Lib/unittest/mock.py

+3
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ def __setattr__(self, name, value):
800800
mock_name = f'{self._extract_mock_name()}.{name}'
801801
raise AttributeError(f'Cannot set {mock_name}')
802802

803+
if isinstance(value, PropertyMock):
804+
self.__dict__[name] = value
805+
return
803806
return object.__setattr__(self, name, value)
804807

805808

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`unittest.mock.Mock.attach_mock` no longer triggers a call to a ``PropertyMock`` being attached.

0 commit comments

Comments
 (0)