Add aclose to AsyncNetworkBackend interface#1057
Add aclose to AsyncNetworkBackend interface#1057johnjhughes wants to merge 1 commit intoencode:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds an aclose method to the AsyncNetworkBackend interface to make the async backend contract explicit for type checkers.
Changes:
- Added an
aclosemethod to theAsyncNetworkBackendclass that raisesNotImplementedError
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| async def aclose(self) -> None: | ||
| raise NotImplementedError() # pragma: nocover | ||
|
|
There was a problem hiding this comment.
Adding this method to the AsyncNetworkBackend interface creates a breaking change for type checkers. None of the existing implementations (AnyIOBackend, TrioBackend, AutoBackend, AsyncMockBackend) have implemented this method, which means:
- Type checkers will report that these implementations are missing the required aclose method
- Test backends in tests/test_cancellations.py (SlowWriteBackend, SlowReadBackend) will also fail type checking
Additionally, there is no usage of backend.aclose() anywhere in the codebase (e.g., connection pools do not call aclose on their backends), which suggests this method may not be needed yet.
If the goal is to make backends closeable for type checking purposes, all implementations need to be updated in the same PR to include either a working implementation or a no-op pass statement. The AutoBackend implementation in particular would need to delegate to self._backend.aclose() if that backend exists.
| async def aclose(self) -> None: | |
| raise NotImplementedError() # pragma: nocover |
| async def aclose(self) -> None: | ||
| raise NotImplementedError() # pragma: nocover |
There was a problem hiding this comment.
This change creates an asymmetry in the API design. The synchronous NetworkBackend class (line 36) does not have a close method, but now AsyncNetworkBackend has an aclose method. This inconsistency may be confusing for users and library maintainers.
Consider whether:
- NetworkBackend should also have a close method for consistency
- Or if neither backend type needs lifecycle management methods since they are typically stateless factories
Note that both NetworkStream (line 21) and AsyncNetworkStream (line 66) have close/aclose methods respectively, so the current pattern is that streams (connections) need cleanup but backends (connection factories) do not.
Summary
aclosemethod to theAsyncNetworkBackendinterface to make the async backend contract explicit for type checkersTesting