8000 Change `fail_after`&`move_on_after` to set deadline relative to entering. Add CancelScope.relative_deadline by jakkdl · Pull Request #3010 · python-trio/trio · GitHub
[go: up one dir, main page]

Skip to content

Change fail_after&move_on_after to set deadline relative to entering. Add CancelScope.relative_deadline #3010

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

Merged
merged 32 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dea2e79
flake8-trio has been renamed
jakkdl May 22, 2024
d6e91cc
Add explicit documentation on timeout semantic for fail&move_on_after
jakkdl Jun 6, 2024
dc3bd3d
Merge branch 'master' into fail_after_documentation
jakkdl Jun 6, 2024
da25949
use a5rocks alternate phrasing for move_on_after
jakkdl Jun 7, 2024
7a8262a
add `relative_deadline` attribute to CancelScope. `move_on_after` and…
jakkdl Jun 20, 2024
d028037
Merge branch 'master' into fail_after_documentation
jakkdl Jun 20, 2024
5447f45
fix codecov, mypy now fails to unify return types across the timeouts…
jakkdl Jun 20, 2024
1381560
instead of a breaking change we now raise deprecationwarning and give…
jakkdl Jun 24, 2024
742c013
don't inherit from AbstractContextManager since that breaks 3.8, and …
jakkdl Jun 24, 2024
8000
0075b56
fix RTD build. Replace star import with explicit list. Remove useless…
jakkdl Jun 24, 2024
9d3d0b9
reimplement as transforming class
jakkdl Jun 26, 2024
b29db0d
Merge branch 'master' into fail_after_documentation
jakkdl Jun 27, 2024
480681d
whoops, forgot to actually enter the cancelscope
jakkdl Jun 27, 2024
00ba1ca
Merge remote-tracking branch 'origin/main' into fail_after_documentation
jakkdl Aug 31, 2024
510aaa1
remove unneeded type:ignore with mypy 1.11
jakkdl Aug 31, 2024
7079d71
Merge branch 'main' into fail_after_documentation
jakkdl Sep 5, 2024
3474128
write docs, update newsfragments to match current implementation
jakkdl Sep 5, 2024
df5876d
add transitional functions
jakkdl Sep 5, 2024
a240948
fix test
jakkdl Sep 5, 2024
0183b43
fix tests/codecov
jakkdl Sep 5, 2024
ca63354
fix test
jakkdl Sep 5, 2024
7591297
Merge remote-tracking branch 'origin/main' into repro_pyright_verifyt…
jakkdl Sep 12, 2024
75fc52b
quick re-implementation after new spec. Docs/docstrings have not had …
jakkdl Sep 12, 2024
5a32eb0
clean up return type of fail_at/_after for sphinx
jakkdl Sep 16, 2024
c2a3d8e
remove _is_relative, and calculate it on the fly instead. Add nan han…
jakkdl Sep 16, 2024
f8e9417
change some RuntimeError to DeprecationWarning, fix tests/coverage
jakkdl Sep 16, 2024
36786a6
pragma: no cover
jakkdl Sep 16, 2024
8e863b8
Merge remote-tracking branch 'origin/main' into fail_after_documentation
jakkdl Sep 16, 2024
6a7650d
duplicate validation logic, bump pyright
jakkdl Sep 18, 2024
02ac588
Merge remote-tracking branch 'origin/main' into fail_after_documentation
jakkdl Sep 18, 2024
bf12a80
update docs/newsfragments/docstrings
jakkdl Sep 18, 2024
9961abc
properly link to ASYNC122
jakkdl Sep 19, 2024
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
Prev Previous commit
Next Next commit
clean up return type of fail_at/_after for sphinx
  • Loading branch information
jakkdl committed Sep 16, 2024
commit 5a32eb0ca3c329a534df3c0d45a113377fdbf0ac
35 changes: 0 additions & 35 deletions docs/source/reference-core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -598,41 +598,6 @@ which is sometimes useful:

.. autofunction:: current_effective_deadline


.. _saved_relative_timeouts:

Separating initialization and entry of a :class:`CancelScope`
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Before v0.26.3 there was no separation of initialization and entry of a :class:`CancelScope`. This meant that trying to initialize :func:`move_on_after` or :func:`fail_after` and entering them at a later point meant that the timeout was relative to initialization, leading to confusing behaviour.

.. code-block:: python

# THIS WILL NOW GIVE A DEPRECATION ERROR
my_cs = trio.move_on_after(5)
trio.sleep(1)
with my_cs: # this would move on after 4 seconds
...

This is now resolved with the addition of a wrapper class :class:`trio._timeouts._RelativeCancelScope` that is transparent to end users when initializing and entering at the same time. To silence the :class:`DeprecationWarning` and use the new behavior, pass ``timeout_from_enter=True`` to :func:`move_on_after` or :func:`fail_after`.

.. code-block:: python

my_cs = trio.move_on_after(5, timeout_from_enter = True)
trio.sleep(1)
with my_cs: # this will now move on after 5 seconds
...

If you want to retain previous behaviour, you should now instead write something like

.. code-block:: python

my_cs = trio.move_on_at(trio.current_time() + 5)
trio.sleep(1)
with my_cs: # this will now unambiguously move on after 4 seconds
...

.. autoclass:: trio._timeouts._RelativeCancelScope

.. _tasks:

Tasks let you do multiple things at once
Expand Down
12 changes: 11 additions & 1 deletion src/trio/_timeouts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import math
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -32,7 +33,6 @@ def move_on_after(
seconds: float,
*,
shield: bool = False,
timeout_from_enter: bool = False,
) -> trio.CancelScope:
"""Use as a context manager to create a cancel scope whose deadline is
set to now + *seconds*.
Expand Down Expand Up @@ -180,3 +180,13 @@ def fail_after(
yield scope
if scope.cancelled_caught:
raise TooSlowError


# Users don't need to know that fail_at & fail_after wraps move_on_at and move_on_after
# and there is no functional difference. So we replace the return value when generating
# documentation.
if "sphinx" in sys.modules:
import inspect

for c in (fail_at, fail_after):
c.__signature__ = inspect.Signature.from_callable(c).replace(return_annotation=trio.CancelScope) # type: ignore[union-attr]
0