From aef4f8ffbce069ac67609b9966253585d7397659 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:00:19 -0500 Subject: [PATCH 01/11] Add type annotations for windows_pipes and its test --- pyproject.toml | 4 ---- trio/_tests/test_windows_pipes.py | 18 +++++++++--------- trio/_windows_pipes.py | 16 ++++++++-------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 73b110ebac..3a9a75b326 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,9 +83,6 @@ disallow_untyped_calls = false # files not yet fully typed [[tool.mypy.overrides]] module = [ -# internal -"trio/_windows_pipes", - # tests "trio/testing/_fake_net", "trio/_core/_tests/test_guest_mode", @@ -116,7 +113,6 @@ module = [ "trio/_tests/test_tracing", "trio/_tests/test_util", "trio/_tests/test_wait_for_object", -"trio/_tests/test_windows_pipes", "trio/_tests/tools/test_gen_exports", ] check_untyped_defs = false diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 5c4bae7d25..399d7116bb 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -24,14 +24,14 @@ async def make_pipe() -> Tuple[PipeSendStream, PipeReceiveStream]: return PipeSendStream(w), PipeReceiveStream(r) -async def test_pipe_typecheck(): +async def test_pipe_typecheck() -> None: with pytest.raises(TypeError): PipeSendStream(1.0) with pytest.raises(TypeError): PipeReceiveStream(None) -async def test_pipe_error_on_close(): +async def test_pipe_error_on_close() -> None: # Make sure we correctly handle a failure from kernel32.CloseHandle r, w = pipe() @@ -47,18 +47,18 @@ async def test_pipe_error_on_close(): await receive_stream.aclose() -async def test_pipes_combined(): +async def test_pipes_combined() -> None: write, read = await make_pipe() count = 2**20 replicas = 3 - async def sender(): + async def sender() -> None: async with write: big = bytearray(count) for _ in range(replicas): await write.send_all(big) - async def reader(): + async def reader() -> None: async with read: await wait_all_tasks_blocked() total_received = 0 @@ -76,7 +76,7 @@ async def reader(): n.start_soon(reader) -async def test_async_with(): +async def test_async_with() -> None: w, r = await make_pipe() async with w, r: pass @@ -87,11 +87,11 @@ async def test_async_with(): await r.receive_some(10) -async def test_close_during_write(): +async def test_close_during_write() -> None: w, r = await make_pipe() async with _core.open_nursery() as nursery: - async def write_forever(): + async def write_forever() -> None: with pytest.raises(_core.ClosedResourceError) as excinfo: while True: await w.send_all(b"x" * 4096) @@ -102,7 +102,7 @@ async def write_forever(): await w.aclose() -async def test_pipe_fully(): +async def test_pipe_fully() -> None: # passing make_clogged_pipe tests wait_send_all_might_not_block, and we # can't implement that on Windows await check_one_way_stream(make_pipe, None) diff --git a/trio/_windows_pipes.py b/trio/_windows_pipes.py index a4f59b2c70..dedf4b7300 100644 --- a/trio/_windows_pipes.py +++ b/trio/_windows_pipes.py @@ -23,10 +23,10 @@ def __init__(self, handle: int) -> None: _core.register_with_iocp(self.handle) @property - def closed(self): + def closed(self) -> bool: return self.handle == -1 - def close(self): + def close(self) -> None: if self.closed: return handle = self.handle @@ -34,7 +34,7 @@ def close(self): if not kernel32.CloseHandle(_handle(handle)): raise_winerror() - def __del__(self): + def __del__(self) -> None: self.close() @@ -76,10 +76,10 @@ async def wait_send_all_might_not_block(self) -> None: # not implemented yet, and probably not needed await _core.checkpoint() - def close(self): + def close(self) -> None: self._handle_holder.close() - async def aclose(self): + async def aclose(self) -> None: self.close() await _core.checkpoint() @@ -94,7 +94,7 @@ def __init__(self, handle: int) -> None: "another task is currently using this pipe" ) - async def receive_some(self, max_bytes=None) -> bytes: + async def receive_some(self, max_bytes: int | None = None) -> bytes: with self._conflict_detector: if self._handle_holder.closed: raise _core.ClosedResourceError("this pipe is already closed") @@ -133,9 +133,9 @@ async def receive_some(self, max_bytes=None) -> bytes: del buffer[size:] return buffer - def close(self): + def close(self) -> None: self._handle_holder.close() - async def aclose(self): + async def aclose(self) -> None: self.close() await _core.checkpoint() From c078bdba37cd5027df2e9301ed2403fc3d750395 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:08:18 -0500 Subject: [PATCH 02/11] Fix mypy issues --- trio/_tests/test_windows_pipes.py | 6 ++++-- trio/_windows_pipes.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 399d7116bb..1ee8e52a9b 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -14,6 +14,8 @@ else: pytestmark = pytest.mark.skip(reason="windows only") pipe: Any = None + _handle: Any = None + kernel32: Any = None PipeSendStream: Any = None PipeReceiveStream: Any = None @@ -26,9 +28,9 @@ async def make_pipe() -> Tuple[PipeSendStream, PipeReceiveStream]: async def test_pipe_typecheck() -> None: with pytest.raises(TypeError): - PipeSendStream(1.0) + PipeSendStream(1.0) # type: ignore[arg-type] with pytest.raises(TypeError): - PipeReceiveStream(None) + PipeReceiveStream(None) # type: ignore[arg-type] async def test_pipe_error_on_close() -> None: diff --git a/trio/_windows_pipes.py b/trio/_windows_pipes.py index dedf4b7300..43592807b8 100644 --- a/trio/_windows_pipes.py +++ b/trio/_windows_pipes.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import sys from typing import TYPE_CHECKING @@ -50,7 +52,7 @@ def __init__(self, handle: int) -> None: "another task is currently using this pipe" ) - async def send_all(self, data: bytes): + async def send_all(self, data: bytes) -> None: with self._conflict_detector: if self._handle_holder.closed: raise _core.ClosedResourceError("this pipe is already closed") From ef7027a180eb65bb6a0fe6583b13e4e6901f7a01 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:25:39 -0500 Subject: [PATCH 03/11] janky fix typing for linux and macos --- trio/_tests/test_windows_pipes.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 1ee8e52a9b..4662d7e623 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -16,8 +16,33 @@ pipe: Any = None _handle: Any = None kernel32: Any = None - PipeSendStream: Any = None - PipeReceiveStream: Any = None + from .._abc import ReceiveStream, SendStream + + class _fake_recieve(ReceiveStream): + def __init__(self, _: int) -> None: + ... + + async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: + return b"" + + async def aclose(self) -> None: + ... + + class _fake_send(SendStream): + def __init__(self, _: int) -> None: + ... + + async def send_all(self, data: bytes | bytearray | memoryview) -> None: + ... + + async def wait_send_all_might_not_block(self) -> None: + ... + + async def aclose(self) -> None: + ... + + PipeReceiveStream = _fake_recieve + PipeSendStream = _fake_send async def make_pipe() -> Tuple[PipeSendStream, PipeReceiveStream]: From b3fddad2113db207b77bb307432b5bafb31c0882 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:29:32 -0500 Subject: [PATCH 04/11] Add future annotations import --- trio/_tests/test_windows_pipes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 4662d7e623..902c21d492 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -1,5 +1,7 @@ +from __future__ import annotations + import sys -from typing import Any, Tuple +from typing import Any import pytest @@ -45,7 +47,7 @@ async def aclose(self) -> None: PipeSendStream = _fake_send -async def make_pipe() -> Tuple[PipeSendStream, PipeReceiveStream]: +async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: """Makes a new pair of pipes.""" (r, w) = pipe() return PipeSendStream(w), PipeReceiveStream(r) From bc72284c5ddee27df35714c9f83a56ae286b2c28 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Thu, 5 Oct 2023 23:06:06 -0500 Subject: [PATCH 05/11] Fix spelling (skip the middle man and define directly) --- trio/_tests/test_windows_pipes.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 902c21d492..4664078112 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -20,7 +20,7 @@ kernel32: Any = None from .._abc import ReceiveStream, SendStream - class _fake_recieve(ReceiveStream): + class PipeReceiveStream(ReceiveStream): def __init__(self, _: int) -> None: ... @@ -30,7 +30,7 @@ async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: async def aclose(self) -> None: ... - class _fake_send(SendStream): + class PipeSendStream(SendStream): def __init__(self, _: int) -> None: ... @@ -43,9 +43,6 @@ async def wait_send_all_might_not_block(self) -> None: async def aclose(self) -> None: ... - PipeReceiveStream = _fake_recieve - PipeSendStream = _fake_send - async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: """Makes a new pair of pipes.""" From 6fc51bd6248e98b7a81a743311788896586b1e29 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:15:38 -0500 Subject: [PATCH 06/11] Skup typechecking `test_windows_pipes` on non-windows --- trio/_tests/test_windows_pipes.py | 40 +++++++------------------------ 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 4664078112..20836723b9 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -1,47 +1,23 @@ from __future__ import annotations import sys -from typing import Any +from typing import TYPE_CHECKING import pytest from .. import _core from ..testing import check_one_way_stream, wait_all_tasks_blocked -if sys.platform == "win32": - from asyncio.windows_utils import pipe +on_windows = sys.platform == "win32" +# Mark all the tests in this file as being windows-only +pytestmark = pytest.mark.skipif(not on_windows, reason="windows only") - from .._core._windows_cffi import _handle, kernel32 - from .._windows_pipes import PipeReceiveStream, PipeSendStream -else: - pytestmark = pytest.mark.skip(reason="windows only") - pipe: Any = None - _handle: Any = None - kernel32: Any = None - from .._abc import ReceiveStream, SendStream +assert on_windows or not TYPE_CHECKING # Skip type checking when not on Windows - class PipeReceiveStream(ReceiveStream): - def __init__(self, _: int) -> None: - ... +from asyncio.windows_utils import pipe - async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: - return b"" - - async def aclose(self) -> None: - ... - - class PipeSendStream(SendStream): - def __init__(self, _: int) -> None: - ... - - async def send_all(self, data: bytes | bytearray | memoryview) -> None: - ... - - async def wait_send_all_might_not_block(self) -> None: - ... - - async def aclose(self) -> None: - ... +from .._core._windows_cffi import _handle, kernel32 +from .._windows_pipes import PipeReceiveStream, PipeSendStream async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: From 017863689c51697d78218744ad0c0a3999df363d Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:21:57 -0500 Subject: [PATCH 07/11] Wrap windows imports in if statement --- trio/_tests/test_windows_pipes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 20836723b9..432ff402bb 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -14,10 +14,11 @@ assert on_windows or not TYPE_CHECKING # Skip type checking when not on Windows -from asyncio.windows_utils import pipe +if sys.platform == "win32": + from asyncio.windows_utils import pipe -from .._core._windows_cffi import _handle, kernel32 -from .._windows_pipes import PipeReceiveStream, PipeSendStream + from .._core._windows_cffi import _handle, kernel32 + from .._windows_pipes import PipeReceiveStream, PipeSendStream async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: From e74adb211d9733128c5bd337e890c9b26931356f Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:31:12 -0500 Subject: [PATCH 08/11] Re-add else block, removing results in mypy failures --- trio/_tests/test_windows_pipes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 432ff402bb..17c5ec341d 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import pytest @@ -19,6 +19,12 @@ from .._core._windows_cffi import _handle, kernel32 from .._windows_pipes import PipeReceiveStream, PipeSendStream +else: + pipe = Any + _handle = Any + kernel32 = Any + PipeReceiveStream = Any + PipeSendStream = Any async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: From ee64fe3a68f25f6ba62827d2dd3d5f7087f14d2f Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:59:44 -0500 Subject: [PATCH 09/11] Change if block so mypy understands --- trio/_tests/test_windows_pipes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 17c5ec341d..6a69f90f21 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -8,11 +8,12 @@ from .. import _core from ..testing import check_one_way_stream, wait_all_tasks_blocked -on_windows = sys.platform == "win32" # Mark all the tests in this file as being windows-only -pytestmark = pytest.mark.skipif(not on_windows, reason="windows only") +pytestmark = pytest.mark.skipif(sys.platform != "win32", reason="windows only") -assert on_windows or not TYPE_CHECKING # Skip type checking when not on Windows +assert ( + sys.platform == "win32" or not TYPE_CHECKING +) # Skip type checking when not on Windows if sys.platform == "win32": from asyncio.windows_utils import pipe From a92b9c818715fc8d5434025977e1d7a05bfc3130 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 14 Oct 2023 12:10:06 -0500 Subject: [PATCH 10/11] Change comment position --- trio/_tests/test_windows_pipes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index 6a69f90f21..c26b578629 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -11,9 +11,9 @@ # Mark all the tests in this file as being windows-only pytestmark = pytest.mark.skipif(sys.platform != "win32", reason="windows only") -assert ( +assert ( # Skip type checking when not on Windows sys.platform == "win32" or not TYPE_CHECKING -) # Skip type checking when not on Windows +) if sys.platform == "win32": from asyncio.windows_utils import pipe From 5c4529c433dc57a78091f64b583a193d8ee438f1 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:49:59 -0500 Subject: [PATCH 11/11] Try removing else block again --- trio/_tests/test_windows_pipes.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/trio/_tests/test_windows_pipes.py b/trio/_tests/test_windows_pipes.py index c26b578629..f0783b7b06 100644 --- a/trio/_tests/test_windows_pipes.py +++ b/trio/_tests/test_windows_pipes.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING import pytest @@ -20,12 +20,6 @@ from .._core._windows_cffi import _handle, kernel32 from .._windows_pipes import PipeReceiveStream, PipeSendStream -else: - pipe = Any - _handle = Any - kernel32 = Any - PipeReceiveStream = Any - PipeSendStream = Any async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]: