Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to explicit typing.Optional for type hints #513

Merged
merged 3 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions httpcore/_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import contextmanager
from typing import Iterator, Union
from typing import Iterator, Optional, Union

from ._models import URL, Response
from ._sync.connection_pool import ConnectionPool
Expand All @@ -9,9 +9,9 @@ def request(
method: Union[bytes, str],
url: Union[URL, bytes, str],
*,
headers: Union[dict, list] = None,
content: Union[bytes, Iterator[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, Iterator[bytes], None] = None,
extensions: Optional[dict] = None,
) -> Response:
"""
Sends an HTTP request, returning the response.
Expand Down Expand Up @@ -50,9 +50,9 @@ def stream(
method: Union[bytes, str],
url: Union[URL, bytes, str],
*,
headers: Union[dict, list] = None,
content: Union[bytes, Iterator[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, Iterator[bytes], None] = None,
extensions: Optional[dict] = None,
) -> Iterator[Response]:
"""
Sends an HTTP request, returning the response within a content manager.
Expand Down
16 changes: 8 additions & 8 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class AsyncHTTPConnection(AsyncConnectionInterface):
def __init__(
self,
origin: Origin,
ssl_context: ssl.SSLContext = None,
keepalive_expiry: float = None,
ssl_context: Optional[ssl.SSLContext] = None,
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: str = None,
uds: str = None,
network_backend: AsyncNetworkBackend = None,
local_address: Optional[str] = None,
uds: Optional[str] = None,
network_backend: Optional[AsyncNetworkBackend] = None,
) -> None:
self._origin = origin
self._ssl_context = ssl_context
Expand Down Expand Up @@ -201,8 +201,8 @@ async def __aenter__(self) -> "AsyncHTTPConnection":

async def __aexit__(
self,
exc_type: Type[BaseException] = None,
exc_value: BaseException = None,
traceback: TracebackType = None,
exc_type: Optional[Type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None:
await self.aclose()
20 changes: 10 additions & 10 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def unset_connection(self) -> None:
self._connection_acquired = AsyncEvent()

async def wait_for_connection(
self, timeout: float = None
self, timeout: Optional[float] = None
) -> AsyncConnectionInterface:
await self._connection_acquired.wait(timeout=timeout)
assert self.connection is not None
Expand All @@ -43,16 +43,16 @@ class AsyncConnectionPool(AsyncRequestInterface):

def __init__(
self,
ssl_context: ssl.SSLContext = None,
ssl_context: Optional[ssl.SSLContext] = None,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
max_keepalive_connections: Optional[int] = None,
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: str = None,
uds: str = None,
network_backend: AsyncNetworkBackend = None,
local_address: Optional[str] = None,
uds: Optional[str] = None,
network_backend: Optional[AsyncNetworkBackend] = None,
) -> None:
"""
A connection pool for making HTTP requests.
Expand Down Expand Up @@ -319,9 +319,9 @@ async def __aenter__(self) -> "AsyncConnectionPool":

async def __aexit__(
self,
exc_type: Type[BaseException] = None,
exc_value: BaseException = None,
traceback: TracebackType = None,
exc_type: Optional[Type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None:
await self.aclose()

Expand Down
17 changes: 11 additions & 6 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class AsyncHTTP11Connection(AsyncConnectionInterface):
READ_NUM_BYTES = 64 * 1024

def __init__(
self, origin: Origin, stream: AsyncNetworkStream, keepalive_expiry: float = None
self,
origin: Origin,
stream: AsyncNetworkStream,
keepalive_expiry: Optional[float] = None,
) -> None:
self._origin = origin
self._network_stream = stream
Expand Down Expand Up @@ -127,7 +130,9 @@ async def _send_request_body(self, request: Request) -> None:
event = h11.EndOfMessage()
await self._send_event(event, timeout=timeout)

async def _send_event(self, event: H11Event, timeout: float = None) -> None:
async def _send_event(
self, event: H11Event, timeout: Optional[float] = None
) -> None:
bytes_to_send = self._h11_state.send(event)
await self._network_stream.write(bytes_to_send, timeout=timeout)

Expand Down Expand Up @@ -163,7 +168,7 @@ async def _receive_response_body(self, request: Request) -> AsyncIterator[bytes]
elif isinstance(event, (h11.EndOfMessage, h11.PAUSED)):
break

async def _receive_event(self, timeout: float = None) -> H11Event:
async def _receive_event(self, timeout: Optional[float] = None) -> H11Event:
while True:
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
event = self._h11_state.next_event()
Expand Down Expand Up @@ -268,9 +273,9 @@ async def __aenter__(self) -> "AsyncHTTP11Connection":

async def __aexit__(
self,
exc_type: Type[BaseException] = None,
exc_value: BaseException = None,
traceback: TracebackType = None,
exc_type: Optional[Type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None:
await self.aclose()

Expand Down
15 changes: 10 additions & 5 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class AsyncHTTP2Connection(AsyncConnectionInterface):
CONFIG = h2.config.H2Configuration(validate_inbound_headers=False)

def __init__(
self, origin: Origin, stream: AsyncNetworkStream, keepalive_expiry: float = None
self,
origin: Origin,
stream: AsyncNetworkStream,
keepalive_expiry: typing.Optional[float] = None,
):
self._origin = origin
self._network_stream = stream
Expand Down Expand Up @@ -263,7 +266,9 @@ async def _receive_stream_event(
raise RemoteProtocolError(event)
return event

async def _receive_events(self, request: Request, stream_id: int = None) -> None:
async def _receive_events(
self, request: Request, stream_id: typing.Optional[int] = None
) -> None:
async with self._read_lock:
if self._connection_error_event is not None: # pragma: nocover
raise RemoteProtocolError(self._connection_error_event)
Expand Down Expand Up @@ -431,9 +436,9 @@ async def __aenter__(self) -> "AsyncHTTP2Connection":

async def __aexit__(
self,
exc_type: typing.Type[BaseException] = None,
exc_value: BaseException = None,
traceback: types.TracebackType = None,
exc_type: typing.Optional[typing.Type[BaseException]] = None,
exc_value: typing.Optional[BaseException] = None,
traceback: typing.Optional[types.TracebackType] = None,
) -> None:
await self.aclose()

Expand Down
34 changes: 17 additions & 17 deletions httpcore/_async/http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


def merge_headers(
default_headers: Sequence[Tuple[bytes, bytes]] = None,
override_headers: Sequence[Tuple[bytes, bytes]] = None,
default_headers: Optional[Sequence[Tuple[bytes, bytes]]] = None,
override_headers: Optional[Sequence[Tuple[bytes, bytes]]] = None,
) -> List[Tuple[bytes, bytes]]:
"""
Append default_headers and override_headers, de-duplicating if a key exists
Expand Down Expand Up @@ -57,18 +57,18 @@ class AsyncHTTPProxy(AsyncConnectionPool):
def __init__(
self,
proxy_url: Union[URL, bytes, str],
proxy_auth: Tuple[Union[bytes, str], Union[bytes, str]] = None,
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence] = None,
ssl_context: ssl.SSLContext = None,
proxy_auth: Optional[Tuple[Union[bytes, str], Union[bytes, str]]] = None,
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence, None] = None,
ssl_context: Optional[ssl.SSLContext] = None,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
max_keepalive_connections: Optional[int] = None,
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: str = None,
uds: str = None,
network_backend: AsyncNetworkBackend = None,
local_address: Optional[str] = None,
uds: Optional[str] = None,
network_backend: Optional[AsyncNetworkBackend] = None,
) -> None:
"""
A connection pool for making HTTP requests.
Expand Down Expand Up @@ -151,9 +151,9 @@ class AsyncForwardHTTPConnection(AsyncConnectionInterface):
def __init__(
self,
proxy_origin: Origin,
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence] = None,
keepalive_expiry: float = None,
network_backend: AsyncNetworkBackend = None,
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence, None] = None,
keepalive_expiry: Optional[float] = None,
network_backend: Optional[AsyncNetworkBackend] = None,
) -> None:
self._connection = AsyncHTTPConnection(
origin=proxy_origin,
Expand Down Expand Up @@ -210,12 +210,12 @@ def __init__(
self,
proxy_origin: Origin,
remote_origin: Origin,
ssl_context: ssl.SSLContext = None,
proxy_headers: Sequence[Tuple[bytes, bytes]] = None,
keepalive_expiry: float = None,
ssl_context: Optional[ssl.SSLContext] = None,
proxy_headers: Optional[Sequence[Tuple[bytes, bytes]]] = None,
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
network_backend: AsyncNetworkBackend = None,
network_backend: Optional[AsyncNetworkBackend] = None,
) -> None:
self._connection: AsyncConnectionInterface = AsyncHTTPConnection(
origin=proxy_origin,
Expand Down
14 changes: 7 additions & 7 deletions httpcore/_async/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import AsyncIterator, Union
from typing import AsyncIterator, Optional, Union

from .._compat import asynccontextmanager
from .._models import (
Expand All @@ -19,9 +19,9 @@ async def request(
method: Union[bytes, str],
url: Union[URL, bytes, str],
*,
headers: Union[dict, list] = None,
content: Union[bytes, AsyncIterator[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, AsyncIterator[bytes], None] = None,
extensions: Optional[dict] = None,
) -> Response:
# Strict type checking on our parameters.
method = enforce_bytes(method, name="method")
Expand Down Expand Up @@ -51,9 +51,9 @@ async def stream(
method: Union[bytes, str],
url: Union[URL, bytes, str],
*,
headers: Union[dict, list] = None,
content: Union[bytes, AsyncIterator[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, AsyncIterator[bytes], None] = None,
extensions: Optional[dict] = None,
) -> AsyncIterator[Response]:
# Strict type checking on our parameters.
method = enforce_bytes(method, name="method")
Expand Down
22 changes: 11 additions & 11 deletions httpcore/_async/socks_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def _init_socks5_connection(
*,
host: bytes,
port: int,
auth: typing.Tuple[bytes, bytes] = None,
auth: typing.Optional[typing.Tuple[bytes, bytes]] = None,
) -> None:
conn = socks5.SOCKS5Connection()

Expand Down Expand Up @@ -105,16 +105,16 @@ class AsyncSOCKSProxy(AsyncConnectionPool):
def __init__(
self,
proxy_url: typing.Union[URL, bytes, str],
proxy_auth: typing.Tuple[
typing.Union[bytes, str], typing.Union[bytes, str]
proxy_auth: typing.Optional[
typing.Tuple[typing.Union[bytes, str], typing.Union[bytes, str]]
] = None,
ssl_context: ssl.SSLContext = None,
ssl_context: typing.Optional[ssl.SSLContext] = None,
max_connections: typing.Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
max_keepalive_connections: typing.Optional[int] = None,
keepalive_expiry: typing.Optional[float] = None,
http1: bool = True,
http2: bool = False,
network_backend: AsyncNetworkBackend = None,
network_backend: typing.Optional[AsyncNetworkBackend] = None,
) -> None:
"""
A connection pool for making HTTP requests.
Expand Down Expand Up @@ -186,12 +186,12 @@ def __init__(
self,
proxy_origin: Origin,
remote_origin: Origin,
proxy_auth: typing.Tuple[bytes, bytes] = None,
ssl_context: ssl.SSLContext = None,
keepalive_expiry: float = None,
proxy_auth: typing.Optional[typing.Tuple[bytes, bytes]] = None,
ssl_context: typing.Optional[ssl.SSLContext] = None,
keepalive_expiry: typing.Optional[float] = None,
http1: bool = True,
http2: bool = False,
network_backend: AsyncNetworkBackend = None,
network_backend: typing.Optional[AsyncNetworkBackend] = None,
) -> None:
self._proxy_origin = proxy_origin
self._remote_origin = remote_origin
Expand Down
14 changes: 7 additions & 7 deletions httpcore/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def enforce_url(value: Union["URL", bytes, str], *, name: str) -> "URL":


def enforce_headers(
value: Union[HeadersAsMapping, HeadersAsSequence] = None, *, name: str
value: Union[HeadersAsMapping, HeadersAsSequence, None] = None, *, name: str
) -> List[Tuple[bytes, bytes]]:
"""
Convienence function that ensure all items in request or response headers
Expand Down Expand Up @@ -318,9 +318,9 @@ def __init__(
method: Union[bytes, str],
url: Union[URL, bytes, str],
*,
headers: Union[dict, list] = None,
content: Union[bytes, Iterable[bytes], AsyncIterable[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, Iterable[bytes], AsyncIterable[bytes], None] = None,
extensions: Optional[dict] = None,
) -> None:
"""
Parameters:
Expand Down Expand Up @@ -356,9 +356,9 @@ def __init__(
self,
status: int,
*,
headers: Union[dict, list] = None,
content: Union[bytes, Iterable[bytes], AsyncIterable[bytes]] = None,
extensions: dict = None,
headers: Union[dict, list, None] = None,
content: Union[bytes, Iterable[bytes], AsyncIterable[bytes], None] = None,
extensions: Optional[dict] = None,
) -> None:
"""
Parameters:
Expand Down