Commit ae9dc88
authored
feat: add async context manager support to BaseClient (#688)
# Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make
sure it goes smoothly:
- [x] Follow the [`CONTRIBUTING`
Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md).
- [x] Make your Pull Request title in the
<https://www.conventionalcommits.org/> specification.
- Important Prefixes for
[release-please](https://github.com/googleapis/release-please):
- `fix:` which represents bug fixes, and correlates to a
[SemVer](https://semver.org/) patch.
- `feat:` represents a new feature, and correlates to a SemVer minor.
- `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking
change (indicated by the `!`) and will result in a SemVer major.
- [x] Ensure the tests and linter pass (Run `bash scripts/format.sh`
from the repository root to format)
- [x] Appropriate docs were updated (if necessary)
Follow-up to #682, as suggested by @ishymko in the
[review](#682 (review)).
This extends the async context manager pattern to `BaseClient`, which
wraps `ClientTransport` and also exposes a `close()` method.
Fixes #674 🦕
## Problem
`BaseClient` delegates resource cleanup to its underlying
`ClientTransport` via `close()`, but doesn't implement
`__aenter__`/`__aexit__`. This means clients cannot be used with `async
with`, leading to the same resource leak risk that #682 solved for
transports:
```python
client = BaseClient(card=card, config=config, transport=transport, consumers=[], middleware=[])
result = await client.send_message(msg) # if this raises, close() is never called
await client.close()
```
## Fix
Added `__aenter__` and `__aexit__` methods to `BaseClient` in
`src/a2a/client/base_client.py`:
`__aenter__` returns `self`
`__aexit__ `awaits `close()`
This enables the standard async context manager pattern:
```python
async with BaseClient(card=card, config=config, transport=transport, consumers=[], middleware=[]) as client:
async for event in client.send_message(msg):
...
# close() called automatically, even on exceptions
```
This is a non-breaking, additive change. Calling `close()` manually or
via `try/finally` continues to work exactly as before.
## Test
Tests were added to `tests/client/test_base_client.py`, following the
same approach as the `ClientTransport` tests from #682.
Release-As: 0.3.231 parent d3c973f commit ae9dc88
2 files changed
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| |||
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
46 | 62 | | |
47 | 63 | | |
48 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
90 | 110 | | |
91 | 111 | | |
92 | 112 | | |
| |||
0 commit comments