Skip to content

Implement async context manager protocol for AIO clients#2180

Open
Leo Singer (lpsinger) wants to merge 1 commit intoconfluentinc:masterfrom
lpsinger:async-context-manager
Open

Implement async context manager protocol for AIO clients#2180
Leo Singer (lpsinger) wants to merge 1 commit intoconfluentinc:masterfrom
lpsinger:async-context-manager

Conversation

@lpsinger
Copy link
Contributor

When AIOProducer or AIOConsumer is used as the target of a with: statement, automatically close the connection at the end of the context.

What

Checklist

  • Contains customer facing changes? Including API/behavior changes
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • If not, please explain why it is not required

References

JIRA:

Test & Review

Open questions / Follow-ups

@confluent-cla-assistant
Copy link

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ lpsinger
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

When AIOProducer or AIOConsumer is used as the target of a `with:`
statement, automatically close the connection at the end of the
context.
Comment on lines +84 to +93
async def test_async_context_manager(self, mock_producer, mock_common, basic_config):
with AIOProducer(basic_config) as producer:
assert producer._is_closed is False
assert producer._is_closed is True

with AIOProducer(basic_config) as producer2:
assert producer2._is_closed is False
await producer2.close()
await producer2.close()
assert producer2._is_closed is True
Copy link
Member

@ojasvajain Ojasva Jain (ojasvajain) Mar 10, 2026

Choose a reason for hiding this comment

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

  1. We need to use async with AIOProducer ... since we defined aenter and aexit magic methods.

  2. Can we also add the same test for AIOConsumer?

Comment on lines +19 to +23
try:
from typing import Self
except ImportError:
# FIXME: remove once we depend on Python >= 3.11
from typing_extensions import Self

Choose a reason for hiding this comment

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

Can we add a version check instead? For example: if sys.version_info >= (3, 11):

Comment on lines +20 to +25
try:
from typing import Self
except ImportError:
# FIXME: remove once we depend on Python >= 3.11
from typing_extensions import Self

Choose a reason for hiding this comment

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

Copy link
Member

@ojasvajain Ojasva Jain (ojasvajain) left a comment

Choose a reason for hiding this comment

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

Couple of comments. Please pull the latest changes and update the CHANGELOG.md. Thanks for the contribution!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants