Skip to content

fix(tracer): declare in_subsegment_async as an async context manager - #8367

Open
vishwakt wants to merge 1 commit into
aws-powertools:developfrom
vishwakt:fix/tracer-in-subsegment-async-annotation
Open

fix(tracer): declare in_subsegment_async as an async context manager#8367
vishwakt wants to merge 1 commit into
aws-powertools:developfrom
vishwakt:fix/tracer-in-subsegment-async-annotation

Conversation

@vishwakt

@vishwakt vishwakt commented Aug 8, 2026

Copy link
Copy Markdown

Issue number: closes #8365

Summary

Changes

BaseProvider.in_subsegment_async was decorated with @contextmanager and annotated to return Generator[BaseSegment, None, None], which declares a synchronous context manager. The method is entered with async with in three places: Tracer._decorate_async_function, the documented escape hatch for concurrent async functions in capture_method, and user code targeting the default X-Ray provider, whose in_subsegment_async returns an asynchronous context manager.

This PR removes the @contextmanager decorator from the abstract method and changes the return annotation to AbstractAsyncContextManager[BaseSegment]. No runtime change, the abstract method body is only a docstring.

User experience

Before, mypy rejects correct code:

async with tracer.provider.in_subsegment_async(name="## example"):
    ...
error: "_GeneratorContextManager[BaseSegment, None, None]" has no attribute "__aenter__"  [attr-defined]
error: "_GeneratorContextManager[BaseSegment, None, None]" has no attribute "__aexit__"; maybe "__exit__"?  [attr-defined]

After, the same code type checks without a cast. Verified with mypy on the snippet from the issue, on aws_lambda_powertools/tracing, and on examples/tracer/src/capture_method_async_concurrency.py. All unit tests in tests/unit/test_tracing.py pass.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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
@vishwakt
vishwakt requested a review from a team as a code owner August 8, 2026 07:30
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Aug 8, 2026
@boring-cyborg boring-cyborg Bot added the tracer Tracer utility label Aug 8, 2026
@boring-cyborg

boring-cyborg Bot commented Aug 8, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@sonarqubecloud

sonarqubecloud Bot commented Aug 8, 2026

Copy link
Copy Markdown

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

Labels

size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. tracer Tracer utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Tracer's BaseProvider declares in_subsegment_async as a synchronous context manager

1 participant