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

♻️ Deprecate asyncify(cancellable=True) in favor of asyncify(abandon_on_cancel=True), following AnyIO 4.1.0 #202

Merged
merged 4 commits into from
Aug 24, 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 .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
if: matrix.anyio-version == 'anyio-v4'
run: pip install --upgrade "anyio>=4.0.0,<5.0"
- name: Lint
if: matrix.anyio-version == 'anyio-v4'
run: bash scripts/lint.sh
- run: mkdir coverage
- name: Test
Expand Down
37 changes: 37 additions & 0 deletions asyncer/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# AnyIO 4.1.0 renamed cancellable to abandon_on_cancel
import importlib
import importlib.metadata
from typing import Callable, TypeVar, Union

import anyio
import anyio.to_thread
from anyio import CapacityLimiter
from typing_extensions import TypeVarTuple, Unpack

ANYIO_VERSION = importlib.metadata.version("anyio")

T_Retval = TypeVar("T_Retval")
PosArgsT = TypeVarTuple("PosArgsT")

if ANYIO_VERSION >= "4.1.0":

async def run_sync(
func: Callable[[Unpack[PosArgsT]], T_Retval],
*args: Unpack[PosArgsT],
abandon_on_cancel: bool = False,
limiter: Union[CapacityLimiter, None] = None,
) -> T_Retval:
return await anyio.to_thread.run_sync(
func, *args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
)
else:

async def run_sync(
func: Callable[[Unpack[PosArgsT]], T_Retval],
*args: Unpack[PosArgsT],
abandon_on_cancel: bool = False,
limiter: Union[CapacityLimiter, None] = None,
) -> T_Retval:
return await anyio.to_thread.run_sync(
func, *args, cancellable=abandon_on_cancel, limiter=limiter
)
20 changes: 17 additions & 3 deletions asyncer/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
TypeVar,
Union,
)
from warnings import warn

from asyncer._compat import run_sync

if sys.version_info >= (3, 10):
from typing import ParamSpec
Expand Down Expand Up @@ -319,7 +322,8 @@ def wrapper(*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs) -> T_Retval:
def asyncify(
function: Callable[T_ParamSpec, T_Retval],
*,
cancellable: bool = False,
abandon_on_cancel: bool = False,
cancellable: Union[bool, None] = None,
limiter: Optional[anyio.CapacityLimiter] = None,
) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:
"""
Expand Down Expand Up @@ -359,14 +363,24 @@ def do_work(arg1, arg2, kwarg1="", kwarg2="") -> str:
original one, that when called runs the same original function in a thread worker
and returns the result.
"""
if cancellable is not None:
abandon_on_cancel = cancellable
warn(
"The `cancellable=` keyword argument to `asyncer.asyncify()` is "
"deprecated since Asyncer 0.0.8, following AnyIO 4.1.0. "
"Use `abandon_on_cancel=` instead.",
DeprecationWarning,
stacklevel=2,
)

@functools.wraps(function)
async def wrapper(
*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
) -> T_Retval:
partial_f = functools.partial(function, *args, **kwargs)
return await anyio.to_thread.run_sync(
partial_f, cancellable=cancellable, limiter=limiter

return await run_sync(
partial_f, abandon_on_cancel=abandon_on_cancel, limiter=limiter
)

return wrapper
37 changes: 37 additions & 0 deletions tests/test_param_cancellable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import warnings

import anyio
import pytest
from asyncer import asyncify


def test_cancellable_warns():
def do_async_work():
return "Hello World!"

async def main():
result = await asyncify(do_async_work, cancellable=True)()
return result

with pytest.warns(DeprecationWarning) as record:
result = anyio.run(main)
assert isinstance(record[0].message, Warning)
assert (
"The `cancellable=` keyword argument to `asyncer.asyncify()` is "
"deprecated since Asyncer 0.0.8" in record[0].message.args[0]
)
assert result == "Hello World!"


def test_abandon_on_cancel_no():
def do_async_work():
return "Hello World!"

async def main():
result = await asyncify(do_async_work, abandon_on_cancel=True)()
return result

with warnings.catch_warnings():
warnings.simplefilter("error")
result = anyio.run(main)
assert result == "Hello World!"