You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Tracer's BaseProvider declares in_subsegment_async as a synchronous context manager #8365
The type checker accepts async with tracer.provider.in_subsegment_async(...).
The abstract method in_subsegment_async in aws_lambda_powertools/tracing/base.py returns an asynchronous context
manager. Its annotation shows this: -> AbstractAsyncContextManager[BaseSegment]. The declaration does not
have the @contextmanager decorator.
Current Behaviour
The abstract method has the @contextmanager decorator. Its return
annotation is Generator[BaseSegment, None, None]. These declarations
tell the type checker that the method returns a synchronous context
manager.
This declaration does not agree with three parts of Powertools:
Tracer._decorate_async_function enters the result with async with.
The capture_method documentation shows in_subsegment_async with async with as the escape hatch for concurrent asynchronous functions.
The default provider is the recorder from aws-xray-sdk. Its in_subsegment_async returns an asynchronous context manager.
Thus correct code causes a type error. The user must add a cast to
remove the error.
Confirmed. Tracer._decorate_async_function enters in_subsegment_async with async with, and the default X-Ray provider returns an async context manager, so the abstract declaration in BaseProvider is out of sync with both. I opened #8367 with the fix proposed here: drop the @contextmanager decorator and annotate the return as AbstractAsyncContextManager[BaseSegment]. Annotation only, no runtime change. Verified the mypy errors from the snippet are gone and tests pass.
Hey @tibbe, thanks for the really clear report, and @vishwakt thanks for jumping on it with a PR.
I reproduced it and you're right on all three points. base.py declares in_subsegment_async with @contextmanager and a Generator return type, while:
we ourselves enter it with async with in tracer.py:609
our own docs show async with as the escape hatch (tracer.py:530)
the shipped X-Ray provider returns AsyncSubsegmentContextManager, which has __aenter__
So correct code gets a type error and people have to add a cast to silence it. That's a genuine bug in our annotation.
I applied the proposed fix locally to check it and it holds up:
mypy on your snippet goes clean
async with still works at runtime
tracer tests pass (44)
mypy clean across the whole tracing module
And it's not a breaking change. The @contextmanager was on an abstract method with an empty body, so it never did anything at runtime. This is annotation only.
@vishwakt your PR #8367 looks good to me, and putting AbstractAsyncContextManager under TYPE_CHECKING is nicer than what I had. One small thing to double check: contextmanager is still used by the sync in_subsegment just above, so that import needs to stay. Looks like it does in your diff, just confirming ruff didn't flag anything.
Thanks both of you, this was an easy one to review because the report was so precise.
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Activity
vishwakt commentedon Aug 8, 2026
Confirmed. Tracer._decorate_async_function enters in_subsegment_async with async with, and the default X-Ray provider returns an async context manager, so the abstract declaration in BaseProvider is out of sync with both. I opened #8367 with the fix proposed here: drop the @contextmanager decorator and annotate the return as AbstractAsyncContextManager[BaseSegment]. Annotation only, no runtime change. Verified the mypy errors from the snippet are gone and tests pass.
leandrodamascena commentedon Aug 10, 2026
Hey @tibbe, thanks for the really clear report, and @vishwakt thanks for jumping on it with a PR.
I reproduced it and you're right on all three points.
base.pydeclaresin_subsegment_asyncwith@contextmanagerand aGeneratorreturn type, while:async withintracer.py:609async withas the escape hatch (tracer.py:530)AsyncSubsegmentContextManager, which has__aenter__So correct code gets a type error and people have to add a
castto silence it. That's a genuine bug in our annotation.I applied the proposed fix locally to check it and it holds up:
async withstill works at runtimeAnd it's not a breaking change. The
@contextmanagerwas on an abstract method with an empty body, so it never did anything at runtime. This is annotation only.@vishwakt your PR #8367 looks good to me, and putting
AbstractAsyncContextManagerunderTYPE_CHECKINGis nicer than what I had. One small thing to double check:contextmanageris still used by the syncin_subsegmentjust above, so that import needs to stay. Looks like it does in your diff, just confirming ruff didn't flag anything.Thanks both of you, this was an easy one to review because the report was so precise.
fix(tracer): declare in_subsegment_async as an async context manager (#…
powertools-for-aws-oss-automation commentedon Aug 10, 2026
Warning
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
github-actions commentedon Aug 10, 2026
This is now released under 3.34.0 version!