[Python-checkins] bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991)

Kushal Das webhook-mailer at python.org
Wed May 8 13:32:29 EDT 2019


https://github.com/python/cpython/commit/b9b08cd948de97d756a199b60becce8397a8c882
commit: b9b08cd948de97d756a199b60becce8397a8c882
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: Kushal Das <mail at kushaldas.in>
date: 2019-05-08T23:02:23+05:30
summary:

bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991)

* bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode

* Make the requested changes.

files:
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 1e8057d5f5bb..47ed06c6f486 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -572,7 +572,8 @@ 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' "
+                                     "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 5f917dd20f1d..b20b8e20e7e6 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()



More information about the Python-checkins mailing list