Skip to content

Commit 84d06b8

Browse files
jakkdlpgjones
authored andcommitted
Improve typing against Trio
1 parent 4cf3528 commit 84d06b8

File tree

13 files changed

+80
-46
lines changed

13 files changed

+80
-46
lines changed

pyproject.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,16 @@ warn_unused_configs = true
9292
warn_unused_ignores = true
9393

9494
[[tool.mypy.overrides]]
95-
module =["aioquic.*", "cryptography.*", "h11.*", "h2.*", "priority.*", "pytest_asyncio.*", "trio.*", "uvloop.*"]
95+
module =["aioquic.*", "cryptography.*", "h11.*", "h2.*", "priority.*", "pytest_asyncio.*", "uvloop.*"]
9696
ignore_missing_imports = true
9797

98+
[[tool.mypy.overrides]]
99+
module = ["trio.*", "tests.trio.*"]
100+
disallow_any_generics = true
101+
disallow_untyped_calls = true
102+
strict_optional = true
103+
warn_return_any = true
104+
98105
[tool.pytest.ini_options]
99106
addopts = "--no-cov-on-fail --showlocals --strict-markers"
100107
asyncio_mode = "strict"

src/hypercorn/middleware/dispatcher.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Callable, Dict
66

77
from ..asyncio.task_group import TaskGroup
8-
from ..typing import ASGIFramework, Scope
8+
from ..typing import ASGIFramework, ASGIReceiveEvent, Scope
99

1010
MAX_QUEUE_SIZE = 10
1111

@@ -74,7 +74,9 @@ class TrioDispatcherMiddleware(_DispatcherMiddleware):
7474
async def _handle_lifespan(self, scope: Scope, receive: Callable, send: Callable) -> None:
7575
import trio
7676

77-
self.app_queues = {path: trio.open_memory_channel(MAX_QUEUE_SIZE) for path in self.mounts}
77+
self.app_queues = {
78+
path: trio.open_memory_channel[ASGIReceiveEvent](MAX_QUEUE_SIZE) for path in self.mounts
79+
}
7880
self.startup_complete = {path: False for path in self.mounts}
7981
self.shutdown_complete = {path: False for path in self.mounts}
8082

src/hypercorn/trio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async def serve(
1616
config: Config,
1717
*,
1818
shutdown_trigger: Optional[Callable[..., Awaitable[None]]] = None,
19-
task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED,
19+
task_status: trio.TaskStatus = trio.TASK_STATUS_IGNORED,
2020
mode: Optional[Literal["asgi", "wsgi"]] = None,
2121
) -> None:
2222
"""Serve an ASGI framework app given the config.

src/hypercorn/trio/lifespan.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def __init__(self, app: AppWrapper, config: Config, state: LifespanState) -> Non
2222
self.config = config
2323
self.startup = trio.Event()
2424
self.shutdown = trio.Event()
25-
self.app_send_channel, self.app_receive_channel = trio.open_memory_channel(
26-
config.max_app_queue_size
27-
)
25+
self.app_send_channel, self.app_receive_channel = trio.open_memory_channel[
26+
ASGIReceiveEvent
27+
](config.max_app_queue_size)
2828
self.state = state
2929
self.supported = True
3030

3131
async def handle_lifespan(
32-
self, *, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED
32+
self, *, task_status: trio.TaskStatus = trio.TASK_STATUS_IGNORED
3333
) -> None:
3434
task_status.started()
3535
scope: LifespanScope = {

src/hypercorn/trio/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def worker_serve(
3333
*,
3434
sockets: Optional[Sockets] = None,
3535
shutdown_trigger: Optional[Callable[..., Awaitable[None]]] = None,
36-
task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED,
36+
task_status: trio.TaskStatus = trio.TASK_STATUS_IGNORED,
3737
) -> None:
3838
config.set_statsd_logger_class(StatsdLogger)
3939

@@ -57,7 +57,7 @@ async def worker_serve(
5757
sock.listen(config.backlog)
5858

5959
ssl_context = config.create_ssl_context()
60-
listeners = []
60+
listeners: list[trio.SSLListener[trio.SocketStream] | trio.SocketListener] = []
6161
binds = []
6262
for sock in sockets.secure_sockets:
6363
listeners.append(

src/hypercorn/trio/task_group.py

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

33
import sys
4+
from contextlib import AbstractAsyncContextManager
45
from types import TracebackType
56
from typing import Any, Awaitable, Callable, Optional
67

@@ -41,8 +42,8 @@ async def _handle(
4142

4243
class TaskGroup:
4344
def __init__(self) -> None:
44-
self._nursery: Optional[trio._core._run.Nursery] = None
45-
self._nursery_manager: Optional[trio._core._run.NurseryManager] = None
45+
self._nursery: trio.Nursery | None = None
46+
self._nursery_manager: AbstractAsyncContextManager[trio.Nursery] | None = None
4647

4748
async def spawn_app(
4849
self,
@@ -51,7 +52,9 @@ async def spawn_app(
5152
scope: Scope,
5253
send: Callable[[Optional[ASGISendEvent]], Awaitable[None]],
5354
) -> Callable[[ASGIReceiveEvent], Awaitable[None]]:
54-
app_send_channel, app_receive_channel = trio.open_memory_channel(config.max_app_queue_size)
55+
app_send_channel, app_receive_channel = trio.open_memory_channel[ASGIReceiveEvent](
56+
config.max_app_queue_size
57+
)
5558
self._nursery.start_soon(
5659
_handle,
5760
app,

src/hypercorn/trio/tcp_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(
2323
config: Config,
2424
context: WorkerContext,
2525
state: LifespanState,
26-
stream: trio.abc.Stream,
26+
stream: trio.SSLStream[trio.SocketStream],
2727
) -> None:
2828
self.app = app
2929
self.config = config

src/hypercorn/trio/udp_server.py

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

3+
import socket
4+
35
import trio
46

57
from .task_group import TaskGroup
@@ -19,17 +21,15 @@ def __init__(
1921
config: Config,
2022
context: WorkerContext,
2123
state: LifespanState,
22-
socket: trio.socket.socket,
24+
socket: socket.socket,
2325
) -> None:
2426
self.app = app
2527
self.config = config
2628
self.context = context
2729
self.socket = trio.socket.from_stdlib_socket(socket)
2830
self.state = state
2931

30-
async def run(
31-
self, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED
32-
) -> None:
32+
async def run(self, task_status: trio.TaskStatus = trio.TASK_STATUS_IGNORED) -> None:
3333
from ..protocol.quic import QuicProtocol # h3/Quic is an optional part of Hypercorn
3434

3535
task_status.started()

src/hypercorn/trio/worker_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def _cancel_wrapper(func: Callable[[], Awaitable[None]]) -> Callable[[], Awaitable[None]]:
1212
@wraps(func)
1313
async def wrapper(
14-
task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED,
14+
task_status: trio.TaskStatus = trio.TASK_STATUS_IGNORED,
1515
) -> None:
1616
cancel_scope = trio.CancelScope()
1717
task_status.started(cancel_scope)

tests/test_app_wrappers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import trio
99

1010
from hypercorn.app_wrappers import _build_environ, InvalidPathError, WSGIWrapper
11-
from hypercorn.typing import ASGISendEvent, ConnectionState, HTTPScope
11+
from hypercorn.typing import ASGIReceiveEvent, ASGISendEvent, ConnectionState, HTTPScope
1212

1313

1414
def echo_body(environ: dict, start_response: Callable) -> List[bytes]:
@@ -41,8 +41,8 @@ async def test_wsgi_trio() -> None:
4141
"extensions": {},
4242
"state": ConnectionState({}),
4343
}
44-
send_channel, receive_channel = trio.open_memory_channel(1)
45-
await send_channel.send({"type": "http.request"})
44+
send_channel, receive_channel = trio.open_memory_channel[ASGIReceiveEvent](1)
45+
await send_channel.send({"type": "http.request"}) # type: ignore
4646

4747
messages = []
4848

0 commit comments

Comments
 (0)