Repository navigation
Prevent prefix "called_" for methods on mock objects in safe mode #100690
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Jan 2, 2023 Pinging @terryjreedy and @gpshead as requested in the discussion.
To add a similar example Apache Airflow also fixed places where
has_callswas used instead ofassert_has_callsapache/airflow#20453Reacted by Christian Klein@tirkarthi Thanks, I'm going to add this name.
My opinion: this can only happen in test suites that use
assert thing, which is not pyunit style but for example pytest style.
Our doc (and commit messages) should continue to not useassert thingstyle. But for the people that do use the other style, it’s worth detecting this little bug magnet, for the same reasonassret_*orassert_thingare detected.Reacted by Alex WaygoodI also think this would be a useful check to add. It's very common to use
unittest.mockin the context of a pytest test suite, and it's something I've used in the past. I would have appreciated this kind of check when writing those tests.I categorized this request as a new feature, but the PR is marked bugfix with backports. Which should it be?
This is a good change, catching legitimate bugs in user code, and in a safe module, but it seems clear to me that it’s a new feature.(adding @cjw296 here who reviewed the PR)
To me it also seemed like a new feature, which should probably go into
main, but not be backported. (Though backports via the third-partymockpackage on PyPI seems fine.)I don't agree: there was code there to catch this kind of problem, but it was insufficient / buggy. I'm fine with backporting this to 3.10 and 3.11, but since the two of you feel otherwise, I'm fine to remove those labels. They can always be added back later.
- added a commit that references this issue
on Jan 6, 2023 - added 2 commits that reference this issue
on Jan 31, 2023 26 remaining items
- added 5 commits that reference this issue
on Apr 29, 2025 - added a commit that references this issue
on May 20, 2025 - added a commit that references this issue
on Dec 10, 2025 - added 4 commits that reference this issue
on Feb 26, 2026 - added 2 commits that reference this issue
on Apr 15, 2026
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:
Given you have a valid assertion to check whether a mocked function has been called:
If you now want to check the arguments, and do not pay full attention, you can end up with a tautology like
The issue:
mock_foo.called_once_withis 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 toassert True.Like with the preventing the call of methods that start with
assertandassret(issue 21238) and also disallowing the typosasert,aseert, andassrt(#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.
Previous discussion
https://discuss.python.org/t/include-prefix-called-in-list-of-forbidden-method-prefixes-for-mock-objects-in-unsafe-mode/22249/4
Linked PRs
ATTRIB_DENY_LISTand make it immutable #100819