fix(tracer): declare in_subsegment_async as an async context manager - #8367
Open
vishwakt wants to merge 1 commit into
Open
fix(tracer): declare in_subsegment_async as an async context manager#8367vishwakt wants to merge 1 commit into
vishwakt wants to merge 1 commit into
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. |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.