◐ Shell
clean mode source ↗

bpo-17013: Extend Mock.called to allow waiting for calls by Kentzo · Pull Request #17133 · python/cpython

This implementation of bpo-17013 is alternative to #16094

Changes are based on my work for asynctest. Specifically on _AwaitEvent that was left out when related code was ported to CPython.

Key features:

  • Gives meaning to the existing Mock.called property, otherwise not much useful
  • Does not require end users to do anything: change is automatically available in every Mock
  • Utilizes existing semantics of python conditionals (both asyncio and threading)

Accepting this change will allow me to port _AwaitEvent therefore giving identical semantics to both wait-for-calls and wait-for-awaits.

I will provide necessary typing annotations for typeshed.

Considerations:

  1. This approach changes type of the Mock.called property from bool to private bool-like _CallEvent. However, the only practical problem I could think of is if someone was checking type of Mock.called (e.g. via isinstance). That does not sound like a plausible problem: after all the property itself is seldom used with exception of conditional expressions where _CallEvent.__bool__ and _CallEvent.__eq__ is sufficient.
  2. It probably makes sense to provide convenience methods like wait_for_call, but I would like to hear the opinion of the reviewers.

CC: @vstinner @tirkarthi @mariocj89

https://bugs.python.org/issue17013