Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.