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

This commit is contained in:
blhsing 2024-06-11 13:42:49 +08:00 committed by GitHub
parent 422c4fc855
commit 9e9ee50421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 0 deletions

View File

@ -1127,6 +1127,14 @@ class TestCallList(unittest.TestCase):
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):

View File

@ -830,6 +830,9 @@ class NonCallableMock(Base):
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)

View File

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