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

In to_thread_run_sync(), add abandon_on_cancel= as an alias for the cancellable= flag #2841

Merged
merged 9 commits into from
Nov 2, 2023
17 changes: 9 additions & 8 deletions trio/_tests/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CancelScope,
CapacityLimiter,
Event,
TrioDeprecationWarning,
_core,
fail_after,
move_on_after,
Expand Down Expand Up @@ -556,7 +557,7 @@ def release_on_behalf_of(self, borrower: Task) -> None:

# TODO: should CapacityLimiter have an abc or protocol so users can modify it?
# because currently it's `final` so writing code like this is not allowed.
await to_thread_run_sync(lambda: None, limiter=CustomLimiter()) # type: ignore[arg-type]
await to_thread_run_sync(lambda: None, limiter=CustomLimiter()) # type: ignore[call-overload]
assert record == ["acquire", "release"]


Expand All @@ -574,7 +575,7 @@ def release_on_behalf_of(self, borrower: Task) -> NoReturn:
bs = BadCapacityLimiter()

with pytest.raises(ValueError) as excinfo:
await to_thread_run_sync(lambda: None, limiter=bs) # type: ignore[arg-type]
await to_thread_run_sync(lambda: None, limiter=bs) # type: ignore[call-overload]
assert excinfo.value.__context__ is None
assert record == ["acquire", "release"]
record = []
Expand All @@ -583,7 +584,7 @@ def release_on_behalf_of(self, borrower: Task) -> NoReturn:
# chains with it
d: dict[str, object] = {}
with pytest.raises(ValueError) as excinfo:
await to_thread_run_sync(lambda: d["x"], limiter=bs) # type: ignore[arg-type]
await to_thread_run_sync(lambda: d["x"], limiter=bs) # type: ignore[call-overload]
assert isinstance(excinfo.value.__context__, KeyError)
assert record == ["acquire", "release"]

Expand Down Expand Up @@ -892,7 +893,7 @@ def __bool__(self) -> bool:
raise NotImplementedError

with pytest.raises(NotImplementedError):
await to_thread_run_sync(int, abandon_on_cancel=BadBool()) # type: ignore[arg-type]
await to_thread_run_sync(int, abandon_on_cancel=BadBool()) # type: ignore[call-overload]


async def test_from_thread_reuses_task() -> None:
Expand Down Expand Up @@ -1081,15 +1082,15 @@ async def child() -> None:

async def test_cancellable_and_abandon_raises() -> None:
with pytest.raises(ValueError):
await to_thread_run_sync(bool, cancellable=True, abandon_on_cancel=False)
await to_thread_run_sync(bool, cancellable=True, abandon_on_cancel=False) # type: ignore[call-overload]

with pytest.raises(ValueError):
await to_thread_run_sync(bool, cancellable=True, abandon_on_cancel=True)
await to_thread_run_sync(bool, cancellable=True, abandon_on_cancel=True) # type: ignore[call-overload]


async def test_cancellable_warns() -> None:
with pytest.warns(DeprecationWarning):
with pytest.warns(TrioDeprecationWarning):
await to_thread_run_sync(bool, cancellable=False)

with pytest.warns(DeprecationWarning):
with pytest.warns(TrioDeprecationWarning):
await to_thread_run_sync(bool, cancellable=True)
22 changes: 11 additions & 11 deletions trio/_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import inspect
import queue as stdlib_queue
import threading
import warnings
from collections.abc import Awaitable, Callable
from itertools import count
from typing import Generic, TypeVar, overload
Expand All @@ -16,7 +15,6 @@
from sniffio import current_async_library_cvar

import trio
from trio._core._traps import RaiseCancelT

from ._core import (
RunVar,
Expand All @@ -25,6 +23,8 @@
enable_ki_protection,
start_thread_soon,
)
from ._core._traps import RaiseCancelT
from ._deprecate import warn_deprecated
from ._sync import CapacityLimiter
from ._util import coroutine_or_error

Expand Down Expand Up @@ -172,8 +172,8 @@ def run_in_system_nursery(self, token: TrioToken) -> None:
token.run_sync_soon(self.run_sync)


@overload
async def to_thread_run_sync(
@overload # Decorator used on function with Coroutine[Any, Any, RetT]
async def to_thread_run_sync( # type: ignore[misc]
sync_fn: Callable[..., RetT],
*args: object,
thread_name: str | None = None,
Expand All @@ -183,8 +183,8 @@ async def to_thread_run_sync(
...


@overload
async def to_thread_run_sync(
@overload # Decorator used on function with Coroutine[Any, Any, RetT]
async def to_thread_run_sync( # type: ignore[misc]
sync_fn: Callable[..., RetT],
*args: object,
thread_name: str | None = None,
Expand Down Expand Up @@ -292,11 +292,11 @@ async def to_thread_run_sync( # type: ignore[misc]
raise ValueError(
"Cannot set `cancellable` and `abandon_on_cancel` simultaneously."
)
warnings.warn(
DeprecationWarning(
"`cancellable` keyword argument is deprecated, "
"use `abandon on cancel` instead."
)
warn_deprecated(
"`cancellable=` keyword argument",
richardsheridan marked this conversation as resolved.
Show resolved Hide resolved
"0.23.0",
issue=2841,
instead="`abandon_on_cancel=` keyword argument",
richardsheridan marked this conversation as resolved.
Show resolved Hide resolved
)
abandon_on_cancel = cancellable
# raise early if abandon_on_cancel.__bool__ raises
Expand Down