Skip to content

Commit 81eae21

Browse files
[3.13] pythongh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ (pythonGH-120019) (python#120336)
pythongh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ (pythonGH-120019) (cherry picked from commit 9e9ee50) Co-authored-by: blhsing <[email protected]>
1 parent aba5f2a commit 81eae21

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Lib/test/test_unittest/testmock/testhelpers.py

Lines changed: 8 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,9 @@ def __setattr__(self, name, value):
830830
mock_name = f'{self._extract_mock_name()}.{name}'
831831
raise AttributeError(f'Cannot set {mock_name}')
832832

833+
if isinstance(value, PropertyMock):
834+
self.__dict__[name] = value
835+
return
833836
return object.__setattr__(self, name, value)
834837

835838

Lines changed: 1 addition & 0 deletions
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)