From debfe253b986c612d395d0aabe4dee949a82578e Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 27 Apr 2019 23:59:17 -0600 Subject: [PATCH 1/2] bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode --- Lib/unittest/mock.py | 2 +- Lib/unittest/test/testmock/testmock.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 1636073ff00935d..b9069c2af0df2c0 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -595,7 +595,7 @@ def __getattr__(self, name): raise AttributeError(name) if not self._mock_unsafe: if name.startswith(('assert', 'assret')): - raise AttributeError(name) + raise AttributeError("Attributes cannot start with 'assert'") result = self._mock_children.get(name) if result is _deleted: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index bdaebbe66b74dbc..11aec67ecfff8ed 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1902,6 +1902,13 @@ def trace(frame, event, arg): obj = mock(spec=Something) self.assertIsInstance(obj, Something) + def test_assert_message_attribute(self): + for mock in Mock(), MagicMock(): + with self.assertRaisesRegex(AttributeError, + "Attributes cannot start with " + "'assert'"): + mock.assert_screen_status + if __name__ == '__main__': unittest.main() From 96edbb4e64f63991ec01e3200f2c0dcb1f1e83fa Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 6 May 2019 10:29:38 -0600 Subject: [PATCH 2/2] Make the requested changes. --- Lib/unittest/mock.py | 3 ++- Lib/unittest/test/testmock/testmock.py | 12 +++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index a0be7931b531730..a33a6208d6b5841 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -571,7 +571,8 @@ def __getattr__(self, name): raise AttributeError(name) if not self._mock_unsafe: if name.startswith(('assert', 'assret')): - raise AttributeError("Attributes cannot start with 'assert'") + raise AttributeError("Attributes cannot start with 'assert' " + "or 'assret'") result = self._mock_children.get(name) if result is _deleted: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 94cf72035a0cb61..b20b8e20e7e6fd7 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1453,9 +1453,10 @@ def static_method(): pass #Issue21238 def test_mock_unsafe(self): m = Mock() - with self.assertRaises(AttributeError): + msg = "Attributes cannot start with 'assert' or 'assret'" + with self.assertRaisesRegex(AttributeError, msg): m.assert_foo_call() - with self.assertRaises(AttributeError): + with self.assertRaisesRegex(AttributeError, msg): m.assret_foo_call() m = Mock(unsafe=True) m.assert_foo_call() @@ -1916,13 +1917,6 @@ def trace(frame, event, arg): # pragma: no cover obj = mock(spec=Something) self.assertIsInstance(obj, Something) - def test_assert_message_attribute(self): - for mock in Mock(), MagicMock(): - with self.assertRaisesRegex(AttributeError, - "Attributes cannot start with " - "'assert'"): - mock.assert_screen_status - if __name__ == '__main__': unittest.main()