Skip to content

Commit

Permalink
trio.sleep_forever should be NoReturn (#3096)
Browse files Browse the repository at this point in the history
* NoReturn

* rst

* fix "implicit return"

* TeamSpen210 rc

* Add a test for the error

---------

Co-authored-by: Spencer Brown <[email protected]>
  • Loading branch information
Redoubts and TeamSpen210 authored Sep 30, 2024
1 parent 4e979bf commit f508bb4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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

0 comments on commit f508bb4

Please sign in to comment.