Skip to content

Commit 127505b

Browse files
authored
Use new typing style (#963)
* Use new typing style * Pass all checks
1 parent 4ee1ca2 commit 127505b

25 files changed

+458
-474
lines changed

httpcore/_api.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
from __future__ import annotations
2+
13
from contextlib import contextmanager
2-
from typing import Iterator, Optional, Union
4+
from typing import Iterator
35

46
from ._models import URL, Extensions, HeaderTypes, Response
57
from ._sync.connection_pool import ConnectionPool
68

79

810
def request(
9-
method: Union[bytes, str],
10-
url: Union[URL, bytes, str],
11+
method: bytes | str,
12+
url: URL | bytes | str,
1113
*,
1214
headers: HeaderTypes = None,
13-
content: Union[bytes, Iterator[bytes], None] = None,
14-
extensions: Optional[Extensions] = None,
15+
content: bytes | Iterator[bytes] | None = None,
16+
extensions: Extensions | None = None,
1517
) -> Response:
1618
"""
1719
Sends an HTTP request, returning the response.
@@ -47,12 +49,12 @@ def request(
4749

4850
@contextmanager
4951
def stream(
50-
method: Union[bytes, str],
51-
url: Union[URL, bytes, str],
52+
method: bytes | str,
53+
url: URL | bytes | str,
5254
*,
5355
headers: HeaderTypes = None,
54-
content: Union[bytes, Iterator[bytes], None] = None,
55-
extensions: Optional[Extensions] = None,
56+
content: bytes | Iterator[bytes] | None = None,
57+
extensions: Extensions | None = None,
5658
) -> Iterator[Response]:
5759
"""
5860
Sends an HTTP request, returning the response within a content manager.

httpcore/_async/connection.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
import itertools
24
import logging
35
import ssl
46
from types import TracebackType
5-
from typing import Iterable, Iterator, Optional, Type
7+
from typing import Iterable, Iterator
68

79
from .._backends.auto import AutoBackend
810
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream
@@ -37,15 +39,15 @@ class AsyncHTTPConnection(AsyncConnectionInterface):
3739
def __init__(
3840
self,
3941
origin: Origin,
40-
ssl_context: Optional[ssl.SSLContext] = None,
41-
keepalive_expiry: Optional[float] = None,
42+
ssl_context: ssl.SSLContext | None = None,
43+
keepalive_expiry: float | None = None,
4244
http1: bool = True,
4345
http2: bool = False,
4446
retries: int = 0,
45-
local_address: Optional[str] = None,
46-
uds: Optional[str] = None,
47-
network_backend: Optional[AsyncNetworkBackend] = None,
48-
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
47+
local_address: str | None = None,
48+
uds: str | None = None,
49+
network_backend: AsyncNetworkBackend | None = None,
50+
socket_options: Iterable[SOCKET_OPTION] | None = None,
4951
) -> None:
5052
self._origin = origin
5153
self._ssl_context = ssl_context
@@ -59,7 +61,7 @@ def __init__(
5961
self._network_backend: AsyncNetworkBackend = (
6062
AutoBackend() if network_backend is None else network_backend
6163
)
62-
self._connection: Optional[AsyncConnectionInterface] = None
64+
self._connection: AsyncConnectionInterface | None = None
6365
self._connect_failed: bool = False
6466
self._request_lock = AsyncLock()
6567
self._socket_options = socket_options
@@ -208,13 +210,13 @@ def __repr__(self) -> str:
208210
# These context managers are not used in the standard flow, but are
209211
# useful for testing or working with connection instances directly.
210212

211-
async def __aenter__(self) -> "AsyncHTTPConnection":
213+
async def __aenter__(self) -> AsyncHTTPConnection:
212214
return self
213215

214216
async def __aexit__(
215217
self,
216-
exc_type: Optional[Type[BaseException]] = None,
217-
exc_value: Optional[BaseException] = None,
218-
traceback: Optional[TracebackType] = None,
218+
exc_type: type[BaseException] | None = None,
219+
exc_value: BaseException | None = None,
220+
traceback: TracebackType | None = None,
219221
) -> None:
220222
await self.aclose()

httpcore/_async/connection_pool.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from __future__ import annotations
2+
13
import ssl
24
import sys
35
from types import TracebackType
4-
from typing import AsyncIterable, AsyncIterator, Iterable, List, Optional, Type
6+
from typing import AsyncIterable, AsyncIterator, Iterable
57

68
from .._backends.auto import AutoBackend
79
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
@@ -15,12 +17,10 @@
1517
class AsyncPoolRequest:
1618
def __init__(self, request: Request) -> None:
1719
self.request = request
18-
self.connection: Optional[AsyncConnectionInterface] = None
20+
self.connection: AsyncConnectionInterface | None = None
1921
self._connection_acquired = AsyncEvent()
2022

21-
def assign_to_connection(
22-
self, connection: Optional[AsyncConnectionInterface]
23-
) -> None:
23+
def assign_to_connection(self, connection: AsyncConnectionInterface | None) -> None:
2424
self.connection = connection
2525
self._connection_acquired.set()
2626

@@ -29,7 +29,7 @@ def clear_connection(self) -> None:
2929
self._connection_acquired = AsyncEvent()
3030

3131
async def wait_for_connection(
32-
self, timeout: Optional[float] = None
32+
self, timeout: float | None = None
3333
) -> AsyncConnectionInterface:
3434
if self.connection is None:
3535
await self._connection_acquired.wait(timeout=timeout)
@@ -47,17 +47,17 @@ class AsyncConnectionPool(AsyncRequestInterface):
4747

4848
def __init__(
4949
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,
5454
http1: bool = True,
5555
http2: bool = False,
5656
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,
6161
) -> None:
6262
"""
6363
A connection pool for making HTTP requests.
@@ -116,8 +116,8 @@ def __init__(
116116

117117
# The mutable state on a connection pool is the queue of incoming requests,
118118
# 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] = []
121121

122122
# We only mutate the state of the connection pool within an 'optional_thread_lock'
123123
# context. This holds a threading lock unless we're running in async mode,
@@ -139,7 +139,7 @@ def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
139139
)
140140

141141
@property
142-
def connections(self) -> List[AsyncConnectionInterface]:
142+
def connections(self) -> list[AsyncConnectionInterface]:
143143
"""
144144
Return a list of the connections currently in the pool.
145145
@@ -227,7 +227,7 @@ async def handle_async_request(self, request: Request) -> Response:
227227
extensions=response.extensions,
228228
)
229229

230-
def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
230+
def _assign_requests_to_connections(self) -> list[AsyncConnectionInterface]:
231231
"""
232232
Manage the state of the connection pool, assigning incoming
233233
requests to connections as available.
@@ -298,7 +298,7 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
298298

299299
return closing_connections
300300

301-
async def _close_connections(self, closing: List[AsyncConnectionInterface]) -> None:
301+
async def _close_connections(self, closing: list[AsyncConnectionInterface]) -> None:
302302
# Close connections which have been removed from the pool.
303303
with AsyncShieldCancellation():
304304
for connection in closing:
@@ -312,14 +312,14 @@ async def aclose(self) -> None:
312312
self._connections = []
313313
await self._close_connections(closing_connections)
314314

315-
async def __aenter__(self) -> "AsyncConnectionPool":
315+
async def __aenter__(self) -> AsyncConnectionPool:
316316
return self
317317

318318
async def __aexit__(
319319
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,
323323
) -> None:
324324
await self.aclose()
325325

httpcore/_async/http11.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
from __future__ import annotations
2+
13
import enum
24
import logging
35
import ssl
46
import time
57
from types import TracebackType
6-
from typing import (
7-
Any,
8-
AsyncIterable,
9-
AsyncIterator,
10-
List,
11-
Optional,
12-
Tuple,
13-
Type,
14-
Union,
15-
)
8+
from typing import Any, AsyncIterable, AsyncIterator, Union
169

1710
import h11
1811

@@ -55,12 +48,12 @@ def __init__(
5548
self,
5649
origin: Origin,
5750
stream: AsyncNetworkStream,
58-
keepalive_expiry: Optional[float] = None,
51+
keepalive_expiry: float | None = None,
5952
) -> None:
6053
self._origin = origin
6154
self._network_stream = stream
62-
self._keepalive_expiry: Optional[float] = keepalive_expiry
63-
self._expire_at: Optional[float] = None
55+
self._keepalive_expiry: float | None = keepalive_expiry
56+
self._expire_at: float | None = None
6457
self._state = HTTPConnectionState.NEW
6558
self._state_lock = AsyncLock()
6659
self._request_count = 0
@@ -167,9 +160,7 @@ async def _send_request_body(self, request: Request) -> None:
167160

168161
await self._send_event(h11.EndOfMessage(), timeout=timeout)
169162

170-
async def _send_event(
171-
self, event: h11.Event, timeout: Optional[float] = None
172-
) -> None:
163+
async def _send_event(self, event: h11.Event, timeout: float | None = None) -> None:
173164
bytes_to_send = self._h11_state.send(event)
174165
if bytes_to_send is not None:
175166
await self._network_stream.write(bytes_to_send, timeout=timeout)
@@ -178,7 +169,7 @@ async def _send_event(
178169

179170
async def _receive_response_headers(
180171
self, request: Request
181-
) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], bytes]:
172+
) -> tuple[bytes, int, bytes, list[tuple[bytes, bytes]], bytes]:
182173
timeouts = request.extensions.get("timeout", {})
183174
timeout = timeouts.get("read", None)
184175

@@ -214,8 +205,8 @@ async def _receive_response_body(self, request: Request) -> AsyncIterator[bytes]
214205
break
215206

216207
async def _receive_event(
217-
self, timeout: Optional[float] = None
218-
) -> Union[h11.Event, Type[h11.PAUSED]]:
208+
self, timeout: float | None = None
209+
) -> h11.Event | type[h11.PAUSED]:
219210
while True:
220211
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
221212
event = self._h11_state.next_event()
@@ -316,14 +307,14 @@ def __repr__(self) -> str:
316307
# These context managers are not used in the standard flow, but are
317308
# useful for testing or working with connection instances directly.
318309

319-
async def __aenter__(self) -> "AsyncHTTP11Connection":
310+
async def __aenter__(self) -> AsyncHTTP11Connection:
320311
return self
321312

322313
async def __aexit__(
323314
self,
324-
exc_type: Optional[Type[BaseException]] = None,
325-
exc_value: Optional[BaseException] = None,
326-
traceback: Optional[TracebackType] = None,
315+
exc_type: type[BaseException] | None = None,
316+
exc_value: BaseException | None = None,
317+
traceback: TracebackType | None = None,
327318
) -> None:
328319
await self.aclose()
329320

@@ -360,15 +351,15 @@ def __init__(self, stream: AsyncNetworkStream, leading_data: bytes) -> None:
360351
self._stream = stream
361352
self._leading_data = leading_data
362353

363-
async def read(self, max_bytes: int, timeout: Optional[float] = None) -> bytes:
354+
async def read(self, max_bytes: int, timeout: float | None = None) -> bytes:
364355
if self._leading_data:
365356
buffer = self._leading_data[:max_bytes]
366357
self._leading_data = self._leading_data[max_bytes:]
367358
return buffer
368359
else:
369360
return await self._stream.read(max_bytes, timeout)
370361

371-
async def write(self, buffer: bytes, timeout: Optional[float] = None) -> None:
362+
async def write(self, buffer: bytes, timeout: float | None = None) -> None:
372363
await self._stream.write(buffer, timeout)
373364

374365
async def aclose(self) -> None:
@@ -377,8 +368,8 @@ async def aclose(self) -> None:
377368
async def start_tls(
378369
self,
379370
ssl_context: ssl.SSLContext,
380-
server_hostname: Optional[str] = None,
381-
timeout: Optional[float] = None,
371+
server_hostname: str | None = None,
372+
timeout: float | None = None,
382373
) -> AsyncNetworkStream:
383374
return await self._stream.start_tls(ssl_context, server_hostname, timeout)
384375

0 commit comments

Comments
 (0)