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.calledproperty, 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:
- This approach changes type of the
Mock.calledproperty fromboolto private bool-like_CallEvent. However, the only practical problem I could think of is if someone was checking type ofMock.called(e.g. viaisinstance). 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. - It probably makes sense to provide convenience methods like
wait_for_call, but I would like to hear the opinion of the reviewers.