Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ #120019

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading