Repository navigation
Conversation
Rules, initialize rules, invariants and teardown of the new class may be async def functions. Each test case runs them on its own asyncio.Runner, one method at a time, so loop-bound objects created in one rule can be used in later rules; the runner is closed after teardown, which cancels leftover tasks. RuleBasedStateMachine now rejects async methods with InvalidDefinition: they used to return an unawaited coroutine, so the body never ran and the test could pass without checking anything.
Pyodide always has a running event loop, so asyncio.Runner cannot start one there, matching the existing asyncio.run tests that are skipped on emscripten. InvalidDefinition is not in the API reference, so the release note no longer links to it.
|
Friendly ping: this is ready for review and CI is green. I am happy to change the API shape, or to split the PR up, if that would make it easier to review. |
|
Gentle ping: this still merges cleanly and CI is green. I'm happy to adjust the API or split the PR up if that would make review easier. |
|
Apologies for the delay here, but the combination of stateful testing and async code is hideously complicated, so it may well be a while before I have the time and headspace to review it carefully enough to merge. (see e.g. PEP-789 for an example of how complicated this can get; finishing that is also higher priority for me!) |
|
Thanks for letting me know, @Zac-HD, and no worries: that makes sense. I'll stop pinging and leave it with you. When you do get to it, I'm happy to change the design or split it up if that makes it easier to review. |
Part of #4107. This is the asyncio-first step suggested in the review of #4147. It runs each async method to completion, so rules never run concurrently, and it doesn't import trio or anyio.
The problem today
An
async defrule on aRuleBasedStateMachinereturns a coroutine that is never awaited. Its body never runs, and the test can pass:On
master,run_state_machine_as_test(Machine)passes.makeruns 0 times, andusegets coroutines. The only symptom is aRuntimeWarningabout the coroutine never being awaited. Without a target bundle, the coroutine instead trips thereturn_valuehealth check, whose message doesn't mention async.What this adds
asyncio.Runner, which is the stdlib way to run several coroutines on one loop. Hypothesis requires Python 3.11+, so it is always available. Each async rule, initialize rule, invariant and teardown runs withrunner.run(...)and finishes before the next step starts. Objects bound to the loop survive from one rule to the next: connections,asyncio.Queues, and context variables (the runner keeps one context for the whole test case). Covered intest_runs_async_and_sync_methods_on_one_loop_per_test_case.runner.close()runs after teardown, even when a rule failed. It cancels tasks still pending and closes the loop, so nothing carries over into the next test case. Covered intest_closes_the_loop_and_cancels_leftover_tasks_when_a_rule_fails.test_reports_the_failing_async_steps).RuleBasedStateMachinenow rejects async methods with anInvalidDefinitionthat names the method and points to the new class. The check runs once per class, insetup_state(). Covered for rules, initialize rules, invariants and teardown.Design notes and open questions
_call(function, *args, **kwargs)method, and_close()runs after teardown. These are the only extension points, and both are private. A trio or anyio machine later needs only those two methods. I kept them private until we agree on the shape. Supportasyncmethods in stateful testing #4107 mentions an__execute_fnhook, and I'm happy to rename it or make it public.@initialize(), the ⭐ idea in Supportasyncmethods in stateful testing #4107; and printingawait state.rule()in the reported steps. I would rather do each in a follow-up, once this shape is agreed.run_state_machine_as_testinside a running loop, for example from anasync defpytest test, raises asyncio's own error (Runner.run() cannot be called from a running event loop). That seems right to me, since the machine owns its loop.Tests
The new tests are in
tests/cover/test_asyncio_stateful.py, as suggested on #4147.tests/cover/test_stateful.pyand every other test file that defines a state machine pass unchanged (307 tests).shed,ruffandmypyare clean on the changed files.AI assistance
Per CONTRIBUTING.rst, I'm marking this as AI-assisted. I wrote the code, tests and this description with an AI coding assistant (Claude). I've read all of it, run the tests, and take responsibility for the change.