Skip to content

Commit b161fec

Browse files
authored
Add type annotations for windows_pipes and its test (#2812)
* Add type annotations for windows_pipes and its test
1 parent f2cb7b1 commit b161fec

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ disallow_untyped_calls = false
8383
# files not yet fully typed
8484
[[tool.mypy.overrides]]
8585
module = [
86-
# internal
87-
"trio/_windows_pipes",
88-
8986
# tests
9087
"trio/testing/_fake_net",
9188
"trio/_core/_tests/test_guest_mode",
@@ -115,7 +112,6 @@ module = [
115112
"trio/_tests/test_tracing",
116113
"trio/_tests/test_util",
117114
"trio/_tests/test_wait_for_object",
118-
"trio/_tests/test_windows_pipes",
119115
"trio/_tests/tools/test_gen_exports",
120116
]
121117
check_untyped_defs = false

trio/_tests/test_windows_pipes.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1+
from __future__ import annotations
2+
13
import sys
2-
from typing import Any, Tuple
4+
from typing import TYPE_CHECKING
35

46
import pytest
57

68
from .. import _core
79
from ..testing import check_one_way_stream, wait_all_tasks_blocked
810

11+
# Mark all the tests in this file as being windows-only
12+
pytestmark = pytest.mark.skipif(sys.platform != "win32", reason="windows only")
13+
14+
assert ( # Skip type checking when not on Windows
15+
sys.platform == "win32" or not TYPE_CHECKING
16+
)
17+
918
if sys.platform == "win32":
1019
from asyncio.windows_utils import pipe
1120

1221
from .._core._windows_cffi import _handle, kernel32
1322
from .._windows_pipes import PipeReceiveStream, PipeSendStream
14-
else:
15-
pytestmark = pytest.mark.skip(reason="windows only")
16-
pipe: Any = None
17-
PipeSendStream: Any = None
18-
PipeReceiveStream: Any = None
1923

2024

21-
async def make_pipe() -> Tuple[PipeSendStream, PipeReceiveStream]:
25+
async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]:
2226
"""Makes a new pair of pipes."""
2327
(r, w) = pipe()
2428
return PipeSendStream(w), PipeReceiveStream(r)
2529

2630

27-
async def test_pipe_typecheck():
31+
async def test_pipe_typecheck() -> None:
2832
with pytest.raises(TypeError):
29-
PipeSendStream(1.0)
33+
PipeSendStream(1.0) # type: ignore[arg-type]
3034
with pytest.raises(TypeError):
31-
PipeReceiveStream(None)
35+
PipeReceiveStream(None) # type: ignore[arg-type]
3236

3337

34-
async def test_pipe_error_on_close():
38+
async def test_pipe_error_on_close() -> None:
3539
# Make sure we correctly handle a failure from kernel32.CloseHandle
3640
r, w = pipe()
3741

@@ -47,18 +51,18 @@ async def test_pipe_error_on_close():
4751
await receive_stream.aclose()
4852

4953

50-
async def test_pipes_combined():
54+
async def test_pipes_combined() -> None:
5155
write, read = await make_pipe()
5256
count = 2**20
5357
replicas = 3
5458

55-
async def sender():
59+
async def sender() -> None:
5660
async with write:
5761
big = bytearray(count)
5862
for _ in range(replicas):
5963
await write.send_all(big)
6064

61-
async def reader():
65+
async def reader() -> None:
6266
async with read:
6367
await wait_all_tasks_blocked()
6468
total_received = 0
@@ -76,7 +80,7 @@ async def reader():
7680
n.start_soon(reader)
7781

7882

79-
async def test_async_with():
83+
async def test_async_with() -> None:
8084
w, r = await make_pipe()
8185
async with w, r:
8286
pass
@@ -87,11 +91,11 @@ async def test_async_with():
8791
await r.receive_some(10)
8892

8993

90-
async def test_close_during_write():
94+
async def test_close_during_write() -> None:
9195
w, r = await make_pipe()
9296
async with _core.open_nursery() as nursery:
9397

94-
async def write_forever():
98+
async def write_forever() -> None:
9599
with pytest.raises(_core.ClosedResourceError) as excinfo:
96100
while True:
97101
await w.send_all(b"x" * 4096)
@@ -102,7 +106,7 @@ async def write_forever():
102106
await w.aclose()
103107

104108

105-
async def test_pipe_fully():
109+
async def test_pipe_fully() -> None:
106110
# passing make_clogged_pipe tests wait_send_all_might_not_block, and we
107111
# can't implement that on Windows
108112
await check_one_way_stream(make_pipe, None)

trio/_windows_pipes.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import sys
24
from typing import TYPE_CHECKING
35

@@ -23,18 +25,18 @@ def __init__(self, handle: int) -> None:
2325
_core.register_with_iocp(self.handle)
2426

2527
@property
26-
def closed(self):
28+
def closed(self) -> bool:
2729
return self.handle == -1
2830

29-
def close(self):
31+
def close(self) -> None:
3032
if self.closed:
3133
return
3234
handle = self.handle
3335
self.handle = -1
3436
if not kernel32.CloseHandle(_handle(handle)):
3537
raise_winerror()
3638

37-
def __del__(self):
39+
def __del__(self) -> None:
3840
self.close()
3941

4042

@@ -50,7 +52,7 @@ def __init__(self, handle: int) -> None:
5052
"another task is currently using this pipe"
5153
)
5254

53-
async def send_all(self, data: bytes):
55+
async def send_all(self, data: bytes) -> None:
5456
with self._conflict_detector:
5557
if self._handle_holder.closed:
5658
raise _core.ClosedResourceError("this pipe is already closed")
@@ -76,10 +78,10 @@ async def wait_send_all_might_not_block(self) -> None:
7678
# not implemented yet, and probably not needed
7779
await _core.checkpoint()
7880

79-
def close(self):
81+
def close(self) -> None:
8082
self._handle_holder.close()
8183

82-
async def aclose(self):
84+
async def aclose(self) -> None:
8385
self.close()
8486
await _core.checkpoint()
8587

@@ -94,7 +96,7 @@ def __init__(self, handle: int) -> None:
9496
"another task is currently using this pipe"
9597
)
9698

97-
async def receive_some(self, max_bytes=None) -> bytes:
99+
async def receive_some(self, max_bytes: int | None = None) -> bytes:
98100
with self._conflict_detector:
99101
if self._handle_holder.closed:
100102
raise _core.ClosedResourceError("this pipe is already closed")
@@ -133,9 +135,9 @@ async def receive_some(self, max_bytes=None) -> bytes:
133135
del buffer[size:]
134136
return buffer
135137

136-
def close(self):
138+
def close(self) -> None:
137139
self._handle_holder.close()
138140

139-
async def aclose(self):
141+
async def aclose(self) -> None:
140142
self.close()
141143
await _core.checkpoint()

0 commit comments

Comments
 (0)