Skip to content

Commit

Permalink
pythongh-65454: avoid triggering call to a PropertyMock in NonCallabl…
Browse files Browse the repository at this point in the history
…eMock.__setattr__ (pythonGH-120019)

(cherry picked from commit 9e9ee50)

Co-authored-by: blhsing <blhsing@gmail.com>
  • Loading branch information
blhsing authored and miss-islington committed Jun 11, 2024
1 parent a9f2daf commit 0ce08f1
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 @@ -800,6 +800,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 0ce08f1

Please sign in to comment.