-
Notifications
You must be signed in to change notification settings - Fork 159
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
Maintain contextvars between fixtures and tests #1008
Conversation
The approach I've taken here is to maintain a contextvars.Context instance in a contextvars.ContextVar, copying it from the ambient context whenever we create a new event loop. The fixture setup and teardown run within that context, and each test function gets a copy (as if it were created as a new asyncio.Task from within the fixture task). Fixes pytest-dev#127.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1008 +/- ##
==========================================
+ Coverage 90.83% 91.42% +0.58%
==========================================
Files 2 2
Lines 513 548 +35
Branches 66 72 +6
==========================================
+ Hits 466 501 +35
Misses 28 28
Partials 19 19 ☔ View full report in Codecov by Sentry. |
It occurs to me that we can get rid of this caveat by copying the modified values out to the ambient context after running the setup phase, and resetting them after the finalizer. I'll look into that approach. |
Instead of storing a Context in a context variable, just copy out the changes from the setup task's context into the ambient context, and reset the changes after running the finalizer task.
Done, and I'm even happier with this approach! |
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.
You put a lot of effort into this, thanks! I find this a fairly elegant way to address the long-standing problem with context vars in pytest-asyncio. Your use of helper functions makes the code easy to follow and there are tests to support the change.
I'd appreciate if:
- You addressed my comment regarding the structure of the tests.
- You added a corresponding entry in
docs/reference/changelog.rst
that mentions the support for contextvars and the limitation to Python >=3.11.
Also add type annotations for _create_task_in_context.
Thanks for the review! I think I've addressed all of your comments, but please let me know if I missed something. |
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.
That's exactly what I had in mind, thanks!
The approach I've taken here is to inspect the context after running the fixture setup task and copy any changed values out into the test's ambient context. When we run the finalizer to tear down the fixture, we then reset the modified variables.
Some limitations:
Fixes #127.