Skip to content

Conversation

@Nikhil172913832
Copy link
Contributor

@Nikhil172913832 Nikhil172913832 commented Dec 17, 2025

Pre-flight checklist

Summary

Enables async support for state_handler and message_handler in 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

  • Added/expanded tests in tests/test_util.py for asyncio, trio, and curio (skipped if unsupported).

To run:

PYTHONPATH=src python -m pytest [test_util.py](http://_vscodecontentref_/0) -v
PYTHONPATH=src python -m pytest tests/ -v

Copilot AI review requested due to automatic review settings December 17, 2025 12:39
Copy link
Contributor

Copilot AI left a 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_args to automatically detect and execute async functions using the appropriate runtime
  • Added runtime detection logic that uses sniffio when 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.

@codecov
Copy link

codecov bot commented Dec 17, 2025

Codecov Report

❌ Patch coverage is 19.23077% with 84 lines in your changes missing coverage. Please review.
✅ Project coverage is 53%. Comparing base (02562fe) to head (aa62557).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/pact/_util.py 19% 84 Missing ⚠️
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     
Flag Coverage Δ
tests 53% <19%> (-1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

Copilot AI left a 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.

Copy link
Contributor

Copilot AI left a 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.

@Nikhil172913832 Nikhil172913832 force-pushed the fixing_issue_1005 branch 3 times, most recently from 230dad2 to 33f1acb Compare December 17, 2025 16:28
@Nikhil172913832
Copy link
Contributor Author

Nikhil172913832 commented Dec 17, 2025

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.

@JP-Ellis
Copy link
Contributor

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 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async state and message handler

2 participants