> Imagine the context manager is mock.patch used in testing and you want to run two tests in "parallel", each with a different mocked method. mock.patch isn't aware of `await` so patching will be incorrect.
That's still doable with contextvars. You just need a custom mock-like object (or library) that stores its settings/state in a context variable.
Now, the "mock" module doesn't provide this functionality out of the box, but I hope that somebody will come up with a new mock library that will work that way (or with a new mock primitive) after 3.7.0 is released.
Adding __pause__ and __resume__ was considered in PEP 521, and it was decided that the actual implementation will be too slow and complex. It's very unlikely that PEP 521 is ever accepted. |