Skip to content

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

Merged
leandrodamascena merged 3 commits into
aws-powertools:developfrom
vishwakt:fix/tracer-in-subsegment-async-annotation
Aug 10, 2026
Merged

fix(tracer): declare in_subsegment_async as an async context manager#8367
leandrodamascena merged 3 commits into
aws-powertools:developfrom
vishwakt:fix/tracer-in-subsegment-async-annotation

Conversation

@vishwakt

@vishwakt vishwakt commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

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

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @vishwakt !

@mergify

mergify Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena
leandrodamascena merged commit ad15c7a into aws-powertools:develop Aug 10, 2026
13 checks passed
@boring-cyborg

boring-cyborg Bot commented Aug 10, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.64%. Comparing base (dccd850) to head (2ac3dd2).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
aws_lambda_powertools/tracing/base.py 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

2 participants