Skip to content

Commit e84baec

Browse files
feat: add async context manager support to ClientTransport
1 parent 957e92b commit e84baec

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/a2a/client/transports/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from abc import ABC, abstractmethod
24
from collections.abc import AsyncGenerator, Callable
35

@@ -19,6 +21,19 @@
1921
class ClientTransport(ABC):
2022
"""Abstract base class for a client transport."""
2123

24+
async def __aenter__(self) -> ClientTransport:
25+
"""Enter the async context manager, returning the transport itself."""
26+
return self
27+
28+
async def __aexit__(
29+
self,
30+
exc_type: type[BaseException] | None,
31+
exc_val: BaseException | None,
32+
exc_tb: object | None,
33+
) -> None:
34+
"""Exit the async context manager, ensuring close() is called."""
35+
await self.close()
36+
2237
@abstractmethod
2338
async def send_message(
2439
self,

tests/client/test_base_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ def base_client(
6161
)
6262

6363

64+
@pytest.mark.asyncio
65+
async def test_transport_aenter_returns_self(mock_transport: AsyncMock) -> None:
66+
result = await ClientTransport.__aenter__(mock_transport)
67+
assert result is mock_transport
68+
69+
70+
@pytest.mark.asyncio
71+
async def test_transport_aexit_calls_close(mock_transport: AsyncMock) -> None:
72+
await ClientTransport.__aexit__(mock_transport, None, None, None)
73+
mock_transport.close.assert_awaited_once()
74+
75+
6476
@pytest.mark.asyncio
6577
async def test_send_message_streaming(
6678
base_client: BaseClient, mock_transport: MagicMock, sample_message: Message

0 commit comments

Comments
 (0)