-
Notifications
You must be signed in to change notification settings - Fork 145
Enable async state/message handlers (asyncio/trio/curio) with ContextVar preservation and tests #1411
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
base: main
Are you sure you want to change the base?
Enable async state/message handlers (asyncio/trio/curio) with ContextVar preservation and tests #1411
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables async support for state_handler and message_handler in Pact Python by enhancing the apply_args utility function to detect and execute coroutine functions across multiple async runtimes (asyncio, trio, and curio). The implementation preserves ContextVar state across runtime boundaries and adds parameter matching for underscore-prefixed names to support handlers that ignore certain parameters.
Key Changes
- Enhanced
apply_argsto automatically detect and execute async functions using the appropriate runtime - Added runtime detection logic that uses
sniffiowhen available, falling back to bytecode inspection - Implemented ContextVar preservation when creating new event loops to maintain state across thread boundaries
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
src/pact/_util.py |
Added async execution support to apply_args, new _run_async and _detect_async_runtime_from_coro helper functions, and underscore parameter name matching |
tests/test_util.py |
Comprehensive test suite covering asyncio, trio, and curio runtimes with ContextVar preservation, exception handling, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
770638f to
868f689
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1411 +/- ##
====================================
- Coverage 54% 53% -1%
====================================
Files 32 32
Lines 3849 3946 +97
====================================
+ Hits 2096 2114 +18
- Misses 1753 1832 +79
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
868f689 to
3ccc083
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
3ccc083 to
971f06f
Compare
971f06f to
898202c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 14 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
230dad2 to
33f1acb
Compare
…Var preservation and tests
33f1acb to
aa62557
Compare
|
The lines that codecov is reporting as uncovered are part of the function _run_async_coroutine and _detect_async_runtime_from_coroutine which are being called in the test when I traced through the tests locally but I am not sure why is codecov not considering them covered, maybe I am missing something. I will try to evaluate it a bit more. Secondly, the bytecode tests were failing due to how version <3.12 handle bytecode but these tests were passing on versions >=3.12. So for now I have added skips for versions below 3.12. However, with sniffio installed the detection works fine on versions <3.12. |
|
Once again, thank you for taking a look at this! I won't have time to review this until next week, but I'll definitely make sure to take a good look 🚀 |
Pre-flight checklist
Summary
Enables async support for
state_handlerandmessage_handlerin Pact Python, supporting asyncio, trio, and curio runtimes. Handlers can now be coroutine functions, and ContextVar state is preserved across runtime and thread boundaries. Parameter matching is improved to allow underscore-prefixed names. Mock handling is robust for compatibility suite.Breaking Changes
No breaking changes to the public API. Curio support is skipped on incompatible Python versions (e.g., 3.12+), but this is a compatibility caveat, not a breaking change.
Motivation
Closes #1005. This change allows users to write async state/message handlers in any supported async runtime, making Pact Python more flexible for modern test suites and frameworks. It also ensures ContextVar safety and robust mock handling for compatibility and testability.
Test Plan
tests/test_util.pyfor asyncio, trio, and curio (skipped if unsupported).To run: