Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TypeVarTuple in our APIs #2881

Merged
merged 29 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bd5a451
Use TypeVarTuple in various functions
TeamSpen210 Aug 21, 2023
97d6e0e
Use TypeVarTuple in _run
TeamSpen210 Aug 21, 2023
9bcb185
Cast this partial() definition, we know it's safe
TeamSpen210 Sep 20, 2023
6f8153f
Fix some uncovered type errors
TeamSpen210 Sep 20, 2023
52c69c8
This now passes mypy checking
TeamSpen210 Nov 19, 2023
13d5d63
Fix return types here
TeamSpen210 Nov 19, 2023
9ed1d08
Fix more errors
TeamSpen210 Nov 19, 2023
0933718
Bump Mypy version
TeamSpen210 Nov 19, 2023
a0e9966
Fix docs failure
TeamSpen210 Nov 19, 2023
5899ad4
Add type tests for Nursery.start[_soon] methods
TeamSpen210 Nov 19, 2023
e12273f
Revert using TypeVarTuple for Nursery.start()
TeamSpen210 Nov 23, 2023
5a39c8b
This seems to be a mypy bug?
TeamSpen210 Nov 23, 2023
5b0215c
This type-error is resolved in Mypy master
TeamSpen210 Nov 23, 2023
342733c
Add missing import for PosArgT in _generated_run
TeamSpen210 Nov 23, 2023
749ef94
The StatusT typevar can't be used here without TVT
TeamSpen210 Nov 23, 2023
1a0daa6
Make gen_exports create an __all__ list
TeamSpen210 Nov 30, 2023
ff01a82
Merge branch 'master' into use-typevartuple
TeamSpen210 Nov 30, 2023
b91b4b9
Run _core/_tests/type_tests under Pyright also
TeamSpen210 Nov 30, 2023
972961e
Add newsfragment
TeamSpen210 Nov 30, 2023
bbd436d
Default args are intentionally not considered by Pyright with TypeVar…
TeamSpen210 Nov 30, 2023
bac6558
Type tests shouldn't be checked by coverage
TeamSpen210 Dec 1, 2023
9783f8b
Handle Enum.__signature__ not being detected by Mypy
TeamSpen210 Dec 1, 2023
e1edda0
Mypy does see copy/deepcopy now, this is no longer required
TeamSpen210 Dec 1, 2023
176092b
Merge branch 'master' into use-typevartuple
CoolCat467 Dec 6, 2023
2903a59
Fix type issue from merge
CoolCat467 Dec 6, 2023
d9d7a3d
Remove unnecessary ignore
TeamSpen210 Dec 6, 2023
580627d
Merge branch 'master' into use-typevartuple
A5rocks Dec 13, 2023
e012611
Pyright-related changes
A5rocks Dec 13, 2023
9f94f97
Fix strict-mode error
A5rocks Dec 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
# aliasing doesn't actually fix the warning for types.FrameType, but displaying
# "types.FrameType" is more helpful than just "frame"
"FrameType": "types.FrameType",
"Context": "OpenSSL.SSL.Context",
# SSLListener.accept's return type is seen as trio._ssl.SSLStream
"SSLStream": "trio.SSLStream",
}
Expand All @@ -91,6 +90,8 @@ def autodoc_process_signature(
# Strip the type from the union, make it look like = ...
signature = signature.replace(" | type[trio._core._local._NoValue]", "")
signature = signature.replace("<class 'trio._core._local._NoValue'>", "...")
if "DTLS" in name:
signature = signature.replace("SSL.Context", "OpenSSL.SSL.Context")
# Don't specify PathLike[str] | PathLike[bytes], this is just for humans.
signature = signature.replace("StrOrBytesPath", "str | bytes | os.PathLike")

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ extend-exclude = [
'src/trio/lowlevel.py' = ['F401']
'src/trio/socket.py' = ['F401']
'src/trio/testing/__init__.py' = ['F401']
'**/type_tests/*' = ['F841'] # Annotated local vars are used to specify return values.
TeamSpen210 marked this conversation as resolved.
Show resolved Hide resolved

[tool.ruff.isort]
combine-as-imports = true
Expand Down
21 changes: 15 additions & 6 deletions src/trio/_core/_entry_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import threading
from collections import deque
from typing import Callable, Iterable, NoReturn, Tuple
from typing import TYPE_CHECKING, Callable, NoReturn, Tuple

import attr

from .. import _core
from .._util import NoPublicConstructor, final
from ._wakeup_socketpair import WakeupSocketpair

# TODO: Type with TypeVarTuple, at least to an extent where it makes
# the public interface safe.
if TYPE_CHECKING:
from typing_extensions import TypeVarTuple, Unpack

PosArgsT = TypeVarTuple("PosArgsT")

Function = Callable[..., object]
Job = Tuple[Function, Iterable[object]]
Job = Tuple[Function, Tuple[object, ...]]


@attr.s(slots=True)
Expand Down Expand Up @@ -122,7 +125,10 @@ def size(self) -> int:
return len(self.queue) + len(self.idempotent_queue)

def run_sync_soon(
self, sync_fn: Function, *args: object, idempotent: bool = False
self,
sync_fn: Callable[[Unpack[PosArgsT]], object],
*args: Unpack[PosArgsT],
idempotent: bool = False,
) -> None:
with self.lock:
if self.done:
Expand Down Expand Up @@ -163,7 +169,10 @@ class TrioToken(metaclass=NoPublicConstructor):
_reentry_queue: EntryQueue = attr.ib()

def run_sync_soon(
self, sync_fn: Function, *args: object, idempotent: bool = False
self,
sync_fn: Callable[[Unpack[PosArgsT]], object],
*args: Unpack[PosArgsT],
idempotent: bool = False,
) -> None:
"""Schedule a call to ``sync_fn(*args)`` to occur in the context of a
Trio task.
Expand Down
6 changes: 4 additions & 2 deletions src/trio/_core/_generated_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from collections.abc import Awaitable, Callable

from outcome import Outcome
from typing_extensions import Unpack

from .._abc import Clock
from ._entry_queue import TrioToken
from ._run import PosArgT


def current_statistics() -> RunStatistics:
Expand Down Expand Up @@ -113,8 +115,8 @@ def reschedule(task: Task, next_send: Outcome[Any] = _NO_SEND) -> None:


def spawn_system_task(
async_fn: Callable[..., Awaitable[object]],
*args: object,
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
*args: Unpack[PosArgT],
name: object = None,
context: (contextvars.Context | None) = None,
) -> Task:
Expand Down
62 changes: 35 additions & 27 deletions src/trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
if sys.version_info < (3, 11):
from exceptiongroup import BaseExceptionGroup

FnT = TypeVar("FnT", bound="Callable[..., Any]")
StatusT = TypeVar("StatusT")
StatusT_co = TypeVar("StatusT_co", covariant=True)
StatusT_contra = TypeVar("StatusT_contra", contravariant=True)
RetT = TypeVar("RetT")


if TYPE_CHECKING:
import contextvars
Expand All @@ -70,19 +76,25 @@
# for some strange reason Sphinx works with outcome.Outcome, but not Outcome, in
# start_guest_run. Same with types.FrameType in iter_await_frames
import outcome
from typing_extensions import Self
from typing_extensions import Self, TypeVarTuple, Unpack

PosArgT = TypeVarTuple("PosArgT")

# Needs to be guarded, since Unpack[] would be evaluated at runtime.
class _NurseryStartFunc(Protocol[Unpack[PosArgT], StatusT_co]):
A5rocks marked this conversation as resolved.
Show resolved Hide resolved
"""Type of functions passed to `nursery.start() <trio.Nursery.start>`."""

def __call__(
self, *args: Unpack[PosArgT], task_status: TaskStatus[StatusT_co]
Copy link
Contributor

@A5rocks A5rocks Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self, *args: Unpack[PosArgT], task_status: TaskStatus[StatusT_co]
self, *args: Unpack[PosArgT], *, task_status: TaskStatus[StatusT_co]

edit: I thought that would work (and it does if *args: Unpack[PosArgT] is gone) but it doesn't. I believe the issue is that the type var tuple can have a finite amount of arguments and then... kinda overflow, at least in some cases right? So like, calling with int, str, TaskStatus[str] when PosArgT is just [int, str] would mean that StatusT_co would be str right?

Errr, so we're promising that anything with this protocol can call it as such. But we're also passing functions that don't allow that? (actually, maybe not, and that's an actual bug. But as is our protocol shouldn't accept (a: int, *, task_status: TaskStatus[str]))

edit edit: ok my analysis above is totally incorrect, only keeping it to prevent someone from going down the same path i went down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no, the *args already makes it keyword-only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry about the repeated confusion with this thread, TypeVarTuple is getting to my head :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's real confusing. Since task_status is keyword-only it shouldn't get mixed up. As you can see with the type-tests, if it's being assigned to an annotated type, Mypy can do bidirectional inference and figure it out. But if it's not annotated or just a bare call, it gets confused and assumes it's Never. I think it's a bug.

Copy link
Contributor

@A5rocks A5rocks Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I took a look at mypy's code for the last 45 minutes and spotted:

  1. It looks like it isn't generating constraints for the typevar. (inference works by figuring out all constraints on a typevar and then constraining it -- if there are no constraints, then it returns UninhabitedType (str value: Never ... not literally Never) to us)
  2. This is because the constraints only get generated for a callback protocol if the type being assigned is a subtype of the callback protocol w/ erased tvars (I assume this is to make less confusing error messages). Unfortunately, TypeVarTuple gets erased to tuple[Any, ...] and our callable doesn't take in infinite parameters.
  3. Even if that problem were to be solved, it looks like mypy only generates constraints for either a bunch of type vars or a single unpack. This isn't a problem normally (Callable[[Unpack[Ts], T], None] has an implicit Unpack) but means that mypy focuses on the typevartuple, not making a constraint for our type variable.

I'm not really sure how to solve this myself, a bug report would probably work nicely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@A5rocks probably worth cross-posting your analysis in the issue that teamspen opened.

) -> Awaitable[object]:
...


DEADLINE_HEAP_MIN_PRUNE_THRESHOLD: Final = 1000

# Passed as a sentinel
_NO_SEND: Final[Outcome[Any]] = cast("Outcome[Any]", object())

FnT = TypeVar("FnT", bound="Callable[..., Any]")
StatusT = TypeVar("StatusT")
StatusT_co = TypeVar("StatusT_co", covariant=True)
StatusT_contra = TypeVar("StatusT_contra", contravariant=True)
RetT = TypeVar("RetT")


@final
class _NoStatus(metaclass=NoPublicConstructor):
Expand Down Expand Up @@ -1119,9 +1131,8 @@ def aborted(raise_cancel: _core.RaiseCancelT) -> Abort:

def start_soon(
self,
# TODO: TypeVarTuple
async_fn: Callable[..., Awaitable[object]],
*args: object,
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
*args: Unpack[PosArgT],
name: object = None,
) -> None:
"""Creates a child task, scheduling ``await async_fn(*args)``.
Expand Down Expand Up @@ -1170,7 +1181,7 @@ async def start(
async_fn: Callable[..., Awaitable[object]],
*args: object,
name: object = None,
) -> StatusT:
) -> Any:
r"""Creates and initializes a child task.

Like :meth:`start_soon`, but blocks until the new task has
Expand Down Expand Up @@ -1219,7 +1230,7 @@ async def async_fn(arg1, arg2, *, task_status=trio.TASK_STATUS_IGNORED):
# `run` option, which would cause it to wrap a pre-started()
# exception in an extra ExceptionGroup. See #2611.
async with open_nursery(strict_exception_groups=False) as old_nursery:
task_status: _TaskStatus[StatusT] = _TaskStatus(old_nursery, self)
task_status: _TaskStatus[Any] = _TaskStatus(old_nursery, self)
thunk = functools.partial(async_fn, task_status=task_status)
task = GLOBAL_RUN_CONTEXT.runner.spawn_impl(
thunk, args, old_nursery, name
Expand All @@ -1232,7 +1243,7 @@ async def async_fn(arg1, arg2, *, task_status=trio.TASK_STATUS_IGNORED):
# (Any exceptions propagate directly out of the above.)
if task_status._value is _NoStatus:
raise RuntimeError("child exited without calling task_status.started()")
return task_status._value # type: ignore[return-value] # Mypy doesn't narrow yet.
return task_status._value
finally:
self._pending_starts -= 1
self._check_nursery_closed()
Expand Down Expand Up @@ -1690,9 +1701,8 @@ def reschedule( # type: ignore[misc]

def spawn_impl(
self,
# TODO: TypeVarTuple
async_fn: Callable[..., Awaitable[object]],
args: tuple[object, ...],
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
args: tuple[Unpack[PosArgT]],
nursery: Nursery | None,
name: object,
*,
Expand Down Expand Up @@ -1721,7 +1731,7 @@ def spawn_impl(
# Call the function and get the coroutine object, while giving helpful
# errors for common mistakes.
######
# TODO: resolve the type: ignore when implementing TypeVarTuple
# TypeVarTuple passed into ParamSpec function confuses Mypy.
coro = context.run(coroutine_or_error, async_fn, *args) # type: ignore[arg-type]

if name is None:
Expand Down Expand Up @@ -1809,12 +1819,11 @@ def task_exited(self, task: Task, outcome: Outcome[Any]) -> None:
# System tasks and init
################

@_public # Type-ignore due to use of Any here.
def spawn_system_task( # type: ignore[misc]
@_public
def spawn_system_task(
self,
# TODO: TypeVarTuple
async_fn: Callable[..., Awaitable[object]],
*args: object,
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
*args: Unpack[PosArgT],
name: object = None,
context: contextvars.Context | None = None,
) -> Task:
Expand Down Expand Up @@ -1879,10 +1888,9 @@ def spawn_system_task( # type: ignore[misc]
)

async def init(
# TODO: TypeVarTuple
self,
async_fn: Callable[..., Awaitable[object]],
args: tuple[object, ...],
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
args: tuple[Unpack[PosArgT]],
) -> None:
# run_sync_soon task runs here:
async with open_nursery() as run_sync_soon_nursery:
Expand Down Expand Up @@ -2408,8 +2416,8 @@ def my_done_callback(run_outcome):
# straight through.
def unrolled_run(
runner: Runner,
async_fn: Callable[..., object],
args: tuple[object, ...],
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
args: tuple[Unpack[PosArgT]],
host_uses_signal_set_wakeup_fd: bool = False,
) -> Generator[float, EventResult, None]:
locals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
Expand Down
4 changes: 1 addition & 3 deletions src/trio/_core/_tests/test_guest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,7 @@ async def trio_main() -> None:
# Ensure we don't pollute the thread-level context if run under
# an asyncio without contextvars support (3.6)
context = contextvars.copy_context()
if TYPE_CHECKING:
aiotrio_run(trio_main, host_uses_signal_set_wakeup_fd=True)
# this type error is a bug in typeshed or mypy, as it's equivalent to the above line
# Will be fixed in mypy 1.8 (https://github.com/python/mypy/pull/15896)
context.run(aiotrio_run, trio_main, host_uses_signal_set_wakeup_fd=True) # type: ignore[arg-type]

assert record == {("asyncio", "asyncio"), ("trio", "trio")}
4 changes: 2 additions & 2 deletions src/trio/_core/_tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ async def task1() -> None:
t1.set("plaice")
assert t1.get() == "plaice"

async def task2(tok: str) -> None:
t1.reset(token)
async def task2(tok: RunVarToken[str]) -> None:
t1.reset(tok)

with pytest.raises(LookupError):
t1.get()
Expand Down
7 changes: 6 additions & 1 deletion src/trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ async def child2() -> None:
assert tasks["child2"].child_nurseries == []

async def child1(
*,
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
me = tasks["child1"] = _core.current_task()
Expand Down Expand Up @@ -1704,6 +1705,7 @@ async def sleep_then_start(

# calling started twice
async def double_started(
*,
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
task_status.started()
Expand All @@ -1715,6 +1717,7 @@ async def double_started(

# child crashes before calling started -> error comes out of .start()
async def raise_keyerror(
*,
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
raise KeyError("oops")
Expand All @@ -1725,6 +1728,7 @@ async def raise_keyerror(

# child exiting cleanly before calling started -> triggers a RuntimeError
async def nothing(
*,
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
return
Expand All @@ -1738,6 +1742,7 @@ async def nothing(
# nothing -- the child keeps executing under start(). The value it passed
# is ignored; start() raises Cancelled.
async def just_started(
*,
task_status: _core.TaskStatus[str] = _core.TASK_STATUS_IGNORED,
) -> None:
task_status.started("hi")
Expand Down Expand Up @@ -1920,7 +1925,7 @@ def __init__(self, *largs: it) -> None:
self.nexts = [obj.__anext__ for obj in largs]

async def _accumulate(
self, f: Callable[[], Awaitable[int]], items: list[int | None], i: int
self, f: Callable[[], Awaitable[int]], items: list[int], i: int
) -> None:
items[i] = await f()

Expand Down
78 changes: 78 additions & 0 deletions src/trio/_core/type_tests/nursery_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Test variadic generic typing for Nursery.start[_soon]()."""
from trio import TASK_STATUS_IGNORED, Nursery, TaskStatus


async def task_0() -> None:
...


async def task_1a(value: int) -> None:
...


async def task_1b(value: str) -> None:
...


async def task_2a(a: int, b: str) -> None:
...


async def task_2b(a: str, b: int) -> None:
...


async def task_2c(a: str, b: int, optional: bool = False) -> None:
...


async def task_requires_kw(a: int, *, b: bool) -> None:
...


async def task_startable_1(
a: str,
*,
task_status: TaskStatus[bool] = TASK_STATUS_IGNORED,
) -> None:
...


async def task_startable_2(
a: str,
b: float,
*,
task_status: TaskStatus[bool] = TASK_STATUS_IGNORED,
) -> None:
...


async def task_requires_start(*, task_status: TaskStatus[str]) -> None:
"""Check a function requiring start() to be used."""


async def task_pos_or_kw(value: str, task_status: TaskStatus[int]) -> None:
"""Check a function which doesn't use the *-syntax works."""
...


def check_start_soon(nursery: Nursery) -> None:
"""start_soon() functionality."""
nursery.start_soon(task_0)
nursery.start_soon(task_1a) # type: ignore
nursery.start_soon(task_2b) # type: ignore

nursery.start_soon(task_0, 45) # type: ignore
nursery.start_soon(task_1a, 32)
nursery.start_soon(task_1b, 32) # type: ignore
nursery.start_soon(task_1a, "abc") # type: ignore
nursery.start_soon(task_1b, "abc")

nursery.start_soon(task_2b, "abc") # type: ignore
nursery.start_soon(task_2a, 38, "46")
nursery.start_soon(task_2c, "abc", 12)
nursery.start_soon(task_2c, "abc", 12, True)

nursery.start_soon(task_requires_kw, 12, True) # type: ignore
# Tasks following the start() API can be made to work.
nursery.start_soon(task_startable_1, "cdf")
Loading
Loading