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

miss-islington webhook-mailer at python.org
Thu Dec 10 13:35:41 EST 2020


https://github.com/python/cpython/commit/9fc571359af9320fddbe4aa2710a767f168c1707
commit: 9fc571359af9320fddbe4aa2710a767f168c1707
branch: master
author: vabr-g <vabr at google.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-12-10T10:35:28-08:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2020-12-10-09-24-44.bpo-41877.iJSCvM.rst
M Doc/library/unittest.mock.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index c5360f91f518d..f795a2e8c1aeb 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -262,9 +262,10 @@ the *new_callable* argument to :func:`patch`.
       this is a new Mock (created on first access). See the
       :attr:`return_value` attribute.
 
-    * *unsafe*: By default if any attribute starts with *assert* or
-      *assret* will raise an :exc:`AttributeError`. Passing ``unsafe=True``
-      will allow access to these attributes.
+    * *unsafe*: By default, accessing any attribute with name starting with
+      *assert*, *assret*, *asert*, *aseert* or *assrt* will raise an
+      :exc:`AttributeError`. Passing ``unsafe=True`` will allow access to
+      these attributes.
 
       .. versionadded:: 3.5
 
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 4db1bacf4b10c..d43ea9e23c899 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 dfcf1ef2ee030..016905c3b90e5 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):
diff --git a/Misc/NEWS.d/next/Library/2020-12-10-09-24-44.bpo-41877.iJSCvM.rst b/Misc/NEWS.d/next/Library/2020-12-10-09-24-44.bpo-41877.iJSCvM.rst
new file mode 100644
index 0000000000000..df43cc5d0c9fa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-12-10-09-24-44.bpo-41877.iJSCvM.rst
@@ -0,0 +1 @@
+AttributeError for suspected misspellings of assertions on mocks are now pointing out that the cause are misspelled assertions and also what to do if the misspelling is actually an intended attribute name. The unittest.mock document is also updated to reflect the current set of recognised misspellings.
\ No newline at end of file



More information about the Python-checkins mailing list