fix(tracer): declare in_subsegment_async as an async context manager - #8367
Conversation
BaseProvider.in_subsegment_async was decorated with @contextmanager and annotated to return Generator[BaseSegment, None, None], declaring a synchronous context manager. It is entered with async with by Tracer._decorate_async_function, by the documented escape hatch for concurrent async functions, and by the default X-Ray provider. Remove the decorator and annotate the return type as AbstractAsyncContextManager[BaseSegment] so correct code type checks without a cast. No runtime change, the abstract method body is only a docstring. Closes aws-powertools#8365
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks for working on this @vishwakt !
|
Tick the box to add this pull request to the merge queue (same as
|
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #8367 +/- ##
========================================
Coverage 96.63% 96.64%
========================================
Files 296 296
Lines 14765 14764 -1
Branches 1245 1245
========================================
Hits 14268 14268
+ Misses 362 361 -1
Partials 135 135 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|



Issue number: closes #8365
Summary
Changes
BaseProvider.in_subsegment_asyncwas decorated with@contextmanagerand annotated to returnGenerator[BaseSegment, None, None], which declares a synchronous context manager. The method is entered withasync within three places:Tracer._decorate_async_function, the documented escape hatch for concurrent async functions incapture_method, and user code targeting the default X-Ray provider, whosein_subsegment_asyncreturns an asynchronous context manager.This PR removes the
@contextmanagerdecorator from the abstract method and changes the return annotation toAbstractAsyncContextManager[BaseSegment]. No runtime change, the abstract method body is only a docstring.User experience
Before, mypy rejects correct code:
After, the same code type checks without a cast. Verified with mypy on the snippet from the issue, on
aws_lambda_powertools/tracing, and onexamples/tracer/src/capture_method_async_concurrency.py. All unit tests intests/unit/test_tracing.pypass.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.