-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Accept async context managers for cleanup contexts (#11681) #11704
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: master
Are you sure you want to change the base?
Accept async context managers for cleanup contexts (#11681) #11704
Conversation
…\nAdapt async context managers to behave like single-yield async iterators and add tests.
CodSpeed Performance ReportMerging #11704 will not alter performanceComparing Summary
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #11704 +/- ##
==========================================
- Coverage 98.74% 98.74% -0.01%
==========================================
Files 127 127
Lines 43549 43632 +83
Branches 2327 2331 +4
==========================================
+ Hits 43003 43083 +80
- Misses 390 392 +2
- Partials 156 157 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
aiohttp/web_app.py
Outdated
| # If the callback returned an async context manager (has | ||
| # __aenter__ and __aexit__), wrap it so it behaves like an | ||
| # async iterator that yields once. |
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.
Would it make more sense to make context managers our de facto default now? Surely then all we need to do for backwards compatibility is:
if not isinstance(ctx, AbstractAsyncContextManager):
ctx = asynccontextmanager(ctx)
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.
pushed a fix making it the default.
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.
No, I mean that the code should should be changed to use aenter/aexit instead of anext. Then we don't need the awkward _AsyncCMAsIterator class and much of the other code becomes much simpler.
- fixed doc spelling issues - now uses AbstractAsyncContextManager
for more information, see https://pre-commit.ci
- increased coverage
- reverted spelling_worldlist - moved inside function imports
What do these changes do?
Adapt async context managers to behave like single-yield async iterators.
1- Allow
app.cleanup_ctxcallbacks to return either:a single-yield async generator (existing behavior), or
an async context manager (e.g.
@contextlib.asynccontextmanager).2- Internally adapt async context managers with a small adapter so:
__aenter__runs at startup (equivalent to the generator yielding), and__aexit__runs at cleanup (equivalent to finishing the generator).3- Preserve previous cleanup/error aggregation semantics.
Are there changes in behavior for the user?
No breaking changes; this is fully backwards compatible.
contextlib.asynccontextmanagerinstances for cleanup contexts.Is it a substantial burden for the maintainers to support this?
No
Related issue number
Closes #11681
Checklist
CONTRIBUTORS.txtCHANGES/foldername it
<issue_or_pr_num>.<type>.rst(e.g.588.bugfix.rst)if you don't have an issue number, change it to the pull request
number after creating the PR
.bugfix: A bug fix for something the maintainers deemed animproper undesired behavior that got corrected to match
pre-agreed expectations.
.feature: A new behavior, public APIs. That sort of stuff..deprecation: A declaration of future API removals and breakingchanges in behavior.
.breaking: When something public is removed in a breaking way.Could be deprecated in an earlier release.
.doc: Notable updates to the documentation structure or buildprocess.
.packaging: Notes for downstreams about unobvious side effectsand tooling. Changes in the test invocation considerations and
runtime assumptions.
.contrib: Stuff that affects the contributor experience. e.g.Running tests, building the docs, setting up the development
environment.
.misc: Changes that are hard to assign to any of the abovecategories.
Make sure to use full sentences with correct case and punctuation,
for example:
Use the past tense or the present tense a non-imperative mood,
referring to what's changed compared to the last released version
of this project.