bpo-41877: Improve docs for assert misspellings check in mock (GH-23729)



This is a follow-up to
https://github.com/python/cpython/commit/4662fa9bfe4a849fe87bfb321d8ef0956c89a772.
That original commit expanded guards against misspelling assertions on
mocks. This follow-up updates the documentation and improves the error
message by pointing out the potential cause and solution.

Automerge-Triggered-By: GH:gpshead
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 4db1bac..d43ea9e 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -632,8 +632,9 @@ def __getattr__(self, name):
             raise AttributeError(name)
         if not self._mock_unsafe:
             if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
-                raise AttributeError("Attributes cannot start with 'assert' "
-                                     "or its misspellings")
+                raise AttributeError(
+                    f"{name} is not a valid assertion. Use a spec "
+                    f"for the mock if {name} is meant to be an attribute.")
 
         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 dfcf1ef..016905c 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1598,7 +1598,7 @@ def static_method(): pass
     #Issue21238
     def test_mock_unsafe(self):
         m = Mock()
-        msg = "Attributes cannot start with 'assert' or its misspellings"
+        msg = "is not a valid assertion. Use a spec for the mock"
         with self.assertRaisesRegex(AttributeError, msg):
             m.assert_foo_call()
         with self.assertRaisesRegex(AttributeError, msg):