1
+ from __future__ import annotations
2
+
1
3
import ssl
2
4
import sys
3
5
from types import TracebackType
4
- from typing import AsyncIterable , AsyncIterator , Iterable , List , Optional , Type
6
+ from typing import AsyncIterable , AsyncIterator , Iterable
5
7
6
8
from .._backends .auto import AutoBackend
7
9
from .._backends .base import SOCKET_OPTION , AsyncNetworkBackend
15
17
class AsyncPoolRequest :
16
18
def __init__ (self , request : Request ) -> None :
17
19
self .request = request
18
- self .connection : Optional [ AsyncConnectionInterface ] = None
20
+ self .connection : AsyncConnectionInterface | None = None
19
21
self ._connection_acquired = AsyncEvent ()
20
22
21
- def assign_to_connection (
22
- self , connection : Optional [AsyncConnectionInterface ]
23
- ) -> None :
23
+ def assign_to_connection (self , connection : AsyncConnectionInterface | None ) -> None :
24
24
self .connection = connection
25
25
self ._connection_acquired .set ()
26
26
@@ -29,7 +29,7 @@ def clear_connection(self) -> None:
29
29
self ._connection_acquired = AsyncEvent ()
30
30
31
31
async def wait_for_connection (
32
- self , timeout : Optional [ float ] = None
32
+ self , timeout : float | None = None
33
33
) -> AsyncConnectionInterface :
34
34
if self .connection is None :
35
35
await self ._connection_acquired .wait (timeout = timeout )
@@ -47,17 +47,17 @@ class AsyncConnectionPool(AsyncRequestInterface):
47
47
48
48
def __init__ (
49
49
self ,
50
- ssl_context : Optional [ ssl .SSLContext ] = None ,
51
- max_connections : Optional [ int ] = 10 ,
52
- max_keepalive_connections : Optional [ int ] = None ,
53
- keepalive_expiry : Optional [ float ] = None ,
50
+ ssl_context : ssl .SSLContext | None = None ,
51
+ max_connections : int | None = 10 ,
52
+ max_keepalive_connections : int | None = None ,
53
+ keepalive_expiry : float | None = None ,
54
54
http1 : bool = True ,
55
55
http2 : bool = False ,
56
56
retries : int = 0 ,
57
- local_address : Optional [ str ] = None ,
58
- uds : Optional [ str ] = None ,
59
- network_backend : Optional [ AsyncNetworkBackend ] = None ,
60
- socket_options : Optional [ Iterable [SOCKET_OPTION ]] = None ,
57
+ local_address : str | None = None ,
58
+ uds : str | None = None ,
59
+ network_backend : AsyncNetworkBackend | None = None ,
60
+ socket_options : Iterable [SOCKET_OPTION ] | None = None ,
61
61
) -> None :
62
62
"""
63
63
A connection pool for making HTTP requests.
@@ -116,8 +116,8 @@ def __init__(
116
116
117
117
# The mutable state on a connection pool is the queue of incoming requests,
118
118
# and the set of connections that are servicing those requests.
119
- self ._connections : List [AsyncConnectionInterface ] = []
120
- self ._requests : List [AsyncPoolRequest ] = []
119
+ self ._connections : list [AsyncConnectionInterface ] = []
120
+ self ._requests : list [AsyncPoolRequest ] = []
121
121
122
122
# We only mutate the state of the connection pool within an 'optional_thread_lock'
123
123
# context. This holds a threading lock unless we're running in async mode,
@@ -139,7 +139,7 @@ def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
139
139
)
140
140
141
141
@property
142
- def connections (self ) -> List [AsyncConnectionInterface ]:
142
+ def connections (self ) -> list [AsyncConnectionInterface ]:
143
143
"""
144
144
Return a list of the connections currently in the pool.
145
145
@@ -227,7 +227,7 @@ async def handle_async_request(self, request: Request) -> Response:
227
227
extensions = response .extensions ,
228
228
)
229
229
230
- def _assign_requests_to_connections (self ) -> List [AsyncConnectionInterface ]:
230
+ def _assign_requests_to_connections (self ) -> list [AsyncConnectionInterface ]:
231
231
"""
232
232
Manage the state of the connection pool, assigning incoming
233
233
requests to connections as available.
@@ -298,7 +298,7 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
298
298
299
299
return closing_connections
300
300
301
- async def _close_connections (self , closing : List [AsyncConnectionInterface ]) -> None :
301
+ async def _close_connections (self , closing : list [AsyncConnectionInterface ]) -> None :
302
302
# Close connections which have been removed from the pool.
303
303
with AsyncShieldCancellation ():
304
304
for connection in closing :
@@ -312,14 +312,14 @@ async def aclose(self) -> None:
312
312
self ._connections = []
313
313
await self ._close_connections (closing_connections )
314
314
315
- async def __aenter__ (self ) -> " AsyncConnectionPool" :
315
+ async def __aenter__ (self ) -> AsyncConnectionPool :
316
316
return self
317
317
318
318
async def __aexit__ (
319
319
self ,
320
- exc_type : Optional [ Type [ BaseException ]] = None ,
321
- exc_value : Optional [ BaseException ] = None ,
322
- traceback : Optional [ TracebackType ] = None ,
320
+ exc_type : type [ BaseException ] | None = None ,
321
+ exc_value : BaseException | None = None ,
322
+ traceback : TracebackType | None = None ,
323
323
) -> None :
324
324
await self .aclose ()
325
325
0 commit comments