-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
Prevent prefix "called_" for methods on mock objects in safe mode
Pitch
In safe mode, there's already support for catching typos for accessing the assertion methods:
By default, accessing any attribute whose name starts with assert, assret, asert, aseert or assrt will raise an AttributeError.
Given you have a valid assertion to check whether a mocked function has been called:
assert mock_foo.called
If you now want to check the arguments, and do not pay full attention, you can end up with a tautology like
assert mock_foo.called_once_with(param="test")
The issue: mock_foo.called_once_with is not a valid (assertion) method and therefore an instance of mock.Mock is returned. Because instances of mock.Mock evaluate to true, the assertion is equivalent to assert True.
Like with the preventing the call of methods that start with assert and assret (issue 21238) and also disallowing the typos asert, aseert, and assrt (#23165), this error will not cause a test failure.
Analyzing public repositories on github.com, the Python standard library (thanks @terryjreedy for fixing it in #100647), and our internal code base revealed what seems to be a common source of errors. In our own code base, we have had more than 500 of these issues. More than 50% of those failed after fixing the assertion call, which could potentially have covered existing bugs by relying on bad tests.