Skip to content

Commit

Permalink
Consistent import style (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrquan authored Oct 16, 2024
1 parent 127505b commit 4e0a17c
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 195 deletions.
12 changes: 6 additions & 6 deletions httpcore/_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from contextlib import contextmanager
from typing import Iterator
import contextlib
import typing

from ._models import URL, Extensions, HeaderTypes, Response
from ._sync.connection_pool import ConnectionPool
Expand All @@ -12,7 +12,7 @@ def request(
url: URL | bytes | str,
*,
headers: HeaderTypes = None,
content: bytes | Iterator[bytes] | None = None,
content: bytes | typing.Iterator[bytes] | None = None,
extensions: Extensions | None = None,
) -> Response:
"""
Expand Down Expand Up @@ -47,15 +47,15 @@ def request(
)


@contextmanager
@contextlib.contextmanager
def stream(
method: bytes | str,
url: URL | bytes | str,
*,
headers: HeaderTypes = None,
content: bytes | Iterator[bytes] | None = None,
content: bytes | typing.Iterator[bytes] | None = None,
extensions: Extensions | None = None,
) -> Iterator[Response]:
) -> typing.Iterator[Response]:
"""
Sends an HTTP request, returning the response within a content manager.
Expand Down
10 changes: 5 additions & 5 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import itertools
import logging
import ssl
from types import TracebackType
from typing import Iterable, Iterator
import types
import typing

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


def exponential_backoff(factor: float) -> Iterator[float]:
def exponential_backoff(factor: float) -> typing.Iterator[float]:
"""
Generate a geometric sequence that has a ratio of 2 and starts with 0.
Expand All @@ -47,7 +47,7 @@ def __init__(
local_address: str | None = None,
uds: str | None = None,
network_backend: AsyncNetworkBackend | None = None,
socket_options: Iterable[SOCKET_OPTION] | None = None,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
) -> None:
self._origin = origin
self._ssl_context = ssl_context
Expand Down Expand Up @@ -217,6 +217,6 @@ async def __aexit__(
self,
exc_type: type[BaseException] | None = None,
exc_value: BaseException | None = None,
traceback: TracebackType | None = None,
traceback: types.TracebackType | None = None,
) -> None:
await self.aclose()
14 changes: 7 additions & 7 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import ssl
import sys
from types import TracebackType
from typing import AsyncIterable, AsyncIterator, Iterable
import types
import typing

from .._backends.auto import AutoBackend
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(
local_address: str | None = None,
uds: str | None = None,
network_backend: AsyncNetworkBackend | None = None,
socket_options: Iterable[SOCKET_OPTION] | None = None,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
) -> None:
"""
A connection pool for making HTTP requests.
Expand Down Expand Up @@ -217,7 +217,7 @@ async def handle_async_request(self, request: Request) -> Response:

# Return the response. Note that in this case we still have to manage
# the point at which the response is closed.
assert isinstance(response.stream, AsyncIterable)
assert isinstance(response.stream, typing.AsyncIterable)
return Response(
status=response.status,
headers=response.headers,
Expand Down Expand Up @@ -319,7 +319,7 @@ async def __aexit__(
self,
exc_type: type[BaseException] | None = None,
exc_value: BaseException | None = None,
traceback: TracebackType | None = None,
traceback: types.TracebackType | None = None,
) -> None:
await self.aclose()

Expand Down Expand Up @@ -349,7 +349,7 @@ def __repr__(self) -> str:
class PoolByteStream:
def __init__(
self,
stream: AsyncIterable[bytes],
stream: typing.AsyncIterable[bytes],
pool_request: AsyncPoolRequest,
pool: AsyncConnectionPool,
) -> None:
Expand All @@ -358,7 +358,7 @@ def __init__(
self._pool = pool
self._closed = False

async def __aiter__(self) -> AsyncIterator[bytes]:
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
try:
async for part in self._stream:
yield part
Expand Down
18 changes: 10 additions & 8 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import logging
import ssl
import time
from types import TracebackType
from typing import Any, AsyncIterable, AsyncIterator, Union
import types
import typing

import h11

Expand All @@ -26,7 +26,7 @@


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

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

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

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

Expand Down Expand Up @@ -314,7 +316,7 @@ async def __aexit__(
self,
exc_type: type[BaseException] | None = None,
exc_value: BaseException | None = None,
traceback: TracebackType | None = None,
traceback: types.TracebackType | None = None,
) -> None:
await self.aclose()

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

async def __aiter__(self) -> AsyncIterator[bytes]:
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
kwargs = {"request": self._request}
try:
async with Trace("receive_response_body", logger, self._request, kwargs):
Expand Down Expand Up @@ -373,5 +375,5 @@ async def start_tls(
) -> AsyncNetworkStream:
return await self._stream.start_tls(ssl_context, server_hostname, timeout)

def get_extra_info(self, info: str) -> Any:
def get_extra_info(self, info: str) -> typing.Any:
return self._stream.get_extra_info(info)
8 changes: 4 additions & 4 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import time
import types
from typing import AsyncIterable, AsyncIterator
import typing

import h2.config
import h2.connection
Expand Down Expand Up @@ -253,7 +253,7 @@ async def _send_request_body(self, request: Request, stream_id: int) -> None:
if not has_body_headers(request):
return

assert isinstance(request.stream, AsyncIterable)
assert isinstance(request.stream, typing.AsyncIterable)
async for data in request.stream:
await self._send_stream_data(request, stream_id, data)
await self._send_end_stream(request, stream_id)
Expand Down Expand Up @@ -303,7 +303,7 @@ async def _receive_response(

async def _receive_response_body(
self, request: Request, stream_id: int
) -> AsyncIterator[bytes]:
) -> typing.AsyncIterator[bytes]:
"""
Iterator that returns the bytes of the response body for a given stream ID.
"""
Expand Down Expand Up @@ -559,7 +559,7 @@ def __init__(
self._stream_id = stream_id
self._closed = False

async def __aiter__(self) -> AsyncIterator[bytes]:
async def __aiter__(self) -> typing.AsyncIterator[bytes]:
kwargs = {"request": self._request, "stream_id": self._stream_id}
try:
async with Trace("receive_response_body", logger, self._request, kwargs):
Expand Down
23 changes: 12 additions & 11 deletions httpcore/_async/http_proxy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import base64
import logging
import ssl
from base64 import b64encode
from typing import Iterable, Mapping, Sequence, Tuple, Union
import typing

from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
from .._exceptions import ProxyError
Expand All @@ -24,16 +24,17 @@
from .http11 import AsyncHTTP11Connection
from .interfaces import AsyncConnectionInterface

HeadersAsSequence = Sequence[Tuple[Union[bytes, str], Union[bytes, str]]]
HeadersAsMapping = Mapping[Union[bytes, str], Union[bytes, str]]
ByteOrStr = typing.Union[bytes, str]
HeadersAsSequence = typing.Sequence[typing.Tuple[ByteOrStr, ByteOrStr]]
HeadersAsMapping = typing.Mapping[ByteOrStr, ByteOrStr]


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


def merge_headers(
default_headers: Sequence[tuple[bytes, bytes]] | None = None,
override_headers: Sequence[tuple[bytes, bytes]] | None = None,
default_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
override_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
) -> list[tuple[bytes, bytes]]:
"""
Append default_headers and override_headers, de-duplicating if a key exists
Expand All @@ -52,7 +53,7 @@ def merge_headers(

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


class AsyncHTTPProxy(AsyncConnectionPool):
Expand All @@ -76,7 +77,7 @@ def __init__(
local_address: str | None = None,
uds: str | None = None,
network_backend: AsyncNetworkBackend | None = None,
socket_options: Iterable[SOCKET_OPTION] | None = None,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
) -> None:
"""
A connection pool for making HTTP requests.
Expand Down Expand Up @@ -177,7 +178,7 @@ def __init__(
proxy_headers: HeadersAsMapping | HeadersAsSequence | None = None,
keepalive_expiry: float | None = None,
network_backend: AsyncNetworkBackend | None = None,
socket_options: Iterable[SOCKET_OPTION] | None = None,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
proxy_ssl_context: ssl.SSLContext | None = None,
) -> None:
self._connection = AsyncHTTPConnection(
Expand Down Expand Up @@ -240,12 +241,12 @@ def __init__(
remote_origin: Origin,
ssl_context: ssl.SSLContext | None = None,
proxy_ssl_context: ssl.SSLContext | None = None,
proxy_headers: Sequence[tuple[bytes, bytes]] | None = None,
proxy_headers: typing.Sequence[tuple[bytes, bytes]] | None = None,
keepalive_expiry: float | None = None,
http1: bool = True,
http2: bool = False,
network_backend: AsyncNetworkBackend | None = None,
socket_options: Iterable[SOCKET_OPTION] | None = None,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
) -> None:
self._connection: AsyncConnectionInterface = AsyncHTTPConnection(
origin=proxy_origin,
Expand Down
12 changes: 6 additions & 6 deletions httpcore/_async/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from contextlib import asynccontextmanager
from typing import AsyncIterator
import contextlib
import typing

from .._models import (
URL,
Expand All @@ -24,7 +24,7 @@ async def request(
url: URL | bytes | str,
*,
headers: HeaderTypes = None,
content: bytes | AsyncIterator[bytes] | None = None,
content: bytes | typing.AsyncIterator[bytes] | None = None,
extensions: Extensions | None = None,
) -> Response:
# Strict type checking on our parameters.
Expand All @@ -49,16 +49,16 @@ async def request(
await response.aclose()
return response

@asynccontextmanager
@contextlib.asynccontextmanager
async def stream(
self,
method: bytes | str,
url: URL | bytes | str,
*,
headers: HeaderTypes = None,
content: bytes | AsyncIterator[bytes] | None = None,
content: bytes | typing.AsyncIterator[bytes] | None = None,
extensions: Extensions | None = None,
) -> AsyncIterator[Response]:
) -> typing.AsyncIterator[Response]:
# Strict type checking on our parameters.
method = enforce_bytes(method, name="method")
url = enforce_url(url, name="url")
Expand Down
Loading

0 comments on commit 4e0a17c

Please sign in to comment.