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

Update types for h11 v0.14 #579

Merged
merged 20 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
34 changes: 22 additions & 12 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import enum
import time
from types import TracebackType
from typing import AsyncIterable, AsyncIterator, List, Optional, Tuple, Type, Union
from typing import (
AsyncIterable,
AsyncIterator,
List,
Optional,
Tuple,
Type,
Union,
cast,
)

import h11

Expand All @@ -17,16 +26,14 @@
from ..backends.base import AsyncNetworkStream
from .interfaces import AsyncConnectionInterface

H11Event = Union[

zanieb marked this conversation as resolved.
Show resolved Hide resolved
# A subset of `h11.Event` types supported by `_send_event`
H11SendEvent = Union[
h11.Request,
h11.Response,
h11.InformationalResponse,
h11.Data,
h11.EndOfMessage,
h11.ConnectionClosed,
]


class HTTPConnectionState(enum.IntEnum):
NEW = 0
ACTIVE = 1
Expand Down Expand Up @@ -127,14 +134,14 @@ async def _send_request_body(self, request: Request) -> None:
event = h11.Data(data=chunk)
await self._send_event(event, timeout=timeout)

event = h11.EndOfMessage()
await self._send_event(event, timeout=timeout)
await self._send_event(h11.EndOfMessage(), timeout=timeout)

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

# Receiving the response...

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

async def _receive_event(self, timeout: Optional[float] = None) -> H11Event:
async def _receive_event(
self, timeout: Optional[float] = None
) -> Union[h11.Event, Type[h11.PAUSED]]:
while True:
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
event = self._h11_state.next_event()
Expand All @@ -192,7 +201,8 @@ async def _receive_event(self, timeout: Optional[float] = None) -> H11Event:

self._h11_state.receive_data(data)
else:
return event
# mypy fails to narrow the type in the above if statement above
return cast(Union[h11.Event, Type[h11.PAUSED]], event)

async def _response_closed(self) -> None:
async with self._state_lock:
Expand Down
35 changes: 23 additions & 12 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import enum
import time
from types import TracebackType
from typing import Iterable, Iterator, List, Optional, Tuple, Type, Union
from typing import (
Iterable,
Iterator,
List,
Optional,
Tuple,
Type,
Union,
cast,
)

import h11

Expand All @@ -17,13 +26,12 @@
from ..backends.base import NetworkStream
from .interfaces import ConnectionInterface

H11Event = Union[

zanieb marked this conversation as resolved.
Show resolved Hide resolved
# A subset of `h11.Event` types supported by `_send_event`
H11SendEvent = Union[
h11.Request,
h11.Response,
h11.InformationalResponse,
h11.Data,
h11.EndOfMessage,
h11.ConnectionClosed,
]


Expand Down Expand Up @@ -127,14 +135,14 @@ def _send_request_body(self, request: Request) -> None:
event = h11.Data(data=chunk)
self._send_event(event, timeout=timeout)

event = h11.EndOfMessage()
self._send_event(event, timeout=timeout)
self._send_event(h11.EndOfMessage(), timeout=timeout)

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

# Receiving the response...

Expand Down Expand Up @@ -168,10 +176,12 @@ def _receive_response_body(self, request: Request) -> Iterator[bytes]:
elif isinstance(event, (h11.EndOfMessage, h11.PAUSED)):
break

def _receive_event(self, timeout: Optional[float] = None) -> H11Event:
def _receive_event(
self, timeout: Optional[float] = None
) -> Union[h11.Event, Type[h11.PAUSED]]:
while True:
with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}):
event = self._h11_state.next_event()
event = self._h11_state.next_event()

if event is h11.NEED_DATA:
data = self._network_stream.read(
Expand All @@ -192,7 +202,8 @@ def _receive_event(self, timeout: Optional[float] = None) -> H11Event:

self._h11_state.receive_data(data)
else:
return event
# mypy fails to narrow the type in the above if statement above
return cast(Union[h11.Event, Type[h11.PAUSED]], event)

def _response_closed(self) -> None:
with self._state_lock:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_packages(package):
include_package_data=True,
zip_safe=False,
install_requires=[
"h11>=0.11,<0.13",
"h11>=0.13,<0.15",
"sniffio==1.*",
"anyio==3.*",
"certifi",
Expand Down