Skip to content

Commit

Permalink
[3.13] pythongh-65454: avoid triggering call to a PropertyMock in Non…
Browse files Browse the repository at this point in the history
…CallableMock.__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]>
  • Loading branch information
miss-islington and blhsing authored Jun 11, 2024
1 parent aba5f2a commit 81eae21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/test/test_unittest/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,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)


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

0 comments on commit 81eae21

Please sign in to comment.