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

Enable type checking for Trio #69

Merged
merged 1 commit into from
May 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ssl import SSLContext
from typing import Optional, Union
from typing import Optional

import trio

Expand All @@ -22,7 +22,7 @@ def none_as_inf(value: Optional[float]) -> float:


class SocketStream(AsyncSocketStream):
def __init__(self, stream: Union[trio.SocketStream, trio.SSLStream]) -> None:
def __init__(self, stream: trio.abc.Stream) -> None:
self.stream = stream
self.read_lock = trio.Lock()
self.write_lock = trio.Lock()
Expand All @@ -43,7 +43,9 @@ async def start_tls(
trio.BrokenResourceError: ConnectError,
}
ssl_stream = trio.SSLStream(
self.stream, ssl_context=ssl_context, server_hostname=hostname
self.stream,
ssl_context=ssl_context,
server_hostname=hostname.decode("ascii"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't actually a bug I think because the SSLContext always encodes the hostname:
https://github.com/python/cpython/blob/7b3ab5921fa25ed8b97b6296f97c5c78aacf5447/Lib/ssl.py#L486-L492

...but lot's of things (including the typeshed definitions of the stdlib SSLContext) type the hostname as Optional[str]. And we decode with ascii in the asyncio backend as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fair enough 👍

)

with map_exceptions(exc_map):
Expand Down Expand Up @@ -85,7 +87,7 @@ def is_connection_dropped(self) -> bool:
stream = self.stream

# Peek through any SSLStream wrappers to get the underlying SocketStream.
while hasattr(stream, "transport_stream"):
while isinstance(stream, trio.SSLStream):
stream = stream.transport_stream
assert isinstance(stream, trio.SocketStream)

Expand Down Expand Up @@ -147,11 +149,11 @@ async def open_tcp_stream(

with map_exceptions(exc_map):
with trio.fail_after(connect_timeout):
stream: trio.SocketStream = await trio.open_tcp_stream(hostname, port)
stream: trio.abc.Stream = await trio.open_tcp_stream(hostname, port)

if ssl_context is not None:
stream = trio.SSLStream(
stream, ssl_context, server_hostname=hostname
stream, ssl_context, server_hostname=hostname.decode("ascii")
)
await stream.do_handshake()

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Optionals
trio
trio-typing

# Docs
mkdocs
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ max-line-length = 88
[mypy]
disallow_untyped_defs = True
ignore_missing_imports = True
plugins = trio_typing.plugin

[tool:isort]
combine_as_imports = True
Expand Down