gh-119600: mock: do not access attributes of original when new_callable is set (#119601)

In order to patch flask.g e.g. as in #84982, that
proxies getattr must not be invoked. For that,
mock must not try to read from the original
object. In some cases that is unavoidable, e.g.
when doing autospec. However, patch("flask.g",
new_callable=MagicMock) should be entirely safe.
This commit is contained in:
Robert Collins 2024-06-11 07:41:12 +02:00 committed by GitHub
parent 6efe346069
commit 422c4fc855
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 5 deletions

View file

@ -2045,6 +2045,13 @@ def test(): pass
with self.assertRaises(TypeError):
test()
def test_patch_proxy_object(self):
@patch("test.test_unittest.testmock.support.g", new_callable=MagicMock())
def test(_):
pass
test()
if __name__ == '__main__':
unittest.main()