From d9a7e247253c173ae9a1061e45063d48892d3cfa Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sun, 6 Aug 2023 22:37:49 -0500 Subject: [PATCH 01/24] Add type annotations to `_ssl.py` --- trio/_abc.py | 2 +- trio/_ssl.py | 105 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/trio/_abc.py b/trio/_abc.py index 59454b794c..746360c8f8 100644 --- a/trio/_abc.py +++ b/trio/_abc.py @@ -565,7 +565,7 @@ class Listener(AsyncResource, Generic[T_resource]): __slots__ = () @abstractmethod - async def accept(self) -> AsyncResource: + async def accept(self) -> T_resource: """Wait until an incoming connection arrives, and then return it. Returns: diff --git a/trio/_ssl.py b/trio/_ssl.py index bd8b3b06b6..75841ec287 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -1,3 +1,18 @@ +from __future__ import annotations + +import operator as _operator +import ssl as _stdlib_ssl +from collections.abc import Awaitable, Callable +from enum import Enum as _Enum +from typing import Any, Final as TFinal, TypeGuard, TypeVar + +import trio + +from . import _sync +from ._highlevel_generic import aclose_forcefully +from ._util import ConflictDetector, Final +from .abc import AsyncResource, Listener, Stream + # General theory of operation: # # We implement an API that closely mirrors the stdlib ssl module's blocking @@ -149,16 +164,10 @@ # docs will need to make very clear that this is different from all the other # cancellations in core Trio -import operator as _operator -import ssl as _stdlib_ssl -from enum import Enum as _Enum -import trio -from . import _sync -from ._highlevel_generic import aclose_forcefully -from ._util import ConflictDetector, Final -from .abc import Listener, Stream + +T = TypeVar("T") ################################################################ # SSLStream @@ -187,16 +196,16 @@ # MTU and an initial window of 10 (see RFC 6928), then the initial burst of # data will be limited to ~15000 bytes (or a bit less due to IP-level framing # overhead), so this is chosen to be larger than that. -STARTING_RECEIVE_SIZE = 16384 +STARTING_RECEIVE_SIZE: TFinal = 16384 -def _is_eof(exc): +def _is_eof(exc: BaseException | None) -> bool: # There appears to be a bug on Python 3.10, where SSLErrors # aren't properly translated into SSLEOFErrors. # This stringly-typed error check is borrowed from the AnyIO # project. return isinstance(exc, _stdlib_ssl.SSLEOFError) or ( - hasattr(exc, "strerror") and "UNEXPECTED_EOF_WHILE_READING" in exc.strerror + "UNEXPECTED_EOF_WHILE_READING" in getattr(exc, "strerror", ()) ) @@ -207,15 +216,19 @@ class NeedHandshakeError(Exception): """ + __slots__ = () + class _Once: - def __init__(self, afn, *args): + __slots__ = ("_afn", "_args", "started", "_done") + + def __init__(self, afn: Callable[..., Awaitable[Any]], *args: Any) -> None: self._afn = afn self._args = args self.started = False self._done = _sync.Event() - async def ensure(self, *, checkpoint): + async def ensure(self, *, checkpoint: bool) -> None: if not self.started: self.started = True await self._afn(*self._args) @@ -226,7 +239,7 @@ async def ensure(self, *, checkpoint): await self._done.wait() @property - def done(self): + def done(self) -> bool: return self._done.is_set() @@ -331,18 +344,18 @@ class SSLStream(Stream, metaclass=Final): # SSLListener.__init__, and maybe the open_ssl_over_tcp_* helpers. def __init__( self, - transport_stream, - ssl_context, + transport_stream: Stream, + ssl_context: _stdlib_ssl.SSLContext, *, - server_hostname=None, - server_side=False, - https_compatible=False, - ): + server_hostname: str | None = None, + server_side: bool = False, + https_compatible: bool = False, + ) -> None: self.transport_stream = transport_stream self._state = _State.OK self._https_compatible = https_compatible self._outgoing = _stdlib_ssl.MemoryBIO() - self._delayed_outgoing = None + self._delayed_outgoing: bytes | None = None self._incoming = _stdlib_ssl.MemoryBIO() self._ssl_object = ssl_context.wrap_bio( self._incoming, @@ -399,7 +412,7 @@ def __init__( "version", } - def __getattr__(self, name): + def __getattr__(self, name: str) -> Any: if name in self._forwarded: if name in self._after_handshake and not self._handshook.done: raise NeedHandshakeError(f"call do_handshake() before calling {name!r}") @@ -408,16 +421,16 @@ def __getattr__(self, name): else: raise AttributeError(name) - def __setattr__(self, name, value): + def __setattr__(self, name: str, value: Any) -> None: if name in self._forwarded: setattr(self._ssl_object, name, value) else: super().__setattr__(name, value) - def __dir__(self): - return super().__dir__() + list(self._forwarded) + def __dir__(self) -> list[str]: + return list(super().__dir__()) + list(self._forwarded) - def _check_status(self): + def _check_status(self) -> None: if self._state is _State.OK: return elif self._state is _State.BROKEN: @@ -431,7 +444,13 @@ def _check_status(self): # comments, though, just make sure to think carefully if you ever have to # touch it. The big comment at the top of this file will help explain # too. - async def _retry(self, fn, *args, ignore_want_read=False, is_handshake=False): + async def _retry( + self, + fn: Callable[..., T], + *args: Any, + ignore_want_read: bool = False, + is_handshake: bool = False, + ) -> T | None: await trio.lowlevel.checkpoint_if_cancelled() yielded = False finished = False @@ -603,14 +622,14 @@ async def _retry(self, fn, *args, ignore_want_read=False, is_handshake=False): await trio.lowlevel.cancel_shielded_checkpoint() return ret - async def _do_handshake(self): + async def _do_handshake(self) -> None: try: await self._retry(self._ssl_object.do_handshake, is_handshake=True) except: self._state = _State.BROKEN raise - async def do_handshake(self): + async def do_handshake(self) -> None: """Ensure that the initial handshake has completed. The SSL protocol requires an initial handshake to exchange @@ -645,7 +664,7 @@ async def do_handshake(self): # https://bugs.python.org/issue30141 # So we *definitely* have to make sure that do_handshake is called # before doing anything else. - async def receive_some(self, max_bytes=None): + async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: """Read some data from the underlying transport, decrypt it, and return it. @@ -684,7 +703,9 @@ async def receive_some(self, max_bytes=None): if max_bytes < 1: raise ValueError("max_bytes must be >= 1") try: - return await self._retry(self._ssl_object.read, max_bytes) + recieved = await self._retry(self._ssl_object.read, max_bytes) + assert recieved is not None + return recieved except trio.BrokenResourceError as exc: # This isn't quite equivalent to just returning b"" in the # first place, because we still end up with self._state set to @@ -698,7 +719,7 @@ async def receive_some(self, max_bytes=None): else: raise - async def send_all(self, data): + async def send_all(self, data: bytes | bytearray | memoryview) -> None: """Encrypt some data and then send it on the underlying transport. See :meth:`trio.abc.SendStream.send_all` for details. @@ -719,7 +740,7 @@ async def send_all(self, data): return await self._retry(self._ssl_object.write, data) - async def unwrap(self): + async def unwrap(self) -> tuple[Stream, bytes | bytearray]: """Cleanly close down the SSL/TLS encryption layer, allowing the underlying stream to be used for unencrypted communication. @@ -741,11 +762,11 @@ async def unwrap(self): await self._handshook.ensure(checkpoint=False) await self._retry(self._ssl_object.unwrap) transport_stream = self.transport_stream - self.transport_stream = None self._state = _State.CLOSED + self.transport_stream = None # type: ignore[assignment] # Messy, but State closed, should not use, should be fine... return (transport_stream, self._incoming.read()) - async def aclose(self): + async def aclose(self) -> None: """Gracefully shut down this connection, and close the underlying transport. @@ -832,7 +853,7 @@ async def aclose(self): finally: self._state = _State.CLOSED - async def wait_send_all_might_not_block(self): + async def wait_send_all_might_not_block(self) -> None: """See :meth:`trio.abc.SendStream.wait_send_all_might_not_block`.""" # This method's implementation is deceptively simple. # @@ -897,16 +918,16 @@ class SSLListener(Listener[SSLStream], metaclass=Final): def __init__( self, - transport_listener, - ssl_context, + transport_listener: Listener[Stream], + ssl_context: _stdlib_ssl.SSLContext, *, - https_compatible=False, - ): + https_compatible: bool = False, + ) -> None: self.transport_listener = transport_listener self._ssl_context = ssl_context self._https_compatible = https_compatible - async def accept(self): + async def accept(self) -> SSLStream: """Accept the next connection and wrap it in an :class:`SSLStream`. See :meth:`trio.abc.Listener.accept` for details. @@ -920,6 +941,6 @@ async def accept(self): https_compatible=self._https_compatible, ) - async def aclose(self): + async def aclose(self) -> None: """Close the transport listener.""" await self.transport_listener.aclose() From abcb7cd438f70f25a22fe7c6020f0c7f760b646b Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sun, 6 Aug 2023 22:53:10 -0500 Subject: [PATCH 02/24] Fix for pre-3.10 --- trio/_ssl.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 75841ec287..e6055104bc 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -4,9 +4,10 @@ import ssl as _stdlib_ssl from collections.abc import Awaitable, Callable from enum import Enum as _Enum -from typing import Any, Final as TFinal, TypeGuard, TypeVar +from typing import TYPE_CHECKING, Any, Final as TFinal, TypeVar -import trio +if TYPE_CHECKING: + from typing_extensions import TypeGuard from . import _sync from ._highlevel_generic import aclose_forcefully @@ -165,8 +166,6 @@ # cancellations in core Trio - - T = TypeVar("T") ################################################################ From 81d8d859eff4fa0d70b6153be291833f9ae5cb61 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 7 Aug 2023 00:55:55 -0500 Subject: [PATCH 03/24] Fix CI issues --- trio/_ssl.py | 10 ++++++---- trio/_tests/verify_types.json | 29 +++++++++-------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index e6055104bc..01bee816b4 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -6,6 +6,8 @@ from enum import Enum as _Enum from typing import TYPE_CHECKING, Any, Final as TFinal, TypeVar +import trio + if TYPE_CHECKING: from typing_extensions import TypeGuard @@ -239,7 +241,7 @@ async def ensure(self, *, checkpoint: bool) -> None: @property def done(self) -> bool: - return self._done.is_set() + return bool(self._done.is_set()) _State = _Enum("_State", ["OK", "BROKEN", "CLOSED"]) @@ -702,9 +704,9 @@ async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: if max_bytes < 1: raise ValueError("max_bytes must be >= 1") try: - recieved = await self._retry(self._ssl_object.read, max_bytes) - assert recieved is not None - return recieved + received = await self._retry(self._ssl_object.read, max_bytes) + assert received is not None + return received except trio.BrokenResourceError as exc: # This isn't quite equivalent to just returning b"" in the # first place, because we still end up with self._state set to diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index 55ba3b32d7..82058cc862 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,11 +7,11 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9186602870813397, + "completenessScore": 0.9202551834130781, "exportedSymbolCounts": { - "withAmbiguousType": 0, - "withKnownType": 576, - "withUnknownType": 51 + "withAmbiguousType": 1, + "withKnownType": 577, + "withUnknownType": 46 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, @@ -45,12 +45,13 @@ } ], "otherSymbolCounts": { - "withAmbiguousType": 3, - "withKnownType": 602, - "withUnknownType": 61 + "withAmbiguousType": 4, + "withKnownType": 616, + "withUnknownType": 63 }, "packageName": "trio", "symbols": [ + "trio._core._mock_clock.MockClock.jump", "trio._core._run.Nursery.start", "trio._core._run.Nursery.start_soon", "trio._core._run.TaskStatus.__repr__", @@ -60,20 +61,7 @@ "trio._highlevel_socket.SocketStream.getsockopt", "trio._highlevel_socket.SocketStream.send_all", "trio._highlevel_socket.SocketStream.setsockopt", - "trio._ssl.SSLListener.__init__", - "trio._ssl.SSLListener.accept", - "trio._ssl.SSLListener.aclose", - "trio._ssl.SSLStream.__dir__", - "trio._ssl.SSLStream.__getattr__", - "trio._ssl.SSLStream.__init__", - "trio._ssl.SSLStream.__setattr__", - "trio._ssl.SSLStream.aclose", - "trio._ssl.SSLStream.do_handshake", - "trio._ssl.SSLStream.receive_some", - "trio._ssl.SSLStream.send_all", "trio._ssl.SSLStream.transport_stream", - "trio._ssl.SSLStream.unwrap", - "trio._ssl.SSLStream.wait_send_all_might_not_block", "trio._subprocess.Process.__init__", "trio._subprocess.Process.__repr__", "trio._subprocess.Process.args", @@ -107,6 +95,7 @@ "trio.lowlevel.wait_writable", "trio.open_ssl_over_tcp_listeners", "trio.open_ssl_over_tcp_stream", + "trio.open_tcp_listeners", "trio.open_unix_socket", "trio.run", "trio.run_process", From 067527afe9d4fac8829cd56d4605c7adb11cc026 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 7 Aug 2023 00:59:39 -0500 Subject: [PATCH 04/24] Update `verify_types.json` --- trio/_tests/verify_types.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index 82058cc862..ee7a6d56f8 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -11,7 +11,7 @@ "exportedSymbolCounts": { "withAmbiguousType": 1, "withKnownType": 577, - "withUnknownType": 46 + "withUnknownType": 49 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, @@ -47,11 +47,10 @@ "otherSymbolCounts": { "withAmbiguousType": 4, "withKnownType": 616, - "withUnknownType": 63 + "withUnknownType": 46 }, "packageName": "trio", "symbols": [ - "trio._core._mock_clock.MockClock.jump", "trio._core._run.Nursery.start", "trio._core._run.Nursery.start_soon", "trio._core._run.TaskStatus.__repr__", @@ -95,7 +94,6 @@ "trio.lowlevel.wait_writable", "trio.open_ssl_over_tcp_listeners", "trio.open_ssl_over_tcp_stream", - "trio.open_tcp_listeners", "trio.open_unix_socket", "trio.run", "trio.run_process", From 985accaeee2c48cb84702083119a9b6e068cb4c7 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 7 Aug 2023 01:04:36 -0500 Subject: [PATCH 05/24] Remove unused imports --- trio/_ssl.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 01bee816b4..7d2baf2015 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -4,17 +4,14 @@ import ssl as _stdlib_ssl from collections.abc import Awaitable, Callable from enum import Enum as _Enum -from typing import TYPE_CHECKING, Any, Final as TFinal, TypeVar +from typing import Any, Final as TFinal, TypeVar import trio -if TYPE_CHECKING: - from typing_extensions import TypeGuard - from . import _sync from ._highlevel_generic import aclose_forcefully from ._util import ConflictDetector, Final -from .abc import AsyncResource, Listener, Stream +from .abc import Listener, Stream # General theory of operation: # From edbf9811df907357fe88385f58662ed99cc7f5a4 Mon Sep 17 00:00:00 2001 From: CoolCat467 Date: Sat, 5 Aug 2023 21:56:11 -0500 Subject: [PATCH 06/24] Change lots of `Any` to `object` --- trio/_ssl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 7d2baf2015..4f9b3cd2d2 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -220,7 +220,7 @@ class NeedHandshakeError(Exception): class _Once: __slots__ = ("_afn", "_args", "started", "_done") - def __init__(self, afn: Callable[..., Awaitable[Any]], *args: Any) -> None: + def __init__(self, afn: Callable[..., Awaitable[object]], *args: object) -> None: self._afn = afn self._args = args self.started = False @@ -419,7 +419,7 @@ def __getattr__(self, name: str) -> Any: else: raise AttributeError(name) - def __setattr__(self, name: str, value: Any) -> None: + def __setattr__(self, name: str, value: object) -> None: if name in self._forwarded: setattr(self._ssl_object, name, value) else: From 4a92a2deb47f80c78e6ee619e067b33110fef9bc Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:00:22 -0500 Subject: [PATCH 07/24] Update `verify_types.json` --- trio/_tests/verify_types.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index 2272f57103..ad94320df2 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,11 +7,11 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9202551834130781, + "completenessScore": 0.9218500797448166, "exportedSymbolCounts": { "withAmbiguousType": 1, - "withKnownType": 577, - "withUnknownType": 49 + "withKnownType": 578, + "withUnknownType": 48 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, From 564bb927ea1e8e516c19cc5d25c6f5afa1f8703c Mon Sep 17 00:00:00 2001 From: CoolCat467 Date: Sat, 5 Aug 2023 22:25:29 -0500 Subject: [PATCH 08/24] Add `_ssl` to stricker check block and sort modules --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a23f7f5db9..d1947d5a46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,14 +52,15 @@ module = [ "trio._abc", "trio._core._entry_queue", "trio._core._local", - "trio._core._unbounded_queue", "trio._core._thread_cache", + "trio._core._unbounded_queue", "trio._deprecate", "trio._dtls", "trio._file_io", "trio._highlevel_open_tcp_stream.py", "trio._ki", "trio._socket", + "trio._ssl", "trio._sync", "trio._tools.gen_exports", "trio._util", From 09c57613f3f3a9548c40d32c4e6841fbe4472adc Mon Sep 17 00:00:00 2001 From: CoolCat467 Date: Sat, 5 Aug 2023 22:30:48 -0500 Subject: [PATCH 09/24] Somehow this did not get in to edbf981 --- trio/_ssl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 4f9b3cd2d2..9d94e548b6 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -761,7 +761,7 @@ async def unwrap(self) -> tuple[Stream, bytes | bytearray]: await self._retry(self._ssl_object.unwrap) transport_stream = self.transport_stream self._state = _State.CLOSED - self.transport_stream = None # type: ignore[assignment] # Messy, but State closed, should not use, should be fine... + self.transport_stream = None # type: ignore[assignment] # State is CLOSED now, nothing should use return (transport_stream, self._incoming.read()) async def aclose(self) -> None: From ce776ac874780d64b246a66b4e1d471bcb6f837e Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:50:46 -0500 Subject: [PATCH 10/24] Add `trio._abc.T_resource` to doc ignore list --- docs/source/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/conf.py b/docs/source/conf.py index 39eda12ad5..26ea5e31d5 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -58,6 +58,7 @@ ("py:class", "trio._abc.ReceiveType"), ("py:class", "trio._abc.SendType"), ("py:class", "trio._abc.T"), + ("py:class", "trio._abc.T_resource"), ("py:obj", "trio._abc.ReceiveType"), ("py:obj", "trio._abc.SendType"), ("py:obj", "trio._abc.T"), From c8968995bbb1f420abca973843d287869a0b033e Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:18:45 -0500 Subject: [PATCH 11/24] Attempt to fix pyright issues --- trio/_ssl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 9d94e548b6..d19eace970 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -349,7 +349,7 @@ def __init__( server_side: bool = False, https_compatible: bool = False, ) -> None: - self.transport_stream = transport_stream + self.transport_stream: Stream = transport_stream self._state = _State.OK self._https_compatible = https_compatible self._outgoing = _stdlib_ssl.MemoryBIO() From 93333bc00ee5fd1c99403bd8581319826fbaf559 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:24:05 -0500 Subject: [PATCH 12/24] Update `verify_types.json` --- trio/_tests/verify_types.json | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index ad94320df2..2f344d5eee 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,10 +7,10 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9218500797448166, + "completenessScore": 0.9234449760765551, "exportedSymbolCounts": { - "withAmbiguousType": 1, - "withKnownType": 578, + "withAmbiguousType": 0, + "withKnownType": 579, "withUnknownType": 48 }, "ignoreUnknownTypesFromImports": true, @@ -45,8 +45,8 @@ } ], "otherSymbolCounts": { - "withAmbiguousType": 4, - "withKnownType": 616, + "withAmbiguousType": 2, + "withKnownType": 618, "withUnknownType": 46 }, "packageName": "trio", @@ -60,7 +60,6 @@ "trio._highlevel_socket.SocketStream.getsockopt", "trio._highlevel_socket.SocketStream.send_all", "trio._highlevel_socket.SocketStream.setsockopt", - "trio._ssl.SSLStream.transport_stream", "trio._subprocess.Process.__init__", "trio._subprocess.Process.__repr__", "trio._subprocess.Process.args", From 49c2c1b814805c674a29e1fdf2f78f048199ae35 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:32:19 -0500 Subject: [PATCH 13/24] Remove `__slots__` --- trio/_ssl.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index d19eace970..bdb08af10b 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -218,8 +218,6 @@ class NeedHandshakeError(Exception): class _Once: - __slots__ = ("_afn", "_args", "started", "_done") - def __init__(self, afn: Callable[..., Awaitable[object]], *args: object) -> None: self._afn = afn self._args = args From cbee73e6b5c00c258f14bac786241e770c6b9c5d Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:38:56 -0500 Subject: [PATCH 14/24] Update `verify_types.json` --- trio/_tests/verify_types.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index 90954bfb5a..f0977acb7d 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,10 +7,10 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9522343786507382, + "completenessScore": 0.9522292993630573, "exportedSymbolCounts": { "withAmbiguousType": 0, - "withKnownType": 580, + "withKnownType": 598, "withUnknownType": 30 }, "ignoreUnknownTypesFromImports": true, From acfcead1ade60b0b30735027585a8395dbd14a14 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Tue, 15 Aug 2023 23:32:22 -0500 Subject: [PATCH 15/24] Change to `object` (@A5rocks suggestion) --- trio/_ssl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index bdb08af10b..d8ff658b03 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -443,7 +443,7 @@ def _check_status(self) -> None: async def _retry( self, fn: Callable[..., T], - *args: Any, + *args: object, ignore_want_read: bool = False, is_handshake: bool = False, ) -> T | None: From 31b0ccfb12f7a47f7b5a1a45d1a3b283ce20509b Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:01:06 -0500 Subject: [PATCH 16/24] Fix `server_hostname` --- trio/_highlevel_ssl_helpers.py | 42 ++++++++++++++++++++-------------- trio/_ssl.py | 8 +++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/trio/_highlevel_ssl_helpers.py b/trio/_highlevel_ssl_helpers.py index ad77a302f0..c14b31b20a 100644 --- a/trio/_highlevel_ssl_helpers.py +++ b/trio/_highlevel_ssl_helpers.py @@ -1,4 +1,7 @@ +from __future__ import annotations + import ssl +from collections.abc import Awaitable, Callable import trio @@ -15,13 +18,13 @@ # So... let's punt on that for now. Hopefully we'll be getting a new Python # TLS API soon and can revisit this then. async def open_ssl_over_tcp_stream( - host, - port, + host: str | bytes, + port: int, *, - https_compatible=False, - ssl_context=None, - happy_eyeballs_delay=DEFAULT_DELAY, -): + https_compatible: bool = False, + ssl_context: ssl.SSLContext | None = None, + happy_eyeballs_delay: float = DEFAULT_DELAY, +) -> trio.SSLStream: """Make a TLS-encrypted Connection to the given host and port over TCP. This is a convenience wrapper that calls :func:`open_tcp_stream` and @@ -63,8 +66,13 @@ async def open_ssl_over_tcp_stream( async def open_ssl_over_tcp_listeners( - port, ssl_context, *, host=None, https_compatible=False, backlog=None -): + port: int, + ssl_context: ssl.SSLContext, + *, + host: str | bytes | None = None, + https_compatible: bool = False, + backlog: int | None = None, +) -> list[trio.SSLListener]: """Start listening for SSL/TLS-encrypted TCP connections to the given port. Args: @@ -86,16 +94,16 @@ async def open_ssl_over_tcp_listeners( async def serve_ssl_over_tcp( - handler, - port, - ssl_context, + handler: Callable[[trio.SocketStream], Awaitable[object]], + port: int, + ssl_context: ssl.SSLContext, *, - host=None, - https_compatible=False, - backlog=None, - handler_nursery=None, - task_status=trio.TASK_STATUS_IGNORED, -): + host: str | bytes | None = None, + https_compatible: bool = False, + backlog: int | None = None, + handler_nursery: trio.Nursery | None = None, + task_status: trio.TaskStatus[None] = trio.TASK_STATUS_IGNORED, +) -> None: """Listen for incoming TCP connections, and for each one start a task running ``handler(stream)``. diff --git a/trio/_ssl.py b/trio/_ssl.py index d8ff658b03..004e1ada2d 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -266,8 +266,8 @@ class SSLStream(Stream, metaclass=Final): this connection. Required. Usually created by calling :func:`ssl.create_default_context`. - server_hostname (str or None): The name of the server being connected - to. Used for `SNI + server_hostname (str, bytes, or None): The name of the server being + connected to. Used for `SNI `__ and for validating the server's certificate (if hostname checking is enabled). This is effectively mandatory for clients, and actually @@ -343,7 +343,7 @@ def __init__( transport_stream: Stream, ssl_context: _stdlib_ssl.SSLContext, *, - server_hostname: str | None = None, + server_hostname: str | bytes | None = None, server_side: bool = False, https_compatible: bool = False, ) -> None: @@ -357,7 +357,7 @@ def __init__( self._incoming, self._outgoing, server_side=server_side, - server_hostname=server_hostname, + server_hostname=server_hostname, # type: ignore[arg-type] # Typeshed bug, does accept bytes as well (typeshed#10590) ) # Tracks whether we've already done the initial handshake self._handshook = _Once(self._do_handshake) From 40e9bf293fb6ccefc0a7ada1ef1d486b3682a164 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:04:38 -0500 Subject: [PATCH 17/24] Update `verify_types.json` --- trio/_tests/verify_types.json | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index f0977acb7d..14b5b06860 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,11 +7,11 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9522292993630573, + "completenessScore": 0.9570063694267515, "exportedSymbolCounts": { "withAmbiguousType": 0, - "withKnownType": 598, - "withUnknownType": 30 + "withKnownType": 601, + "withUnknownType": 27 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, @@ -76,12 +76,9 @@ "trio.lowlevel.temporarily_detach_coroutine_object", "trio.lowlevel.wait_readable", "trio.lowlevel.wait_writable", - "trio.open_ssl_over_tcp_listeners", - "trio.open_ssl_over_tcp_stream", "trio.open_unix_socket", "trio.run_process", "trio.serve_listeners", - "trio.serve_ssl_over_tcp", "trio.testing._memory_streams.MemoryReceiveStream.__init__", "trio.testing._memory_streams.MemoryReceiveStream.aclose", "trio.testing._memory_streams.MemoryReceiveStream.close", From 3406eaf9c77c461ae999bf11a9787df82cc7fc11 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:13:28 -0500 Subject: [PATCH 18/24] Add `_highlevel_ssl_helpers` to stricter checks block --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 89a811ab1e..65bebbd545 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,7 @@ module = [ "trio._dtls", "trio._file_io", "trio._highlevel_open_tcp_stream", + "trio._highlevel_ssl_helpers", "trio._ki", "trio._socket", "trio._ssl", From 38c9186c139ac9eadfea02d7d3cd78b51d20b069 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:15:35 -0500 Subject: [PATCH 19/24] Revert "Add `_highlevel_ssl_helpers` to stricter checks block" This reverts commit 3406eaf9c77c461ae999bf11a9787df82cc7fc11. --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 65bebbd545..89a811ab1e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,7 +65,6 @@ module = [ "trio._dtls", "trio._file_io", "trio._highlevel_open_tcp_stream", - "trio._highlevel_ssl_helpers", "trio._ki", "trio._socket", "trio._ssl", From a583326e48f898dea78e3ffa6ecbd022486afa26 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:16:33 -0500 Subject: [PATCH 20/24] Revert changes to `_highlevel_ssl_helpers` Apparently already being handled in #2756 --- trio/_highlevel_ssl_helpers.py | 42 ++++++++++++++-------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/trio/_highlevel_ssl_helpers.py b/trio/_highlevel_ssl_helpers.py index c14b31b20a..ad77a302f0 100644 --- a/trio/_highlevel_ssl_helpers.py +++ b/trio/_highlevel_ssl_helpers.py @@ -1,7 +1,4 @@ -from __future__ import annotations - import ssl -from collections.abc import Awaitable, Callable import trio @@ -18,13 +15,13 @@ # So... let's punt on that for now. Hopefully we'll be getting a new Python # TLS API soon and can revisit this then. async def open_ssl_over_tcp_stream( - host: str | bytes, - port: int, + host, + port, *, - https_compatible: bool = False, - ssl_context: ssl.SSLContext | None = None, - happy_eyeballs_delay: float = DEFAULT_DELAY, -) -> trio.SSLStream: + https_compatible=False, + ssl_context=None, + happy_eyeballs_delay=DEFAULT_DELAY, +): """Make a TLS-encrypted Connection to the given host and port over TCP. This is a convenience wrapper that calls :func:`open_tcp_stream` and @@ -66,13 +63,8 @@ async def open_ssl_over_tcp_stream( async def open_ssl_over_tcp_listeners( - port: int, - ssl_context: ssl.SSLContext, - *, - host: str | bytes | None = None, - https_compatible: bool = False, - backlog: int | None = None, -) -> list[trio.SSLListener]: + port, ssl_context, *, host=None, https_compatible=False, backlog=None +): """Start listening for SSL/TLS-encrypted TCP connections to the given port. Args: @@ -94,16 +86,16 @@ async def open_ssl_over_tcp_listeners( async def serve_ssl_over_tcp( - handler: Callable[[trio.SocketStream], Awaitable[object]], - port: int, - ssl_context: ssl.SSLContext, + handler, + port, + ssl_context, *, - host: str | bytes | None = None, - https_compatible: bool = False, - backlog: int | None = None, - handler_nursery: trio.Nursery | None = None, - task_status: trio.TaskStatus[None] = trio.TASK_STATUS_IGNORED, -) -> None: + host=None, + https_compatible=False, + backlog=None, + handler_nursery=None, + task_status=trio.TASK_STATUS_IGNORED, +): """Listen for incoming TCP connections, and for each one start a task running ``handler(stream)``. From bf88b2667f2ad55c041999724436fa04f7954e9a Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:17:45 -0500 Subject: [PATCH 21/24] Revert "Update `verify_types.json`" This reverts commit 40e9bf293fb6ccefc0a7ada1ef1d486b3682a164. --- trio/_tests/verify_types.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index 14b5b06860..f0977acb7d 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,11 +7,11 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9570063694267515, + "completenessScore": 0.9522292993630573, "exportedSymbolCounts": { "withAmbiguousType": 0, - "withKnownType": 601, - "withUnknownType": 27 + "withKnownType": 598, + "withUnknownType": 30 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, @@ -76,9 +76,12 @@ "trio.lowlevel.temporarily_detach_coroutine_object", "trio.lowlevel.wait_readable", "trio.lowlevel.wait_writable", + "trio.open_ssl_over_tcp_listeners", + "trio.open_ssl_over_tcp_stream", "trio.open_unix_socket", "trio.run_process", "trio.serve_listeners", + "trio.serve_ssl_over_tcp", "trio.testing._memory_streams.MemoryReceiveStream.__init__", "trio.testing._memory_streams.MemoryReceiveStream.aclose", "trio.testing._memory_streams.MemoryReceiveStream.close", From b6e730328c305d9c6f6353a684a1eeadcefde312 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 19 Aug 2023 21:17:46 -0500 Subject: [PATCH 22/24] Update `verify_types.json` and fix line endings --- docs/source/conf.py | 2 +- pyproject.toml | 2 +- trio/_tests/verify_types.json | 26 ++++++-------------------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index db431169fe..b6d5e63043 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -280,4 +280,4 @@ def setup(app): "One line description of project.", "Miscellaneous", ), -] \ No newline at end of file +] diff --git a/pyproject.toml b/pyproject.toml index a72917671a..9157dd8b38 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -156,4 +156,4 @@ showcontent = true [[tool.towncrier.type]] directory = "misc" name = "Miscellaneous internal changes" -showcontent = true \ No newline at end of file +showcontent = true diff --git a/trio/_tests/verify_types.json b/trio/_tests/verify_types.json index b61b28a428..5fcd7580b5 100644 --- a/trio/_tests/verify_types.json +++ b/trio/_tests/verify_types.json @@ -7,11 +7,11 @@ "warningCount": 0 }, "typeCompleteness": { - "completenessScore": 0.9872611464968153, + "completenessScore": 0.9904458598726115, "exportedSymbolCounts": { "withAmbiguousType": 0, - "withKnownType": 620, - "withUnknownType": 8 + "withKnownType": 622, + "withUnknownType": 6 }, "ignoreUnknownTypesFromImports": true, "missingClassDocStringCount": 1, @@ -45,29 +45,15 @@ } ], "otherSymbolCounts": { - "withAmbiguousType": 1, - "withKnownType": 662, - "withUnknownType": 19 + "withAmbiguousType": 0, + "withKnownType": 678, + "withUnknownType": 4 }, "packageName": "trio", "symbols": [ "trio._highlevel_socket.SocketStream.getsockopt", "trio._highlevel_socket.SocketStream.send_all", "trio._highlevel_socket.SocketStream.setsockopt", - "trio._ssl.SSLListener.__init__", - "trio._ssl.SSLListener.accept", - "trio._ssl.SSLListener.aclose", - "trio._ssl.SSLStream.__dir__", - "trio._ssl.SSLStream.__getattr__", - "trio._ssl.SSLStream.__init__", - "trio._ssl.SSLStream.__setattr__", - "trio._ssl.SSLStream.aclose", - "trio._ssl.SSLStream.do_handshake", - "trio._ssl.SSLStream.receive_some", - "trio._ssl.SSLStream.send_all", - "trio._ssl.SSLStream.transport_stream", - "trio._ssl.SSLStream.unwrap", - "trio._ssl.SSLStream.wait_send_all_might_not_block", "trio.lowlevel.notify_closing", "trio.lowlevel.wait_readable", "trio.lowlevel.wait_writable", From cf7a777d580869c1c9ce5a9ba2c6f6049951dcc2 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:05:31 -0500 Subject: [PATCH 23/24] Remove slots from exception --- trio/_ssl.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/trio/_ssl.py b/trio/_ssl.py index 004e1ada2d..f0f01f7583 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -214,8 +214,6 @@ class NeedHandshakeError(Exception): """ - __slots__ = () - class _Once: def __init__(self, afn: Callable[..., Awaitable[object]], *args: object) -> None: From e79168357e64e919e2fef79e371aa567f7ba528e Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:08:19 -0500 Subject: [PATCH 24/24] Remove `_ssl` from not fully typed block --- pyproject.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9157dd8b38..043fb9ce76 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,8 +60,6 @@ disallow_untyped_calls = false # files not yet fully typed [[tool.mypy.overrides]] module = [ -# 2745 -"trio/_ssl", # 2756 "trio/_highlevel_open_unix_stream", "trio/_highlevel_serve_listeners",