From d90daea6afa5605f3cd578416965923b3c88a805 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:36:46 -0500 Subject: [PATCH 01/19] Enable flake8-type-checking rule --- docs/source/typevars.py | 9 +++-- pyproject.toml | 1 + trio/_channel.py | 3 +- trio/_core/_asyncgens.py | 2 +- trio/_core/_generated_instrumentation.py | 6 +++- trio/_core/_generated_run.py | 17 +++++---- trio/_core/_ki.py | 5 +-- trio/_core/_multierror.py | 3 +- trio/_core/_parking_lot.py | 3 +- trio/_core/_run.py | 19 +++++----- trio/_core/_tests/test_asyncgen.py | 6 ++-- trio/_core/_tests/test_io.py | 3 +- trio/_core/_tests/test_run.py | 18 +++++----- trio/_core/_tests/test_windows.py | 6 ++-- trio/_core/_tests/tutil.py | 4 ++- trio/_deprecate.py | 3 +- trio/_dtls.py | 2 +- trio/_highlevel_open_tcp_listeners.py | 5 ++- trio/_highlevel_open_tcp_stream.py | 6 ++-- trio/_highlevel_open_unix_stream.py | 6 ++-- trio/_highlevel_socket.py | 3 +- trio/_highlevel_ssl_helpers.py | 6 ++-- trio/_path.py | 10 +++--- trio/_signals.py | 5 +-- trio/_ssl.py | 6 ++-- trio/_subprocess.py | 7 ++-- trio/_subprocess_platform/__init__.py | 46 ++---------------------- trio/_subprocess_platform/windows.py | 6 +++- trio/_tests/check_type_completeness.py | 5 ++- trio/_tests/test_dtls.py | 6 ++-- trio/_tests/test_exports.py | 6 ++-- trio/_tests/test_signals.py | 6 ++-- trio/_tests/test_socket.py | 3 +- trio/_tests/tools/test_mypy_annotate.py | 5 ++- trio/_threads.py | 9 +++-- trio/_tools/gen_exports.py | 3 +- trio/_util.py | 3 +- trio/testing/_check_streams.py | 2 +- trio/testing/_checkpoints.py | 5 ++- trio/testing/_fake_net.py | 2 +- trio/testing/_trio_test.py | 3 +- 41 files changed, 150 insertions(+), 124 deletions(-) diff --git a/docs/source/typevars.py b/docs/source/typevars.py index c98f995a7d..4cf773d088 100644 --- a/docs/source/typevars.py +++ b/docs/source/typevars.py @@ -6,13 +6,16 @@ import re from pathlib import Path +from typing import TYPE_CHECKING import trio -from sphinx.addnodes import Element, pending_xref -from sphinx.application import Sphinx -from sphinx.environment import BuildEnvironment from sphinx.errors import NoUri +if TYPE_CHECKING: + from sphinx.addnodes import Element, pending_xref + from sphinx.application import Sphinx + from sphinx.environment import BuildEnvironment + def identify_typevars(trio_folder: Path) -> None: """Record all typevars in trio.""" diff --git a/pyproject.toml b/pyproject.toml index 73b110ebac..629032a39c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ select = [ "F", # pyflakes "I", # isort "YTT", # flake8-2020 + "TCH", # flake8-type-checking ] extend-ignore = [ 'F403', # undefined-local-with-import-star diff --git a/trio/_channel.py b/trio/_channel.py index 48f80daf1c..573acea04a 100644 --- a/trio/_channel.py +++ b/trio/_channel.py @@ -2,7 +2,6 @@ from collections import OrderedDict, deque from math import inf -from types import TracebackType from typing import ( TYPE_CHECKING, Generic, @@ -19,6 +18,8 @@ from ._util import NoPublicConstructor, final, generic_function if TYPE_CHECKING: + from types import TracebackType + from typing_extensions import Self diff --git a/trio/_core/_asyncgens.py b/trio/_core/_asyncgens.py index 4261328278..f26f5bd160 100644 --- a/trio/_core/_asyncgens.py +++ b/trio/_core/_asyncgens.py @@ -4,7 +4,6 @@ import sys import warnings import weakref -from types import AsyncGeneratorType from typing import TYPE_CHECKING, NoReturn import attr @@ -17,6 +16,7 @@ ASYNCGEN_LOGGER = logging.getLogger("trio.async_generator_errors") if TYPE_CHECKING: + from types import AsyncGeneratorType from typing import Set _WEAK_ASYNC_GEN_SET = weakref.WeakSet[AsyncGeneratorType[object, NoReturn]] diff --git a/trio/_core/_generated_instrumentation.py b/trio/_core/_generated_instrumentation.py index 652fed1288..fa389beb9e 100644 --- a/trio/_core/_generated_instrumentation.py +++ b/trio/_core/_generated_instrumentation.py @@ -3,10 +3,14 @@ # ************************************************************* from __future__ import annotations -from ._instrumentation import Instrument +from typing import TYPE_CHECKING + from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED from ._run import GLOBAL_RUN_CONTEXT +if TYPE_CHECKING: + from ._instrumentation import Instrument + def add_instrument(instrument: Instrument) -> None: """Start instrumenting the current run loop with the given instrument. diff --git a/trio/_core/_generated_run.py b/trio/_core/_generated_run.py index 3e1b7b78f1..beab7b067a 100644 --- a/trio/_core/_generated_run.py +++ b/trio/_core/_generated_run.py @@ -3,17 +3,20 @@ # ************************************************************* from __future__ import annotations -import contextvars -from collections.abc import Awaitable, Callable -from typing import Any +from typing import TYPE_CHECKING, Any -from outcome import Outcome - -from .._abc import Clock -from ._entry_queue import TrioToken from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED from ._run import _NO_SEND, GLOBAL_RUN_CONTEXT, RunStatistics, Task +if TYPE_CHECKING: + import contextvars + from collections.abc import Awaitable, Callable + + from outcome import Outcome + + from .._abc import Clock + from ._entry_queue import TrioToken + def current_statistics() -> RunStatistics: """Returns ``RunStatistics``, which contains run-loop-level debugging information. diff --git a/trio/_core/_ki.py b/trio/_core/_ki.py index 0ea34619b5..8be97d5e19 100644 --- a/trio/_core/_ki.py +++ b/trio/_core/_ki.py @@ -3,8 +3,6 @@ import inspect import signal import sys -import types -from collections.abc import Callable from functools import wraps from typing import TYPE_CHECKING, Final, Protocol, TypeVar @@ -16,6 +14,9 @@ RetT = TypeVar("RetT") if TYPE_CHECKING: + import types + from collections.abc import Callable + from typing_extensions import ParamSpec, TypeGuard ArgsT = ParamSpec("ArgsT") diff --git a/trio/_core/_multierror.py b/trio/_core/_multierror.py index 05eaad33e3..d4e03edb27 100644 --- a/trio/_core/_multierror.py +++ b/trio/_core/_multierror.py @@ -1,7 +1,6 @@ from __future__ import annotations import sys -from collections.abc import Callable, Sequence from types import TracebackType from typing import TYPE_CHECKING, Any, ClassVar, cast, overload @@ -13,6 +12,8 @@ from exceptiongroup import BaseExceptionGroup, ExceptionGroup if TYPE_CHECKING: + from collections.abc import Callable, Sequence + from typing_extensions import Self ################################################################ # MultiError diff --git a/trio/_core/_parking_lot.py b/trio/_core/_parking_lot.py index 5a38c3ad3b..0c46eb2537 100644 --- a/trio/_core/_parking_lot.py +++ b/trio/_core/_parking_lot.py @@ -73,7 +73,6 @@ import math from collections import OrderedDict -from collections.abc import Iterator from typing import TYPE_CHECKING import attr @@ -82,6 +81,8 @@ from .._util import final if TYPE_CHECKING: + from collections.abc import Iterator + from ._run import Task diff --git a/trio/_core/_run.py b/trio/_core/_run.py index da9caf536c..c616f5d659 100644 --- a/trio/_core/_run.py +++ b/trio/_core/_run.py @@ -10,20 +10,11 @@ import threading import warnings from collections import deque -from collections.abc import ( - Awaitable, - Callable, - Coroutine, - Generator, - Iterator, - Sequence, -) from contextlib import AbstractAsyncContextManager, contextmanager from contextvars import copy_context from heapq import heapify, heappop, heappush from math import inf from time import perf_counter -from types import TracebackType from typing import ( TYPE_CHECKING, Any, @@ -62,10 +53,18 @@ if sys.version_info < (3, 11): from exceptiongroup import BaseExceptionGroup -from types import FrameType if TYPE_CHECKING: import contextvars + from collections.abc import ( + Awaitable, + Callable, + Coroutine, + Generator, + Iterator, + Sequence, + ) + from types import FrameType, TracebackType from typing_extensions import Self diff --git a/trio/_core/_tests/test_asyncgen.py b/trio/_core/_tests/test_asyncgen.py index bc7848df8e..0c8030f20c 100644 --- a/trio/_core/_tests/test_asyncgen.py +++ b/trio/_core/_tests/test_asyncgen.py @@ -2,15 +2,17 @@ import sys import weakref -from collections.abc import AsyncGenerator from math import inf -from typing import NoReturn +from typing import TYPE_CHECKING, NoReturn import pytest from ... import _core from .tutil import buggy_pypy_asyncgens, gc_collect_harder, restore_unraisablehook +if TYPE_CHECKING: + from collections.abc import AsyncGenerator + def test_asyncgen_basics() -> None: collected = [] diff --git a/trio/_core/_tests/test_io.py b/trio/_core/_tests/test_io.py index 039dfbef01..6e88201a11 100644 --- a/trio/_core/_tests/test_io.py +++ b/trio/_core/_tests/test_io.py @@ -2,7 +2,6 @@ import random import socket as stdlib_socket -from collections.abc import Generator from contextlib import suppress from typing import TYPE_CHECKING, Awaitable, Callable, Tuple, TypeVar @@ -16,6 +15,8 @@ # Cross-platform tests for IO handling if TYPE_CHECKING: + from collections.abc import Generator + from typing_extensions import ParamSpec ArgsT = ParamSpec("ArgsT") diff --git a/trio/_core/_tests/test_run.py b/trio/_core/_tests/test_run.py index f67f83a4b8..d03e74079b 100644 --- a/trio/_core/_tests/test_run.py +++ b/trio/_core/_tests/test_run.py @@ -8,16 +8,9 @@ import time import types import weakref -from collections.abc import ( - AsyncGenerator, - AsyncIterator, - Awaitable, - Callable, - Generator, -) from contextlib import ExitStack, contextmanager from math import inf -from typing import NoReturn, TypeVar +from typing import TYPE_CHECKING, NoReturn, TypeVar import outcome import pytest @@ -39,6 +32,15 @@ slow, ) +if TYPE_CHECKING: + from collections.abc import ( + AsyncGenerator, + AsyncIterator, + Awaitable, + Callable, + Generator, + ) + if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/trio/_core/_tests/test_windows.py b/trio/_core/_tests/test_windows.py index f0961cf9e4..ca3fea7206 100644 --- a/trio/_core/_tests/test_windows.py +++ b/trio/_core/_tests/test_windows.py @@ -3,9 +3,7 @@ import os import sys import tempfile -from collections.abc import Generator from contextlib import contextmanager -from io import BufferedWriter from typing import TYPE_CHECKING from unittest.mock import create_autospec @@ -23,6 +21,10 @@ from ...testing import wait_all_tasks_blocked from .tutil import gc_collect_harder, restore_unraisablehook, slow +if TYPE_CHECKING: + from collections.abc import Generator + from io import BufferedWriter + if on_windows: from .._windows_cffi import ( INVALID_HANDLE_VALUE, diff --git a/trio/_core/_tests/tutil.py b/trio/_core/_tests/tutil.py index 070af8ed15..94565e35a8 100644 --- a/trio/_core/_tests/tutil.py +++ b/trio/_core/_tests/tutil.py @@ -7,7 +7,6 @@ import socket as stdlib_socket import sys import warnings -from collections.abc import Generator, Iterable, Sequence from contextlib import closing, contextmanager from typing import TYPE_CHECKING @@ -16,6 +15,9 @@ # See trio/_tests/conftest.py for the other half of this from trio._tests.pytest_plugin import RUN_SLOW +if TYPE_CHECKING: + from collections.abc import Generator, Iterable, Sequence + slow = pytest.mark.skipif(not RUN_SLOW, reason="use --run-slow to run slow tests") # PyPy 7.2 was released with a bug that just never called the async diff --git a/trio/_deprecate.py b/trio/_deprecate.py index 0a9553b854..06fba43a87 100644 --- a/trio/_deprecate.py +++ b/trio/_deprecate.py @@ -2,7 +2,6 @@ import sys import warnings -from collections.abc import Callable from functools import wraps from types import ModuleType from typing import TYPE_CHECKING, ClassVar, TypeVar @@ -10,6 +9,8 @@ import attr if TYPE_CHECKING: + from collections.abc import Callable + from typing_extensions import ParamSpec ArgsT = ParamSpec("ArgsT") diff --git a/trio/_dtls.py b/trio/_dtls.py index d6baebafbc..78e97d6460 100644 --- a/trio/_dtls.py +++ b/trio/_dtls.py @@ -30,6 +30,7 @@ from weakref import ReferenceType, WeakValueDictionary import attr +from OpenSSL import SSL import trio @@ -39,7 +40,6 @@ from types import TracebackType # See DTLSEndpoint.__init__ for why this is imported here - from OpenSSL import SSL from OpenSSL.SSL import Context from typing_extensions import Self, TypeAlias diff --git a/trio/_highlevel_open_tcp_listeners.py b/trio/_highlevel_open_tcp_listeners.py index 019505c827..cf783cbedd 100644 --- a/trio/_highlevel_open_tcp_listeners.py +++ b/trio/_highlevel_open_tcp_listeners.py @@ -2,14 +2,17 @@ import errno import sys -from collections.abc import Awaitable, Callable from math import inf +from typing import TYPE_CHECKING import trio from trio import TaskStatus from . import socket as tsocket +if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/trio/_highlevel_open_tcp_stream.py b/trio/_highlevel_open_tcp_stream.py index 43c0ee2b45..2475b793ea 100644 --- a/trio/_highlevel_open_tcp_stream.py +++ b/trio/_highlevel_open_tcp_stream.py @@ -1,15 +1,17 @@ from __future__ import annotations import sys -from collections.abc import Generator from contextlib import contextmanager -from socket import AddressFamily, SocketKind from typing import TYPE_CHECKING, Any import trio from trio._core._multierror import MultiError from trio.socket import SOCK_STREAM, SocketType, getaddrinfo, socket +if TYPE_CHECKING: + from collections.abc import Generator + from socket import AddressFamily, SocketKind + if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/trio/_highlevel_open_unix_stream.py b/trio/_highlevel_open_unix_stream.py index c05b8f3fc8..3c610947b3 100644 --- a/trio/_highlevel_open_unix_stream.py +++ b/trio/_highlevel_open_unix_stream.py @@ -1,13 +1,15 @@ from __future__ import annotations import os -from collections.abc import Generator from contextlib import contextmanager -from typing import Protocol, TypeVar +from typing import TYPE_CHECKING, Protocol, TypeVar import trio from trio.socket import SOCK_STREAM, socket +if TYPE_CHECKING: + from collections.abc import Generator + class Closable(Protocol): def close(self) -> None: diff --git a/trio/_highlevel_socket.py b/trio/_highlevel_socket.py index 9ee8aa2249..f287f05dc6 100644 --- a/trio/_highlevel_socket.py +++ b/trio/_highlevel_socket.py @@ -2,7 +2,6 @@ from __future__ import annotations import errno -from collections.abc import Generator from contextlib import contextmanager from typing import TYPE_CHECKING, overload @@ -13,6 +12,8 @@ from .abc import HalfCloseableStream, Listener if TYPE_CHECKING: + from collections.abc import Generator + from typing_extensions import Buffer from ._socket import SocketType diff --git a/trio/_highlevel_ssl_helpers.py b/trio/_highlevel_ssl_helpers.py index 1647f373c2..ae7e47e6a4 100644 --- a/trio/_highlevel_ssl_helpers.py +++ b/trio/_highlevel_ssl_helpers.py @@ -1,13 +1,15 @@ from __future__ import annotations import ssl -from collections.abc import Awaitable, Callable -from typing import NoReturn +from typing import TYPE_CHECKING, NoReturn import trio from ._highlevel_open_tcp_stream import DEFAULT_DELAY +if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + # It might have been nice to take a ssl_protocols= argument here to set up # NPN/ALPN, but to do this we have to mutate the context object, which is OK diff --git a/trio/_path.py b/trio/_path.py index b1cb623190..74cfa04d5a 100644 --- a/trio/_path.py +++ b/trio/_path.py @@ -3,11 +3,8 @@ import inspect import os import pathlib -import sys import types -from collections.abc import Awaitable, Callable, Iterable from functools import partial -from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper from typing import ( IO, TYPE_CHECKING, @@ -21,10 +18,13 @@ ) import trio -from trio._file_io import AsyncIOWrapper as _AsyncIOWrapper from trio._util import async_wraps, final, wraps if TYPE_CHECKING: + import sys + from collections.abc import Awaitable, Callable, Iterable + from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper + from _typeshed import ( OpenBinaryMode, OpenBinaryModeReading, @@ -34,6 +34,8 @@ ) from typing_extensions import Concatenate, Literal, ParamSpec, TypeAlias + from trio._file_io import AsyncIOWrapper as _AsyncIOWrapper + P = ParamSpec("P") T = TypeVar("T") diff --git a/trio/_signals.py b/trio/_signals.py index 283c3a44a8..f4d912808f 100644 --- a/trio/_signals.py +++ b/trio/_signals.py @@ -2,9 +2,7 @@ import signal from collections import OrderedDict -from collections.abc import AsyncIterator, Callable, Generator, Iterable from contextlib import contextmanager -from types import FrameType from typing import TYPE_CHECKING import trio @@ -12,6 +10,9 @@ from ._util import ConflictDetector, is_main_thread, signal_raise if TYPE_CHECKING: + from collections.abc import AsyncIterator, Callable, Generator, Iterable + from types import FrameType + from typing_extensions import Self # Discussion of signal handling strategies: diff --git a/trio/_ssl.py b/trio/_ssl.py index 21c4deabb3..5c52b3f807 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -2,9 +2,8 @@ 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, ClassVar, Final as TFinal, TypeVar +from typing import TYPE_CHECKING, Any, ClassVar, Final as TFinal, TypeVar import trio @@ -13,6 +12,9 @@ from ._util import ConflictDetector, final from .abc import Listener, Stream +if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + # General theory of operation: # # We implement an API that closely mirrors the stdlib ssl module's blocking diff --git a/trio/_subprocess.py b/trio/_subprocess.py index 94720b8562..379e6ee390 100644 --- a/trio/_subprocess.py +++ b/trio/_subprocess.py @@ -1,14 +1,11 @@ from __future__ import annotations import os -import signal import subprocess import sys import warnings -from collections.abc import Awaitable, Callable, Mapping, Sequence from contextlib import ExitStack from functools import partial -from io import TextIOWrapper from typing import TYPE_CHECKING, Final, Literal, Protocol, Union, overload import trio @@ -26,6 +23,10 @@ from ._util import NoPublicConstructor, final if TYPE_CHECKING: + import signal + from collections.abc import Awaitable, Callable, Mapping, Sequence + from io import TextIOWrapper + from typing_extensions import TypeAlias diff --git a/trio/_subprocess_platform/__init__.py b/trio/_subprocess_platform/__init__.py index 793d8d7f23..b0097b4537 100644 --- a/trio/_subprocess_platform/__init__.py +++ b/trio/_subprocess_platform/__init__.py @@ -1,13 +1,9 @@ # Platform-specific subprocess bits'n'pieces. -import os import sys from typing import TYPE_CHECKING, Optional, Tuple -import trio - from .. import _core, _subprocess -from .._abc import ReceiveStream, SendStream _wait_child_exiting_error: Optional[ImportError] = None _create_child_pipe_error: Optional[ImportError] = None @@ -15,6 +11,8 @@ if TYPE_CHECKING: # internal types for the pipe representations used in type checking only + from .._abc import ReceiveStream, SendStream + class ClosableSendStream(SendStream): def close(self) -> None: ... @@ -80,45 +78,7 @@ def create_pipe_from_child_output() -> Tuple["ClosableReceiveStream", int]: _wait_child_exiting_error = ex try: - if TYPE_CHECKING: - # Not worth type checking these definitions - pass - - elif os.name == "posix": - - def create_pipe_to_child_stdin(): # noqa: F811 - rfd, wfd = os.pipe() - return trio.lowlevel.FdStream(wfd), rfd - - def create_pipe_from_child_output(): # noqa: F811 - rfd, wfd = os.pipe() - return trio.lowlevel.FdStream(rfd), wfd - - elif os.name == "nt": - import msvcrt - - # This isn't exported or documented, but it's also not - # underscore-prefixed, and seems kosher to use. The asyncio docs - # for 3.5 included an example that imported socketpair from - # windows_utils (before socket.socketpair existed on Windows), and - # when asyncio.windows_utils.socketpair was removed in 3.7, the - # removal was mentioned in the release notes. - from asyncio.windows_utils import pipe as windows_pipe - - from .._windows_pipes import PipeReceiveStream, PipeSendStream - - def create_pipe_to_child_stdin(): - # for stdin, we want the write end (our end) to use overlapped I/O - rh, wh = windows_pipe(overlapped=(False, True)) - return PipeSendStream(wh), msvcrt.open_osfhandle(rh, os.O_RDONLY) - - def create_pipe_from_child_output(): - # for stdout/err, it's the read end that's overlapped - rh, wh = windows_pipe(overlapped=(True, False)) - return PipeReceiveStream(rh), msvcrt.open_osfhandle(wh, 0) - - else: # pragma: no cover - raise ImportError("pipes not implemented on this platform") + pass except ImportError as ex: # pragma: no cover _create_child_pipe_error = ex diff --git a/trio/_subprocess_platform/windows.py b/trio/_subprocess_platform/windows.py index 1634e74fa7..81fb960e4b 100644 --- a/trio/_subprocess_platform/windows.py +++ b/trio/_subprocess_platform/windows.py @@ -1,6 +1,10 @@ -from .. import _subprocess +from typing import TYPE_CHECKING + from .._wait_for_object import WaitForSingleObject +if TYPE_CHECKING: + from .. import _subprocess + async def wait_child_exiting(process: "_subprocess.Process") -> None: # _handle is not in Popen stubs, though it is present on Windows. diff --git a/trio/_tests/check_type_completeness.py b/trio/_tests/check_type_completeness.py index 233a2ab5dc..44bf3cc540 100755 --- a/trio/_tests/check_type_completeness.py +++ b/trio/_tests/check_type_completeness.py @@ -6,8 +6,11 @@ import json import subprocess import sys -from collections.abc import Mapping from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Mapping # the result file is not marked in MANIFEST.in so it's not included in the package failed = False diff --git a/trio/_tests/test_dtls.py b/trio/_tests/test_dtls.py index a077451c4c..2e4f697302 100644 --- a/trio/_tests/test_dtls.py +++ b/trio/_tests/test_dtls.py @@ -1,10 +1,9 @@ from __future__ import annotations import random -from collections.abc import AsyncGenerator from contextlib import asynccontextmanager from itertools import count -from typing import NoReturn +from typing import TYPE_CHECKING, NoReturn import attr import pytest @@ -25,6 +24,9 @@ from .._core._tests.tutil import binds_ipv6, gc_collect_harder, slow +if TYPE_CHECKING: + from collections.abc import AsyncGenerator + ca = trustme.CA() server_cert = ca.issue_cert("example.com") diff --git a/trio/_tests/test_exports.py b/trio/_tests/test_exports.py index c3d8a03b63..100c0354b4 100644 --- a/trio/_tests/test_exports.py +++ b/trio/_tests/test_exports.py @@ -9,10 +9,9 @@ import socket as stdlib_socket import sys import types -from collections.abc import Iterator from pathlib import Path from types import ModuleType -from typing import Protocol +from typing import TYPE_CHECKING, Protocol import attrs import pytest @@ -25,6 +24,9 @@ from .._core._tests.tutil import slow from .pytest_plugin import RUN_SLOW +if TYPE_CHECKING: + from collections.abc import Iterator + mypy_cache_updated = False diff --git a/trio/_tests/test_signals.py b/trio/_tests/test_signals.py index d0f1bd1c74..6caadc3f8b 100644 --- a/trio/_tests/test_signals.py +++ b/trio/_tests/test_signals.py @@ -1,8 +1,7 @@ from __future__ import annotations import signal -from types import FrameType -from typing import NoReturn +from typing import TYPE_CHECKING, NoReturn import pytest @@ -12,6 +11,9 @@ from .._signals import _signal_handler, get_pending_signal_count, open_signal_receiver from .._util import signal_raise +if TYPE_CHECKING: + from types import FrameType + async def test_open_signal_receiver() -> None: orig = signal.getsignal(signal.SIGILL) diff --git a/trio/_tests/test_socket.py b/trio/_tests/test_socket.py index 40ffefb8cd..6a131c9fed 100644 --- a/trio/_tests/test_socket.py +++ b/trio/_tests/test_socket.py @@ -14,13 +14,14 @@ from .. import _core, socket as tsocket from .._core._tests.tutil import binds_ipv6, creates_ipv6 -from .._highlevel_socket import SocketStream from .._socket import _NUMERIC_ONLY, SocketType, _SocketType, _try_sync from ..testing import assert_checkpoints, wait_all_tasks_blocked if TYPE_CHECKING: from typing_extensions import TypeAlias + from .._highlevel_socket import SocketStream + GaiTuple: TypeAlias = Tuple[ AddressFamily, SocketKind, diff --git a/trio/_tests/tools/test_mypy_annotate.py b/trio/_tests/tools/test_mypy_annotate.py index 28ebebb592..a83116dd78 100644 --- a/trio/_tests/tools/test_mypy_annotate.py +++ b/trio/_tests/tools/test_mypy_annotate.py @@ -2,12 +2,15 @@ import io import sys -from pathlib import Path +from typing import TYPE_CHECKING import pytest from trio._tools.mypy_annotate import Result, export, main, process_line +if TYPE_CHECKING: + from pathlib import Path + @pytest.mark.parametrize( "src, expected", diff --git a/trio/_threads.py b/trio/_threads.py index 24905cfbde..4b70c26b12 100644 --- a/trio/_threads.py +++ b/trio/_threads.py @@ -5,16 +5,14 @@ import inspect import queue as stdlib_queue import threading -from collections.abc import Awaitable, Callable from itertools import count -from typing import TypeVar +from typing import TYPE_CHECKING, TypeVar import attr import outcome from sniffio import current_async_library_cvar import trio -from trio._core._traps import RaiseCancelT from ._core import ( RunVar, @@ -26,6 +24,11 @@ from ._sync import CapacityLimiter from ._util import coroutine_or_error +if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + + from trio._core._traps import RaiseCancelT + RetT = TypeVar("RetT") Ret2T = TypeVar("Ret2T") diff --git a/trio/_tools/gen_exports.py b/trio/_tools/gen_exports.py index 153070bfc9..a8c793634f 100755 --- a/trio/_tools/gen_exports.py +++ b/trio/_tools/gen_exports.py @@ -10,7 +10,6 @@ import os import subprocess import sys -from collections.abc import Iterable, Iterator from pathlib import Path from textwrap import indent from typing import TYPE_CHECKING @@ -18,6 +17,8 @@ import attr if TYPE_CHECKING: + from collections.abc import Iterable, Iterator + from typing_extensions import TypeGuard # keep these imports up to date with conditional imports in test_gen_exports diff --git a/trio/_util.py b/trio/_util.py index 36020ca2d7..eefe5cf663 100644 --- a/trio/_util.py +++ b/trio/_util.py @@ -9,7 +9,6 @@ import typing as t from abc import ABCMeta from functools import update_wrapper -from types import AsyncGeneratorType, TracebackType from sniffio import thread_local as sniffio_loop @@ -20,6 +19,8 @@ RetT = t.TypeVar("RetT") if t.TYPE_CHECKING: + from types import AsyncGeneratorType, TracebackType + from typing_extensions import ParamSpec, Self ArgsT = ParamSpec("ArgsT") diff --git a/trio/testing/_check_streams.py b/trio/testing/_check_streams.py index 33947ccc55..3692a4076f 100644 --- a/trio/testing/_check_streams.py +++ b/trio/testing/_check_streams.py @@ -2,7 +2,6 @@ from __future__ import annotations import random -from collections.abc import Generator from contextlib import contextmanager from typing import TYPE_CHECKING, Awaitable, Callable, Generic, Tuple, TypeVar @@ -12,6 +11,7 @@ from ._checkpoints import assert_checkpoints if TYPE_CHECKING: + from collections.abc import Generator from types import TracebackType from typing_extensions import ParamSpec, TypeAlias diff --git a/trio/testing/_checkpoints.py b/trio/testing/_checkpoints.py index 4a4047813b..e51463f071 100644 --- a/trio/testing/_checkpoints.py +++ b/trio/testing/_checkpoints.py @@ -1,10 +1,13 @@ from __future__ import annotations -from collections.abc import Generator from contextlib import AbstractContextManager, contextmanager +from typing import TYPE_CHECKING from .. import _core +if TYPE_CHECKING: + from collections.abc import Generator + @contextmanager def _assert_yields_or_not(expected: bool) -> Generator[None, None, None]: diff --git a/trio/testing/_fake_net.py b/trio/testing/_fake_net.py index 74ce32d37f..5735cfc47f 100644 --- a/trio/testing/_fake_net.py +++ b/trio/testing/_fake_net.py @@ -8,7 +8,6 @@ from __future__ import annotations -import builtins import errno import ipaddress import os @@ -20,6 +19,7 @@ from trio._util import NoPublicConstructor, final if TYPE_CHECKING: + import builtins from socket import AddressFamily, SocketKind from types import TracebackType diff --git a/trio/testing/_trio_test.py b/trio/testing/_trio_test.py index 5619352846..a57c0ee4c7 100644 --- a/trio/testing/_trio_test.py +++ b/trio/testing/_trio_test.py @@ -1,6 +1,5 @@ from __future__ import annotations -from collections.abc import Awaitable, Callable from functools import partial, wraps from typing import TYPE_CHECKING, TypeVar @@ -8,6 +7,8 @@ from ..abc import Clock, Instrument if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + from typing_extensions import ParamSpec ArgsT = ParamSpec("ArgsT") From 2c9a268d1c82789ee3508006882a115ee6094dce Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:48:33 -0500 Subject: [PATCH 02/19] Add noqa to special SSL import --- trio/_dtls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trio/_dtls.py b/trio/_dtls.py index 78e97d6460..724f3057ac 100644 --- a/trio/_dtls.py +++ b/trio/_dtls.py @@ -30,7 +30,6 @@ from weakref import ReferenceType, WeakValueDictionary import attr -from OpenSSL import SSL import trio @@ -40,6 +39,7 @@ from types import TracebackType # See DTLSEndpoint.__init__ for why this is imported here + from OpenSSL import SSL # noqa: TCH004 from OpenSSL.SSL import Context from typing_extensions import Self, TypeAlias From 0c06a558a7098f9587b545cbfb6b50cbd69326d2 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:56:14 -0500 Subject: [PATCH 03/19] Revert changes and add noqa directives to `_subprocess_platform` --- trio/_subprocess_platform/__init__.py | 46 +++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/trio/_subprocess_platform/__init__.py b/trio/_subprocess_platform/__init__.py index b0097b4537..88ff251166 100644 --- a/trio/_subprocess_platform/__init__.py +++ b/trio/_subprocess_platform/__init__.py @@ -1,9 +1,13 @@ # Platform-specific subprocess bits'n'pieces. +import os import sys from typing import TYPE_CHECKING, Optional, Tuple +import trio + from .. import _core, _subprocess +from .._abc import ReceiveStream, SendStream # noqa: TCH001 _wait_child_exiting_error: Optional[ImportError] = None _create_child_pipe_error: Optional[ImportError] = None @@ -11,8 +15,6 @@ if TYPE_CHECKING: # internal types for the pipe representations used in type checking only - from .._abc import ReceiveStream, SendStream - class ClosableSendStream(SendStream): def close(self) -> None: ... @@ -78,7 +80,45 @@ def create_pipe_from_child_output() -> Tuple["ClosableReceiveStream", int]: _wait_child_exiting_error = ex try: - pass + if TYPE_CHECKING: + # Not worth type checking these definitions + pass # noqa: TCH005 + + elif os.name == "posix": + + def create_pipe_to_child_stdin(): # noqa: F811 + rfd, wfd = os.pipe() + return trio.lowlevel.FdStream(wfd), rfd + + def create_pipe_from_child_output(): # noqa: F811 + rfd, wfd = os.pipe() + return trio.lowlevel.FdStream(rfd), wfd + + elif os.name == "nt": + import msvcrt + + # This isn't exported or documented, but it's also not + # underscore-prefixed, and seems kosher to use. The asyncio docs + # for 3.5 included an example that imported socketpair from + # windows_utils (before socket.socketpair existed on Windows), and + # when asyncio.windows_utils.socketpair was removed in 3.7, the + # removal was mentioned in the release notes. + from asyncio.windows_utils import pipe as windows_pipe + + from .._windows_pipes import PipeReceiveStream, PipeSendStream + + def create_pipe_to_child_stdin(): + # for stdin, we want the write end (our end) to use overlapped I/O + rh, wh = windows_pipe(overlapped=(False, True)) + return PipeSendStream(wh), msvcrt.open_osfhandle(rh, os.O_RDONLY) + + def create_pipe_from_child_output(): + # for stdout/err, it's the read end that's overlapped + rh, wh = windows_pipe(overlapped=(True, False)) + return PipeReceiveStream(rh), msvcrt.open_osfhandle(wh, 0) + + else: # pragma: no cover + raise ImportError("pipes not implemented on this platform") except ImportError as ex: # pragma: no cover _create_child_pipe_error = ex From f3f442adb6680e84f28b09938bfdc580a07f30ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:48:24 +0000 Subject: [PATCH 04/19] [pre-commit.ci] auto fixes from pre-commit.com hooks --- trio/_path.py | 1 - 1 file changed, 1 deletion(-) diff --git a/trio/_path.py b/trio/_path.py index 4b63ca59ee..a728f6c731 100644 --- a/trio/_path.py +++ b/trio/_path.py @@ -5,7 +5,6 @@ import pathlib import types from collections.abc import Awaitable, Callable, Iterable, Sequence - from functools import partial from typing import ( IO, From 4b87abcbf87a7f93d0e8eaee32551a5572b810d6 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:53:32 -0500 Subject: [PATCH 05/19] Fix ruff errors --- trio/_path.py | 5 ++--- trio/_tests/test_path.py | 6 ++++-- trio/_threads.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/trio/_path.py b/trio/_path.py index a728f6c731..cd866da145 100644 --- a/trio/_path.py +++ b/trio/_path.py @@ -3,8 +3,8 @@ import inspect import os import pathlib +import sys import types -from collections.abc import Awaitable, Callable, Iterable, Sequence from functools import partial from typing import ( IO, @@ -22,8 +22,7 @@ from trio._util import async_wraps, final, wraps if TYPE_CHECKING: - import sys - from collections.abc import Awaitable, Callable, Iterable + from collections.abc import Awaitable, Callable, Iterable, Sequence from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper from _typeshed import ( diff --git a/trio/_tests/test_path.py b/trio/_tests/test_path.py index bade99739d..48d09f73c8 100644 --- a/trio/_tests/test_path.py +++ b/trio/_tests/test_path.py @@ -2,8 +2,7 @@ import os import pathlib -from collections.abc import Awaitable, Callable -from typing import Any, Type, Union +from typing import TYPE_CHECKING, Any, Type, Union import pytest @@ -11,6 +10,9 @@ from trio._file_io import AsyncIOWrapper from trio._path import AsyncAutoWrapperType as WrapperType +if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + @pytest.fixture def path(tmp_path: pathlib.Path) -> trio.Path: diff --git a/trio/_threads.py b/trio/_threads.py index 2e35634fdc..08ef13d32e 100644 --- a/trio/_threads.py +++ b/trio/_threads.py @@ -415,7 +415,7 @@ def from_thread_check_cancelled() -> None: except AttributeError: raise RuntimeError( "this thread wasn't created by Trio, can't check for cancellation" - ) + ) from None if raise_cancel is not None: raise_cancel() From 1f3501567b2e888fa8dffc1b8601ce5275aa3f02 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:03:29 -0500 Subject: [PATCH 06/19] Enabling `unsafe-fixes` seems to eliminate errors even though no change results. Strange. --- trio/_tools/gen_exports.py | 1 + 1 file changed, 1 insertion(+) diff --git a/trio/_tools/gen_exports.py b/trio/_tools/gen_exports.py index 81dedac9f0..262a6d4672 100755 --- a/trio/_tools/gen_exports.py +++ b/trio/_tools/gen_exports.py @@ -157,6 +157,7 @@ def run_ruff(file: File, source: str) -> tuple[bool, str]: "ruff", "check", "--fix", + "--unsafe-fixes", "--output-format=text", "--stdin-filename", file.path, From 4764ba225cbd91f20ed99069cabdf8a284f673c9 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:09:08 -0500 Subject: [PATCH 07/19] Set the `SPHINX_AUTODOC_RELOAD_MODULES` flag to 1 --- docs/source/conf.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/source/conf.py b/docs/source/conf.py index 06b661f126..48eaaf32be 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -24,6 +24,9 @@ # For trio itself sys.path.insert(0, os.path.abspath("../..")) +# Enable reloading with `typing.TYPE_CHECKING` being True +os.environ["SPHINX_AUTODOC_RELOAD_MODULES"] = "1" + # https://docs.readthedocs.io/en/stable/builds.html#build-environment if "READTHEDOCS" in os.environ: import glob From 4e53f31230c951ade8191a5c4082bc7245661cc6 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Mon, 30 Oct 2023 22:21:21 -0500 Subject: [PATCH 08/19] Fix merge issues --- trio/_core/_run.py | 8 -------- trio/_core/_tests/test_guest_mode.py | 3 ++- trio/_core/_tests/test_instrumentation.py | 6 ++++-- trio/_core/_tests/test_multierror.py | 6 ++++-- trio/_core/_tests/test_run.py | 7 ------- trio/_core/_tests/test_thread_cache.py | 6 ++++-- trio/_highlevel_open_tcp_listeners.py | 2 -- trio/_highlevel_open_tcp_stream.py | 2 -- trio/_highlevel_socket.py | 1 - trio/_highlevel_ssl_helpers.py | 7 +++---- trio/_tests/test_exports.py | 2 +- trio/_tests/test_highlevel_serve_listeners.py | 8 +++++--- trio/_tests/test_highlevel_ssl_helpers.py | 16 ++++++++++------ trio/_tests/test_scheduler_determinism.py | 5 ++++- trio/_tests/test_ssl.py | 8 +++++--- trio/_tests/test_subprocess.py | 6 ++++-- trio/_tests/test_testing.py | 8 +++++--- trio/_tests/test_threads.py | 8 ++++++-- trio/testing/_check_streams.py | 1 - trio/testing/_fake_net.py | 1 - 20 files changed, 57 insertions(+), 54 deletions(-) diff --git a/trio/_core/_run.py b/trio/_core/_run.py index ff17c456a4..1c1bd4e2e4 100644 --- a/trio/_core/_run.py +++ b/trio/_core/_run.py @@ -10,14 +10,6 @@ import threading import warnings from collections import deque -from collections.abc import ( - Awaitable, - Callable, - Coroutine, - Generator, - Iterator, - Sequence, -) from contextlib import AbstractAsyncContextManager, contextmanager, suppress from contextvars import copy_context from heapq import heapify, heappop, heappush diff --git a/trio/_core/_tests/test_guest_mode.py b/trio/_core/_tests/test_guest_mode.py index c1a9b11815..8d63d5246b 100644 --- a/trio/_core/_tests/test_guest_mode.py +++ b/trio/_core/_tests/test_guest_mode.py @@ -29,7 +29,6 @@ import trio import trio.testing -from trio._channel import MemorySendChannel from trio.abc import Instrument from ..._util import signal_raise @@ -38,6 +37,8 @@ if TYPE_CHECKING: from typing_extensions import TypeAlias + from trio._channel import MemorySendChannel + T = TypeVar("T") InHost: TypeAlias = Callable[[object], None] diff --git a/trio/_core/_tests/test_instrumentation.py b/trio/_core/_tests/test_instrumentation.py index f743f2b3d4..b13a852fae 100644 --- a/trio/_core/_tests/test_instrumentation.py +++ b/trio/_core/_tests/test_instrumentation.py @@ -1,14 +1,16 @@ from __future__ import annotations -from typing import Container, Iterable, NoReturn +from typing import TYPE_CHECKING, Container, Iterable, NoReturn import attr import pytest from ... import _abc, _core -from ...lowlevel import Task from .tutil import check_sequence_matches +if TYPE_CHECKING: + from ...lowlevel import Task + @attr.s(eq=False, hash=False) class TaskRecorder(_abc.Instrument): diff --git a/trio/_core/_tests/test_multierror.py b/trio/_core/_tests/test_multierror.py index d1cd0bd4a1..505b371f41 100644 --- a/trio/_core/_tests/test_multierror.py +++ b/trio/_core/_tests/test_multierror.py @@ -9,8 +9,7 @@ import warnings from pathlib import Path from traceback import extract_tb, print_exception -from types import TracebackType -from typing import Callable, NoReturn +from typing import TYPE_CHECKING, Callable, NoReturn import pytest @@ -19,6 +18,9 @@ from .._multierror import MultiError, NonBaseMultiError, concat_tb from .tutil import slow +if TYPE_CHECKING: + from types import TracebackType + if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/trio/_core/_tests/test_run.py b/trio/_core/_tests/test_run.py index bb8f31c372..079be056e2 100644 --- a/trio/_core/_tests/test_run.py +++ b/trio/_core/_tests/test_run.py @@ -8,13 +8,6 @@ import time import types import weakref -from collections.abc import ( - AsyncGenerator, - AsyncIterator, - Awaitable, - Callable, - Generator, -) from contextlib import ExitStack, contextmanager, suppress from math import inf from typing import TYPE_CHECKING, Any, NoReturn, TypeVar, cast diff --git a/trio/_core/_tests/test_thread_cache.py b/trio/_core/_tests/test_thread_cache.py index 77fdf46664..a0711b1849 100644 --- a/trio/_core/_tests/test_thread_cache.py +++ b/trio/_core/_tests/test_thread_cache.py @@ -4,16 +4,18 @@ import time from contextlib import contextmanager from queue import Queue -from typing import Iterator, NoReturn +from typing import TYPE_CHECKING, Iterator, NoReturn import pytest -from outcome import Outcome from pytest import MonkeyPatch from .. import _thread_cache from .._thread_cache import ThreadCache, start_thread_soon from .tutil import gc_collect_harder, slow +if TYPE_CHECKING: + from outcome import Outcome + def test_thread_cache_basics() -> None: q: Queue[Outcome[object]] = Queue() diff --git a/trio/_highlevel_open_tcp_listeners.py b/trio/_highlevel_open_tcp_listeners.py index 12969c23e6..bc0dbddd46 100644 --- a/trio/_highlevel_open_tcp_listeners.py +++ b/trio/_highlevel_open_tcp_listeners.py @@ -3,9 +3,7 @@ import errno import math import sys -from math import inf from typing import TYPE_CHECKING -from collections.abc import Awaitable, Callable import trio from trio import TaskStatus diff --git a/trio/_highlevel_open_tcp_stream.py b/trio/_highlevel_open_tcp_stream.py index 68323f94bd..f6f8aba425 100644 --- a/trio/_highlevel_open_tcp_stream.py +++ b/trio/_highlevel_open_tcp_stream.py @@ -1,9 +1,7 @@ from __future__ import annotations import sys -from collections.abc import Generator from contextlib import contextmanager, suppress -from socket import AddressFamily, SocketKind from typing import TYPE_CHECKING, Any import trio diff --git a/trio/_highlevel_socket.py b/trio/_highlevel_socket.py index bafbd58475..01c5f696a3 100644 --- a/trio/_highlevel_socket.py +++ b/trio/_highlevel_socket.py @@ -2,7 +2,6 @@ from __future__ import annotations import errno -from collections.abc import Generator from contextlib import contextmanager, suppress from typing import TYPE_CHECKING, overload diff --git a/trio/_highlevel_ssl_helpers.py b/trio/_highlevel_ssl_helpers.py index 0baddfbbdf..03562c9edb 100644 --- a/trio/_highlevel_ssl_helpers.py +++ b/trio/_highlevel_ssl_helpers.py @@ -1,20 +1,19 @@ from __future__ import annotations import ssl -from typing import TYPE_CHECKING -from collections.abc import Awaitable, Callable -from typing import NoReturn, TypeVar +from typing import TYPE_CHECKING, NoReturn, TypeVar import trio from ._highlevel_open_tcp_stream import DEFAULT_DELAY -from ._highlevel_socket import SocketStream T = TypeVar("T") if TYPE_CHECKING: from collections.abc import Awaitable, Callable + from ._highlevel_socket import SocketStream + # It might have been nice to take a ssl_protocols= argument here to set up # NPN/ALPN, but to do this we have to mutate the context object, which is OK diff --git a/trio/_tests/test_exports.py b/trio/_tests/test_exports.py index dbdf6593f8..bd04436640 100644 --- a/trio/_tests/test_exports.py +++ b/trio/_tests/test_exports.py @@ -26,7 +26,7 @@ from .pytest_plugin import RUN_SLOW if TYPE_CHECKING: - from collections.abc import Iterator + from collections.abc import Iterable, Iterator mypy_cache_updated = False diff --git a/trio/_tests/test_highlevel_serve_listeners.py b/trio/_tests/test_highlevel_serve_listeners.py index 75cadfd8aa..5fd8ac72e3 100644 --- a/trio/_tests/test_highlevel_serve_listeners.py +++ b/trio/_tests/test_highlevel_serve_listeners.py @@ -2,15 +2,13 @@ import errno from functools import partial -from typing import Awaitable, Callable, NoReturn +from typing import TYPE_CHECKING, Awaitable, Callable, NoReturn import attr import pytest import trio from trio import Nursery, StapledStream, TaskStatus -from trio._channel import MemoryReceiveChannel, MemorySendChannel -from trio.abc import Stream from trio.testing import ( MemoryReceiveStream, MemorySendStream, @@ -19,6 +17,10 @@ wait_all_tasks_blocked, ) +if TYPE_CHECKING: + from trio._channel import MemoryReceiveChannel, MemorySendChannel + from trio.abc import Stream + # types are somewhat tentative - I just bruteforced them until I got something that didn't # give errors StapledMemoryStream = StapledStream[MemorySendStream, MemoryReceiveStream] diff --git a/trio/_tests/test_highlevel_ssl_helpers.py b/trio/_tests/test_highlevel_ssl_helpers.py index 8e90adb3d2..972eeefa3e 100644 --- a/trio/_tests/test_highlevel_ssl_helpers.py +++ b/trio/_tests/test_highlevel_ssl_helpers.py @@ -1,29 +1,33 @@ from __future__ import annotations from functools import partial -from socket import AddressFamily, SocketKind -from ssl import SSLContext -from typing import Any, NoReturn +from typing import TYPE_CHECKING, Any, NoReturn import attr import pytest import trio import trio.testing -from trio.abc import Stream from trio.socket import AF_INET, IPPROTO_TCP, SOCK_STREAM -from .._highlevel_socket import SocketListener from .._highlevel_ssl_helpers import ( open_ssl_over_tcp_listeners, open_ssl_over_tcp_stream, serve_ssl_over_tcp, ) -from .._ssl import SSLListener # using noqa because linters don't understand how pytest fixtures work. from .test_ssl import SERVER_CTX, client_ctx # noqa: F401 +if TYPE_CHECKING: + from socket import AddressFamily, SocketKind + from ssl import SSLContext + + from trio.abc import Stream + + from .._highlevel_socket import SocketListener + from .._ssl import SSLListener + async def echo_handler(stream: Stream) -> None: async with stream: diff --git a/trio/_tests/test_scheduler_determinism.py b/trio/_tests/test_scheduler_determinism.py index 7e0a8e98de..bf0eec3d39 100644 --- a/trio/_tests/test_scheduler_determinism.py +++ b/trio/_tests/test_scheduler_determinism.py @@ -1,9 +1,12 @@ from __future__ import annotations -from pytest import MonkeyPatch +from typing import TYPE_CHECKING import trio +if TYPE_CHECKING: + from pytest import MonkeyPatch + async def scheduler_trace() -> tuple[tuple[str, int], ...]: """Returns a scheduler-dependent value we can use to check determinism.""" diff --git a/trio/_tests/test_ssl.py b/trio/_tests/test_ssl.py index 13decd5c72..94e0356f06 100644 --- a/trio/_tests/test_ssl.py +++ b/trio/_tests/test_ssl.py @@ -13,8 +13,6 @@ import pytest from trio import StapledStream -from trio._core import MockClock -from trio._ssl import T_Stream from trio._tests.pytest_plugin import skip_if_optional_else_raise from trio.abc import ReceiveStream, SendStream from trio.testing import MemoryReceiveStream, MemorySendStream @@ -30,7 +28,6 @@ from .. import _core, socket as tsocket from .._abc import Stream from .._core import BrokenResourceError, ClosedResourceError -from .._core._run import CancelScope from .._core._tests.tutil import slow from .._highlevel_generic import aclose_forcefully from .._highlevel_open_tcp_stream import open_tcp_stream @@ -48,6 +45,11 @@ if TYPE_CHECKING: from typing_extensions import TypeAlias + from trio._core import MockClock + from trio._ssl import T_Stream + + from .._core._run import CancelScope + # We have two different kinds of echo server fixtures we use for testing. The # first is a real server written using the stdlib ssl module and blocking # sockets. It runs in a thread and we talk to it over a real socketpair(), to diff --git a/trio/_tests/test_subprocess.py b/trio/_tests/test_subprocess.py index c901f6f29e..ec0cf109a5 100644 --- a/trio/_tests/test_subprocess.py +++ b/trio/_tests/test_subprocess.py @@ -9,7 +9,6 @@ from functools import partial from pathlib import Path as SyncPath from signal import Signals -from types import FrameType from typing import ( TYPE_CHECKING, Any, @@ -33,14 +32,17 @@ sleep, sleep_forever, ) -from .._abc import Stream from .._core._tests.tutil import skip_if_fbsd_pipes_broken, slow from ..lowlevel import open_process from ..testing import MockClock, assert_no_checkpoints, wait_all_tasks_blocked if TYPE_CHECKING: + from types import FrameType + from typing_extensions import TypeAlias + from .._abc import Stream + if sys.platform == "win32": SignalType: TypeAlias = None else: diff --git a/trio/_tests/test_testing.py b/trio/_tests/test_testing.py index d4348a8c59..f6e137c55e 100644 --- a/trio/_tests/test_testing.py +++ b/trio/_tests/test_testing.py @@ -2,13 +2,11 @@ # XX this should get broken up, like testing.py did import tempfile +from typing import TYPE_CHECKING import pytest from pytest import WarningsRecorder -from trio import Nursery -from trio.abc import ReceiveStream, SendStream - from .. import _core, sleep, socket as tsocket from .._core._tests.tutil import can_bind_ipv6 from .._highlevel_generic import StapledStream, aclose_forcefully @@ -17,6 +15,10 @@ from ..testing._check_streams import _assert_raises from ..testing._memory_streams import _UnboundedByteQueue +if TYPE_CHECKING: + from trio import Nursery + from trio.abc import ReceiveStream, SendStream + async def test_wait_all_tasks_blocked() -> None: record = [] diff --git a/trio/_tests/test_threads.py b/trio/_tests/test_threads.py index 86d59b4dbb..64f60174fa 100644 --- a/trio/_tests/test_threads.py +++ b/trio/_tests/test_threads.py @@ -9,6 +9,7 @@ import weakref from functools import partial from typing import ( + TYPE_CHECKING, AsyncGenerator, Awaitable, Callable, @@ -22,7 +23,6 @@ import pytest import sniffio -from outcome import Outcome from pytest import MonkeyPatch from .. import ( @@ -44,9 +44,13 @@ from_thread_run_sync, to_thread_run_sync, ) -from ..lowlevel import Task from ..testing import wait_all_tasks_blocked +if TYPE_CHECKING: + from outcome import Outcome + + from ..lowlevel import Task + RecordType = List[Tuple[str, Union[threading.Thread, Type[BaseException]]]] T = TypeVar("T") diff --git a/trio/testing/_check_streams.py b/trio/testing/_check_streams.py index da0cd5716e..0b9c904275 100644 --- a/trio/testing/_check_streams.py +++ b/trio/testing/_check_streams.py @@ -2,7 +2,6 @@ from __future__ import annotations import random -from collections.abc import Generator from contextlib import contextmanager, suppress from typing import TYPE_CHECKING, Awaitable, Callable, Generic, Tuple, TypeVar diff --git a/trio/testing/_fake_net.py b/trio/testing/_fake_net.py index e27be80d40..622d5e713e 100644 --- a/trio/testing/_fake_net.py +++ b/trio/testing/_fake_net.py @@ -8,7 +8,6 @@ from __future__ import annotations -import builtins import contextlib import errno import ipaddress From 1d6cbab8557190703a1de83b9b24db02c7775085 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Fri, 3 Nov 2023 21:27:25 -0500 Subject: [PATCH 09/19] Fix duplicate imports post merge --- trio/_threads.py | 1 - 1 file changed, 1 deletion(-) diff --git a/trio/_threads.py b/trio/_threads.py index afdff6afa8..9051e2f4b5 100644 --- a/trio/_threads.py +++ b/trio/_threads.py @@ -22,7 +22,6 @@ enable_ki_protection, start_thread_soon, ) -from ._core._traps import RaiseCancelT from ._deprecate import warn_deprecated from ._sync import CapacityLimiter from ._util import coroutine_or_error From 10b7bb1f777facf771a53d9b2815381a9b3e4e7c Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:16:57 -0500 Subject: [PATCH 10/19] Change how we get `StrOrBytesPath` to match `_file_io.py` --- trio/_subprocess.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/trio/_subprocess.py b/trio/_subprocess.py index 82a2cfe1be..76b0d37431 100644 --- a/trio/_subprocess.py +++ b/trio/_subprocess.py @@ -28,11 +28,15 @@ from collections.abc import Awaitable, Callable, Mapping, Sequence from io import TextIOWrapper + from _typeshed import ( + StrOrBytesPath, + ) from typing_extensions import Self, TypeAlias - - -# Only subscriptable in 3.9+ -StrOrBytesPath: TypeAlias = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"] +else: + # Only subscriptable in 3.9+ + StrOrBytesPath: TypeAlias = Union[ + str, bytes, "os.PathLike[str]", "os.PathLike[bytes]" + ] # Linux-specific, but has complex lifetime management stuff so we hard-code it From d6fd3819a49dbd6bb18eeeaafc5f6023beddb27c Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:22:10 -0500 Subject: [PATCH 11/19] Attempt to add reference to `StrOrBytesPath` --- docs/source/reference-io.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/reference-io.rst b/docs/source/reference-io.rst index d0525e39d4..52c3690c00 100644 --- a/docs/source/reference-io.rst +++ b/docs/source/reference-io.rst @@ -728,6 +728,8 @@ task and interact with it while it's running: .. automethod:: fileno +.. autoclass:: trio._subprocess.StrOrBytesPath + .. autoclass:: trio.Process() .. autoattribute:: returncode From 25f478c6caee4777cb6e56821e659313032327e0 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:32:08 -0500 Subject: [PATCH 12/19] Revert "Attempt to add reference to `StrOrBytesPath`" This reverts commit d6fd3819a49dbd6bb18eeeaafc5f6023beddb27c. --- docs/source/reference-io.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/source/reference-io.rst b/docs/source/reference-io.rst index 52c3690c00..d0525e39d4 100644 --- a/docs/source/reference-io.rst +++ b/docs/source/reference-io.rst @@ -728,8 +728,6 @@ task and interact with it while it's running: .. automethod:: fileno -.. autoclass:: trio._subprocess.StrOrBytesPath - .. autoclass:: trio.Process() .. autoattribute:: returncode From d8ce20c4f7ac1c699dd54fb33b9175614bba93e7 Mon Sep 17 00:00:00 2001 From: CoolCat467 <52022020+CoolCat467@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:32:39 -0500 Subject: [PATCH 13/19] Revert "Change how we get `StrOrBytesPath` to match `_file_io.py`" This reverts commit 10b7bb1f777facf771a53d9b2815381a9b3e4e7c. --- trio/_subprocess.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/trio/_subprocess.py b/trio/_subprocess.py index 76b0d37431..82a2cfe1be 100644 --- a/trio/_subprocess.py +++ b/trio/_subprocess.py @@ -28,15 +28,11 @@ from collections.abc import Awaitable, Callable, Mapping, Sequence from io import TextIOWrapper - from _typeshed import ( - StrOrBytesPath, - ) from typing_extensions import Self, TypeAlias -else: - # Only subscriptable in 3.9+ - StrOrBytesPath: TypeAlias = Union[ - str, bytes, "os.PathLike[str]", "os.PathLike[bytes]" - ] + + +# Only subscriptable in 3.9+ +StrOrBytesPath: TypeAlias = Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"] # Linux-specific, but has complex lifetime management stuff so we hard-code it From 019665f11806f554ccc8b59c4395f65951a0be5f Mon Sep 17 00:00:00 2001 From: jakkdl