feat: add __enter__ and __aenter__ for context manager support

Client and AsyncClient had __exit__/__aexit__ and close() methods
but were missing __enter__/__aenter__, so `with Client() as c:` and
`async with AsyncClient() as c:` would raise TypeError.

Closes #532
This commit is contained in:
Pawan Singh Kapkoti 2026-04-06 03:25:00 +01:00
parent dbccf192ac
commit 0638e947c6

View File

@ -117,9 +117,15 @@ class BaseClient(contextlib.AbstractContextManager, contextlib.AbstractAsyncCont
**kwargs,
)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()