Open jakkdl opened 3 weeks ago
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.63%. Comparing base (
80eec96
) to head (480681d
).
(ignore the commit history, I created the branch in a messy way. Will squash the PR anyway)
Per my comment on the issue,
I think it would make sense for
trio.fail_after(x)
to be relative to entering the context manager;trio.fail_at(trio.current_time() + x)
seems a more natural way to express the relative-to-construction-time semantics. (and the same fortrio.move_on_{at,after}
)Either way, we should document the chosen semantics.
While I think that documenting this is a net improvement, it might also lead people to rely on the creation-time semantics for *_after
in a way that makes changing that to enter-time semantics harder later. Would you be up for making that change and documenting it as a one-step PR?
Per my comment on the issue,
I think it would make sense for
trio.fail_after(x)
to be relative to entering the context manager;trio.fail_at(trio.current_time() + x)
seems a more natural way to express the relative-to-construction-time semantics. (and the same fortrio.move_on_{at,after}
) Either way, we should document the chosen semantics.While I think that documenting this is a net improvement, it might also lead people to rely on the creation-time semantics for
*_after
in a way that makes changing that to enter-time semantics harder later. Would you be up for making that change and documenting it as a one-step PR?
Sure. Started thinking about different ways of implementing it in the issue: https://github.com/python-trio/trio/issues/2512#issuecomment-2157955978
relative_deadline
after entering.None
over math.inf
quite a bit, but changing the behavior of CancelScope.deadline
would be breaking for anybody accessing the value of the property in the inf
/None
case - so I should probably just change relative_deadline
to also use inf
instead of None
.relative_deadline
, but can probably mostly just take the stuff I wrote in the newsfragment.move_on_after
and fail_after
to be explicit about deadline being relative to entering. (i.e. undo/invert what was originally changed in this PR).The pycharm bug referred to in timeouts seem to be fixed https://youtrack.jetbrains.com/issue/PY-36444/PyCharm-doesnt-infer-types-when-using-contextlib.contextmanager-decorator and released over a year ago, so I think we can drop the workaround... except mypy now behaves slightly different in one test (?).
RTD is failing because towncrier appends link to the github issue at the end of the newsfragments, but since we're inside a code block that leads to invalid python syntax. Trying to append newlines to the rst
file doesn't help, they get stripped. Not sure the code blocks are really needed, could probably just move that stuff into the docstrings, but lets deal with that when we've decided on the precise implementation.
I gave it a shot to instead implement this as a transforming class. The basic implementation is way cleaner, but the big downside is that it will break code that currently does
cs = trio.move_on_after(5)
with cs:
cs.deadline = 7 # AttributeError: _RelativeCancelScope does not have an attribute `deadline`
# you need to instead write
with cs as cs2:
cs2.deadline = 7
There's a couple ways to fix that, though it's gonna be somewhat ugly. Either properties/methods that mirror the properties/methods of CancelScope
, or dunder magic with __getattr__
/__setattr__
. We can make them invisible to type checkers and raise warnings though, and error if the user tries to access deadline
before the scope is entered (if timeout_from_enter=True
).
Partially deals with #2512 by documenting current behavior.This PR now works to fully resolve #2512 by adding a
relative_deadline
attribute toCancelScope
, which is then used byfail_after
andmove_on_after
. Upon entering theCancelScope
the absolute deadline is then calculated asmin(deadline, current_time() + relative_deadline)
.This PR also reverts an old workaround in
timeouts.py
due to pycharm being unable to resolve@contextmanager
for type checking.TODO:
fail_after
andmove_on_after
relative_deadline
None
vsmath.inf
for no deadlinerelative_deadline
after entering the cm. See https://github.com/python-trio/trio/issues/2512#issuecomment-2180329727