-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TrioTestCase: Match other people's APIs for context stacks #8
Comments
Yes, that seems reasonable. Although I think we'd want both spellings (push_async_callback and addAsyncCleanup) so that we're compatible with both AsyncExitStack and TestCase. Possibly we should even inherit from AsyncExitStack, so that the typing matches up without needing to use an interface... Although I still don't remember the whole philosophy I was going for with passing around an AsyncExitStack, other than that I think Nursery should support AsyncExitStack methods... because, if you want to run some code when you get cancelled, you can do that currently today, but it's a hassle that requires a fair bit of boilerplate. Possibly a better API than this ExitStack business would be to just have a helper function which takes a Nursery and a callback and starts up a task to run that callback when the Nursery is cancelled? I don't remember if that would work with the concrete use case. Maybe I left some commit messages behind when I added the AsyncExitStack, you might check.
That would be reasonable to support too, sure.
Don't we already automatically provide a nursery? |
The reason of doing so in |
One of the changes in #7 is that TrioTestCase now has a
stack
member of typecontextlib.AsyncExitStack
, which you can use viaawait self.stack.enter_async_context(...)
orself.stack.push_async_callback(...)
.As it happens, I think we're only internally using the latter.
I think it'd be a good idea for
TrioTestCase
to look like other test classes. Upstreamunittest.TestCase
and more interestinglyunittest.IsolatedAsyncioTestCase
solve this problem in a different way: they haveself.addCleanup(...)
andawait self.addAsyncCleanup(...)
methods.So, in theory, we could adopt that approach pretty straightforwardly.
However, upstream unittest is considering adding support for
ExitStack
/AsyncExitStack
- see bpo-45046 and python/cpython#28045. The API is slightly different from ours - instead of exposing an actualstack
member or even having such a private member, they just implemententerContext
etc. andenterAsyncContext
themselves, by (async) calling the enter function and adding an (async) cleanup for the exit function.I have a vague feeling that context managers are a better approach than
addAsyncCleanup
/push_async_callback
, but I'm not really sure.For what it's worth, pytest-trio just has async fixtures which can cleanup post-
yield
, and are somewhat geared towards using context managers with a literalwith
statement. Their tutorial has this example:I don't think there's any other way in pytest-trio to do cleanup.
(It's maybe also worth noting that pytest-trio's
nursery
fixture cancels the nursery's cancel scope when the test finishes, which is at least a little bit of what we're doing in our cleanups, but not all of it. So there may also be an argument forTrioTestCase
to automatically provide a nursery, maybe....)The text was updated successfully, but these errors were encountered: