Skip to content

Commit 4e0a17c

Browse files
authored
Consistent import style (#970)
1 parent 127505b commit 4e0a17c

24 files changed

+199
-195
lines changed

httpcore/_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3-
from contextlib import contextmanager
4-
from typing import Iterator
3+
import contextlib
4+
import typing
55

66
from ._models import URL, Extensions, HeaderTypes, Response
77
from ._sync.connection_pool import ConnectionPool
@@ -12,7 +12,7 @@ def request(
1212
url: URL | bytes | str,
1313
*,
1414
headers: HeaderTypes = None,
15-
content: bytes | Iterator[bytes] | None = None,
15+
content: bytes | typing.Iterator[bytes] | None = None,
1616
extensions: Extensions | None = None,
1717
) -> Response:
1818
"""
@@ -47,15 +47,15 @@ def request(
4747
)
4848

4949

50-
@contextmanager
50+
@contextlib.contextmanager
5151
def stream(
5252
method: bytes | str,
5353
url: URL | bytes | str,
5454
*,
5555
headers: HeaderTypes = None,
56-
content: bytes | Iterator[bytes] | None = None,
56+
content: bytes | typing.Iterator[bytes] | None = None,
5757
extensions: Extensions | None = None,
58-
) -> Iterator[Response]:
58+
) -> typing.Iterator[Response]:
5959
"""
6060
Sends an HTTP request, returning the response within a content manager.
6161

httpcore/_async/connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import itertools
44
import logging
55
import ssl
6-
from types import TracebackType
7-
from typing import Iterable, Iterator
6+
import types
7+
import typing
88

99
from .._backends.auto import AutoBackend
1010
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream
@@ -22,7 +22,7 @@
2222
logger = logging.getLogger("httpcore.connection")
2323

2424

25-
def exponential_backoff(factor: float) -> Iterator[float]:
25+
def exponential_backoff(factor: float) -> typing.Iterator[float]:
2626
"""
2727
Generate a geometric sequence that has a ratio of 2 and starts with 0.
2828
@@ -47,7 +47,7 @@ def __init__(
4747
local_address: str | None = None,
4848
uds: str | None = None,
4949
network_backend: AsyncNetworkBackend | None = None,
50-
socket_options: Iterable[SOCKET_OPTION] | None = None,
50+
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
5151
) -> None:
5252
self._origin = origin
5353
self._ssl_context = ssl_context
@@ -217,6 +217,6 @@ async def __aexit__(
217217
self,
218218
exc_type: type[BaseException] | None = None,
219219
exc_value: BaseException | None = None,
220-
traceback: TracebackType | None = None,
220+
traceback: types.TracebackType | None = None,
221221
) -> None:
222222
await self.aclose()

httpcore/_async/connection_pool.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import ssl
44
import sys
5-
from types import TracebackType
6-
from typing import AsyncIterable, AsyncIterator, Iterable
5+
import types
6+
import typing
77

88
from .._backends.auto import AutoBackend
99
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
@@ -57,7 +57,7 @@ def __init__(
5757
local_address: str | None = None,
5858
uds: str | None = None,
5959
network_backend: AsyncNetworkBackend | None = None,
60-
socket_options: Iterable[SOCKET_OPTION] | None = None,
60+
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
6161
) -> None:
6262
"""
6363
A connection pool for making HTTP requests.
@@ -217,7 +217,7 @@ async def handle_async_request(self, request: Request) -> Response:
217217

218218
# Return the response. Note that in this case we still have to manage
219219
# the point at which the response is closed.
220-
assert isinstance(response.stream, AsyncIterable)
220+
assert isinstance(response.stream, typing.AsyncIterable)
221221
return Response(
222222
status=response.status,
223223
headers=response.headers,
@@ -319,7 +319,7 @@ async def __aexit__(
319319
self,
320320
exc_type: type[BaseException] | None = None,
321321
exc_value: BaseException | None = None,
322-
traceback: TracebackType | None = None,
322+
traceback: types.TracebackType | None = None,
323323
) -> None:
324324
await self.aclose()
325325

@@ -349,7 +349,7 @@ def __repr__(self) -> str:
349349
class PoolByteStream:
350350
def __init__(
351351
self,
352-
stream: AsyncIterable[bytes],
352+
stream: typing.AsyncIterable[bytes],
353353
pool_request: AsyncPoolRequest,
354354
pool: AsyncConnectionPool,
355355
) -> None:
@@ -358,7 +358,7 @@ def __init__(
358358
self._pool = pool
359359
self._closed = False
360360

361-
async def __aiter__(self) -> AsyncIterator[bytes]:
361+
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
362362
try:
363363
async for part in self._stream:
364364
yield part

httpcore/_async/http11.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import logging
55
import ssl
66
import time
7-
from types import TracebackType
8-
from typing import Any, AsyncIterable, AsyncIterator, Union
7+
import types
8+
import typing
99

1010
import h11
1111

@@ -26,7 +26,7 @@
2626

2727

2828
# A subset of `h11.Event` types supported by `_send_event`
29-
H11SendEvent = Union[
29+
H11SendEvent = typing.Union[
3030
h11.Request,
3131
h11.Data,
3232
h11.EndOfMessage,
@@ -153,7 +153,7 @@ async def _send_request_body(self, request: Request) -> None:
153153
timeouts = request.extensions.get("timeout", {})
154154
timeout = timeouts.get("write", None)
155155

156-
assert isinstance(request.stream, AsyncIterable)
156+
assert isinstance(request.stream, typing.AsyncIterable)
157157
async for chunk in request.stream:
158158
event = h11.Data(data=chunk)
159159
await self._send_event(event, timeout=timeout)
@@ -193,7 +193,9 @@ async def _receive_response_headers(
193193

194194
return http_version, event.status_code, event.reason, headers, trailing_data
195195

196-
async def _receive_response_body(self, request: Request) -> AsyncIterator[bytes]:
196+
async def _receive_response_body(
197+
self, request: Request
198+
) -> typing.AsyncIterator[bytes]:
197199
timeouts = request.extensions.get("timeout", {})
198200
timeout = timeouts.get("read", None)
199201

@@ -314,7 +316,7 @@ async def __aexit__(
314316
self,
315317
exc_type: type[BaseException] | None = None,
316318
exc_value: BaseException | None = None,
317-
traceback: TracebackType | None = None,
319+
traceback: types.TracebackType | None = None,
318320
) -> None:
319321
await self.aclose()
320322

@@ -325,7 +327,7 @@ def __init__(self, connection: AsyncHTTP11Connection, request: Request) -> None:
325327
self._request = request
326328
self._closed = False
327329

328-
async def __aiter__(self) -> AsyncIterator[bytes]:
330+
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
329331
kwargs = {"request": self._request}
330332
try:
331333
async with Trace("receive_response_body", logger, self._request, kwargs):
@@ -373,5 +375,5 @@ async def start_tls(
373375
) -> AsyncNetworkStream:
374376
return await self._stream.start_tls(ssl_context, server_hostname, timeout)
375377

376-
def get_extra_info(self, info: str) -> Any:
378+
def get_extra_info(self, info: str) -> typing.Any:
377379
return self._stream.get_extra_info(info)

httpcore/_async/http2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import time
66
import types
7-
from typing import AsyncIterable, AsyncIterator
7+
import typing
88

99
import h2.config
1010
import h2.connection
@@ -253,7 +253,7 @@ async def _send_request_body(self, request: Request, stream_id: int) -> None:
253253
if not has_body_headers(request):
254254
return
255255

256-
assert isinstance(request.stream, AsyncIterable)
256+
assert isinstance(request.stream, typing.AsyncIterable)
257257
async for data in request.stream:
258258
await self._send_stream_data(request, stream_id, data)
259259
await self._send_end_stream(request, stream_id)
@@ -303,7 +303,7 @@ async def _receive_response(
303303

304304
async def _receive_response_body(
305305
self, request: Request, stream_id: int
306-
) -> AsyncIterator[bytes]:
306+
) -> typing.AsyncIterator[bytes]:
307307
"""
308308
Iterator that returns the bytes of the response body for a given stream ID.
309309
"""
@@ -559,7 +559,7 @@ def __init__(
559559
self._stream_id = stream_id
560560
self._closed = False
561561

562-
async def __aiter__(self) -> AsyncIterator[bytes]:
562+
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
563563
kwargs = {"request": self._request, "stream_id": self._stream_id}
564564
try:
565565
async with Trace("receive_response_body", logger, self._request, kwargs):

httpcore/_async/http_proxy.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3+
import base64
34
import logging
45
import ssl
5-
from base64 import b64encode
6-
from typing import Iterable, Mapping, Sequence, Tuple, Union
6+
import typing
77

88
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
99
from .._exceptions import ProxyError
@@ -24,16 +24,17 @@
2424
from .http11 import AsyncHTTP11Connection
2525
from .interfaces import AsyncConnectionInterface
2626

27-
HeadersAsSequence = Sequence[Tuple[Union[bytes, str], Union[bytes, str]]]
28-
HeadersAsMapping = Mapping[Union[bytes, str], Union[bytes, str]]
27+
ByteOrStr = typing.Union[bytes, str]
28+
HeadersAsSequence = typing.Sequence[typing.Tuple[ByteOrStr, ByteOrStr]]
29+
HeadersAsMapping = typing.Mapping[ByteOrStr, ByteOrStr]
2930

3031

3132
logger = logging.getLogger("httpcore.proxy")
3233

3334

3435
def merge_headers(
35-
default_headers: Sequence[tuple[bytes, bytes]] | None = None,
36-
override_headers: Sequence[tuple[bytes, bytes]] | None = None,
36+
default_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
37+
override_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
3738
) -> list[tuple[bytes, bytes]]:
3839
"""
3940
Append default_headers and override_headers, de-duplicating if a key exists
@@ -52,7 +53,7 @@ def merge_headers(
5253

5354
def build_auth_header(username: bytes, password: bytes) -> bytes:
5455
userpass = username + b":" + password
55-
return b"Basic " + b64encode(userpass)
56+
return b"Basic " + base64.b64encode(userpass)
5657

5758

5859
class AsyncHTTPProxy(AsyncConnectionPool):
@@ -76,7 +77,7 @@ def __init__(
7677
local_address: str | None = None,
7778
uds: str | None = None,
7879
network_backend: AsyncNetworkBackend | None = None,
79-
socket_options: Iterable[SOCKET_OPTION] | None = None,
80+
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
8081
) -> None:
8182
"""
8283
A connection pool for making HTTP requests.
@@ -177,7 +178,7 @@ def __init__(
177178
proxy_headers: HeadersAsMapping | HeadersAsSequence | None = None,
178179
keepalive_expiry: float | None = None,
179180
network_backend: AsyncNetworkBackend | None = None,
180-
socket_options: Iterable[SOCKET_OPTION] | None = None,
181+
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
181182
proxy_ssl_context: ssl.SSLContext | None = None,
182183
) -> None:
183184
self._connection = AsyncHTTPConnection(
@@ -240,12 +241,12 @@ def __init__(
240241
remote_origin: Origin,
241242
ssl_context: ssl.SSLContext | None = None,
242243
proxy_ssl_context: ssl.SSLContext | None = None,
243-
proxy_headers: Sequence[tuple[bytes, bytes]] | None = None,
244+
proxy_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
244245
keepalive_expiry: float | None = None,
245246
http1: bool = True,
246247
http2: bool = False,
247248
network_backend: AsyncNetworkBackend | None = None,
248-
socket_options: Iterable[SOCKET_OPTION] | None = None,
249+
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
249250
) -> None:
250251
self._connection: AsyncConnectionInterface = AsyncHTTPConnection(
251252
origin=proxy_origin,

httpcore/_async/interfaces.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3-
from contextlib import asynccontextmanager
4-
from typing import AsyncIterator
3+
import contextlib
4+
import typing
55

66
from .._models import (
77
URL,
@@ -24,7 +24,7 @@ async def request(
2424
url: URL | bytes | str,
2525
*,
2626
headers: HeaderTypes = None,
27-
content: bytes | AsyncIterator[bytes] | None = None,
27+
content: bytes | typing.AsyncIterator[bytes] | None = None,
2828
extensions: Extensions | None = None,
2929
) -> Response:
3030
# Strict type checking on our parameters.
@@ -49,16 +49,16 @@ async def request(
4949
await response.aclose()
5050
return response
5151

52-
@asynccontextmanager
52+
@contextlib.asynccontextmanager
5353
async def stream(
5454
self,
5555
method: bytes | str,
5656
url: URL | bytes | str,
5757
*,
5858
headers: HeaderTypes = None,
59-
content: bytes | AsyncIterator[bytes] | None = None,
59+
content: bytes | typing.AsyncIterator[bytes] | None = None,
6060
extensions: Extensions | None = None,
61-
) -> AsyncIterator[Response]:
61+
) -> typing.AsyncIterator[Response]:
6262
# Strict type checking on our parameters.
6363
method = enforce_bytes(method, name="method")
6464
url = enforce_url(url, name="url")

0 commit comments

Comments
 (0)