diff --git a/trio/_core/_local.py b/trio/_core/_local.py index 27252bc78d..f1cbb3e61f 100644 --- a/trio/_core/_local.py +++ b/trio/_core/_local.py @@ -17,7 +17,7 @@ class _NoValue: @final -@attr.s(eq=False, hash=False, slots=False) +@attr.s(eq=False, hash=False, slots=True) class RunVarToken(Generic[T], metaclass=NoPublicConstructor): _var: RunVar[T] = attr.ib() previous_value: T | type[_NoValue] = attr.ib(default=_NoValue) diff --git a/trio/_core/_tests/test_guest_mode.py b/trio/_core/_tests/test_guest_mode.py index bd0ef9ce69..be9d4cb9c2 100644 --- a/trio/_core/_tests/test_guest_mode.py +++ b/trio/_core/_tests/test_guest_mode.py @@ -26,15 +26,18 @@ import pytest from outcome import Outcome from pytest import MonkeyPatch, WarningsRecorder -from typing_extensions import TypeAlias import trio import trio.testing from trio._channel import MemorySendChannel +from trio.abc import Instrument from ..._util import signal_raise from .tutil import buggy_pypy_asyncgens, gc_collect_harder, restore_unraisablehook +if TYPE_CHECKING: + from typing_extensions import TypeAlias + T = TypeVar("T") InHost: TypeAlias = Callable[[object], None] @@ -346,7 +349,7 @@ async def get_woken_by_host_deadline(watb_cscope: trio.CancelScope) -> None: # 'sit_in_wait_all_tasks_blocked', we want the test to # actually end. So in after_io_wait we schedule a second host # call to tear things down. - class InstrumentHelper(trio._abc.Instrument): + class InstrumentHelper(Instrument): def __init__(self) -> None: self.primed = False diff --git a/trio/_ssl.py b/trio/_ssl.py index 77d3b80140..57f41c11d2 100644 --- a/trio/_ssl.py +++ b/trio/_ssl.py @@ -239,7 +239,7 @@ def done(self) -> bool: _State = _Enum("_State", ["OK", "BROKEN", "CLOSED"]) -# TODO: variance +# invariant T_Stream = TypeVar("T_Stream", bound=Stream) diff --git a/trio/_tests/test_file_io.py b/trio/_tests/test_file_io.py index d438a9fb10..5d617a0661 100644 --- a/trio/_tests/test_file_io.py +++ b/trio/_tests/test_file_io.py @@ -244,13 +244,15 @@ async def test_aclose_cancelled(path: pathlib.Path) -> None: assert f.closed -async def test_detach_rewraps_asynciobase() -> None: - raw = io.BytesIO() - buffered = io.BufferedReader(raw) # type: ignore[arg-type] # ???????????? +async def test_detach_rewraps_asynciobase(tmp_path: pathlib.Path) -> None: + tmp_file = tmp_path / "filename" + tmp_file.touch() + with open(tmp_file, mode="rb", buffering=0) as raw: + buffered = io.BufferedReader(raw) - async_file = trio.wrap_file(buffered) + async_file = trio.wrap_file(buffered) - detached = await async_file.detach() + detached = await async_file.detach() - assert isinstance(detached, AsyncIOWrapper) - assert detached.wrapped is raw + assert isinstance(detached, AsyncIOWrapper) + assert detached.wrapped is raw diff --git a/trio/_tests/test_highlevel_generic.py b/trio/_tests/test_highlevel_generic.py index 4b2008c08c..3e9fc212a8 100644 --- a/trio/_tests/test_highlevel_generic.py +++ b/trio/_tests/test_highlevel_generic.py @@ -27,8 +27,9 @@ async def aclose(self) -> None: class RecordReceiveStream(ReceiveStream): record: list[str | tuple[str, int | None]] = attr.ib(factory=list) - async def receive_some(self, max_bytes: int | None = None) -> None: # type: ignore[override] + async def receive_some(self, max_bytes: int | None = None) -> bytes: self.record.append(("receive_some", max_bytes)) + return b"" async def aclose(self) -> None: self.record.append("aclose") diff --git a/trio/_tests/test_ssl.py b/trio/_tests/test_ssl.py index 58e069f239..4a4ba95ecd 100644 --- a/trio/_tests/test_ssl.py +++ b/trio/_tests/test_ssl.py @@ -8,10 +8,9 @@ from contextlib import asynccontextmanager, contextmanager from functools import partial from ssl import SSLContext -from typing import Any, AsyncIterator, Iterator, NoReturn +from typing import TYPE_CHECKING, Any, AsyncIterator, Iterator, NoReturn import pytest -from typing_extensions import TypeAlias from trio import StapledStream from trio._core import MockClock @@ -46,6 +45,9 @@ memory_stream_pair, ) +if TYPE_CHECKING: + from typing_extensions import TypeAlias + # 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 e4713b3950..8c32f1c49b 100644 --- a/trio/_tests/test_subprocess.py +++ b/trio/_tests/test_subprocess.py @@ -20,7 +20,6 @@ import pytest from pytest import MonkeyPatch, WarningsRecorder -from typing_extensions import TypeAlias from .. import ( ClosedResourceError, @@ -38,6 +37,9 @@ from ..lowlevel import open_process from ..testing import MockClock, assert_no_checkpoints, wait_all_tasks_blocked +if TYPE_CHECKING: + from typing_extensions import TypeAlias + if sys.platform == "win32": SignalType: TypeAlias = None else: diff --git a/trio/_tests/test_sync.py b/trio/_tests/test_sync.py index 448ead15da..3f9ca88650 100644 --- a/trio/_tests/test_sync.py +++ b/trio/_tests/test_sync.py @@ -1,16 +1,18 @@ from __future__ import annotations import weakref -from typing import Callable, Union +from typing import TYPE_CHECKING, Callable, Union import pytest -from typing_extensions import TypeAlias from .. import _core from .._sync import * from .._timeouts import sleep_forever from ..testing import assert_checkpoints, wait_all_tasks_blocked +if TYPE_CHECKING: + from typing_extensions import TypeAlias + async def test_Event() -> None: e = Event() diff --git a/trio/_tests/test_threads.py b/trio/_tests/test_threads.py index 4fa1e4bedf..77650bf569 100644 --- a/trio/_tests/test_threads.py +++ b/trio/_tests/test_threads.py @@ -9,7 +9,6 @@ import weakref from functools import partial from typing import ( - TYPE_CHECKING, AsyncGenerator, Awaitable, Callable, @@ -462,12 +461,10 @@ async def test_run_in_worker_thread_limiter( # Mutating them in-place is OK though (as long as you use proper # locking etc.). class state: - if TYPE_CHECKING: - ran: int - high_water: int - running: int - parked: int - pass + ran: int + high_water: int + running: int + parked: int state.ran = 0 state.high_water = 0 diff --git a/trio/testing/_fake_net.py b/trio/testing/_fake_net.py index 2b1d2c8b34..fbe2670f46 100644 --- a/trio/testing/_fake_net.py +++ b/trio/testing/_fake_net.py @@ -24,7 +24,6 @@ ) import attr -from typing_extensions import Buffer, Self import trio from trio._util import NoPublicConstructor, final @@ -33,7 +32,7 @@ from socket import AddressFamily, SocketKind from types import TracebackType - from typing_extensions import TypeAlias + from typing_extensions import Buffer, Self, TypeAlias IPAddress: TypeAlias = Union[ipaddress.IPv4Address, ipaddress.IPv6Address]