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

trio.sleep_forever should be NoReturn #3096

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions newsfragments/3095.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update :func:`trio.sleep_forever` to be `NoReturn`.
14 changes: 14 additions & 0 deletions src/trio/_tests/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ async def sleep_3() -> None:
await check_takes_about(sleep_3, TARGET)


async def test_cannot_wake_sleep_forever() -> None:
# Test an error occurs if you manually wake sleep_forever().
task = trio.lowlevel.current_task()

async def wake_task() -> None:
await trio.lowlevel.checkpoint()
trio.lowlevel.reschedule(task, outcome.Value(None))

async with trio.open_nursery() as nursery:
nursery.start_soon(wake_task)
with pytest.raises(RuntimeError):
await trio.sleep_forever()


class TimeoutScope(Protocol):
def __call__(self, seconds: float, *, shield: bool) -> trio.CancelScope: ...

Expand Down
5 changes: 3 additions & 2 deletions src/trio/_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, NoReturn

import trio

Expand Down Expand Up @@ -58,13 +58,14 @@ def move_on_after(
)


async def sleep_forever() -> None:
async def sleep_forever() -> NoReturn:
"""Pause execution of the current task forever (or until cancelled).

Equivalent to calling ``await sleep(math.inf)``.

"""
await trio.lowlevel.wait_task_rescheduled(lambda _: trio.lowlevel.Abort.SUCCEEDED)
raise RuntimeError("Should never have been rescheduled!")


async def sleep_until(deadline: float) -> None:
Expand Down
Loading