From 2fa14c19cdf73e7421a640643baa0d62087df850 Mon Sep 17 00:00:00 2001 From: phi Date: Sat, 7 Sep 2024 12:39:08 +0900 Subject: [PATCH] fix: ruff,pyright errors --- ruff.toml | 3 ++ src/async_wrapper/convert/_async.py | 2 + src/async_wrapper/convert/main.py | 2 + tests/convert/test_async.py | 2 +- tests/convert/test_sync.py | 5 ++- tests/convert/test_toggle.py | 4 +- tests/task_group/test_task_group.py | 2 +- tests/task_group/test_value.py | 2 +- tests/test_pipe.py | 27 ++++++------ tests/test_queue.py | 64 ++++++++++++++--------------- tests/test_wait.py | 24 +++++------ 11 files changed, 73 insertions(+), 64 deletions(-) diff --git a/ruff.toml b/ruff.toml index 69458d5..9dcef2b 100644 --- a/ruff.toml +++ b/ruff.toml @@ -31,6 +31,7 @@ fixable = [ "PIE808", # unnecessary-range-start "RUF020", # never-union "NPY201", # numpy2-deprecation + "PT", ] ignore = [ "TD", @@ -87,6 +88,8 @@ ignore = [ # pydocstyle # TODO "D100", # undocumented-public-module "D104", # undocumented-public-package + # async + "ASYNC109", # async-function-with-timeout ] [lint.per-file-ignores] diff --git a/src/async_wrapper/convert/_async.py b/src/async_wrapper/convert/_async.py index 1308643..6cef06d 100644 --- a/src/async_wrapper/convert/_async.py +++ b/src/async_wrapper/convert/_async.py @@ -1,3 +1,5 @@ +# FIXME: pyright v1.1.379 && pylance v2024.8.2 +# pyright: reportUnnecessaryTypeIgnoreComment=false from __future__ import annotations from functools import cached_property, partial, wraps diff --git a/src/async_wrapper/convert/main.py b/src/async_wrapper/convert/main.py index 9462612..9f5302f 100644 --- a/src/async_wrapper/convert/main.py +++ b/src/async_wrapper/convert/main.py @@ -1,3 +1,5 @@ +# FIXME: pyright v1.1.379 && pylance v2024.8.2 +# pyright: reportUnnecessaryTypeIgnoreComment=false from __future__ import annotations from inspect import iscoroutinefunction diff --git a/tests/convert/test_async.py b/tests/convert/test_async.py index 07a29e1..7f38dbf 100644 --- a/tests/convert/test_async.py +++ b/tests/convert/test_async.py @@ -62,7 +62,7 @@ async def engine( await engine.dispose() -@pytest.fixture() +@pytest.fixture async def session(engine: AsyncEngine) -> AsyncGenerator[AsyncSession, None]: async with AsyncSession(engine) as session: yield session diff --git a/tests/convert/test_sync.py b/tests/convert/test_sync.py index 75ac147..dd94f86 100644 --- a/tests/convert/test_sync.py +++ b/tests/convert/test_sync.py @@ -5,6 +5,7 @@ import anyio import pytest +from anyio.lowlevel import checkpoint from sniffio import current_async_library from typing_extensions import TypeVar @@ -50,7 +51,7 @@ def test_toggle(self, x: int): sample(x, self.epsilon) assert self.epsilon * x < timer.term < self.epsilon * x + self.epsilon - @pytest.mark.anyio() + @pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_async_to_sync_in_async(self, x: int): backend = current_async_library() @@ -72,7 +73,7 @@ def __await__(self) -> Generator[Any, None, ValueT]: def sample_coroutine(value: ValueT) -> Coroutine[Any, Any, ValueT]: async def inner() -> ValueT: - await anyio.sleep(0) + await checkpoint() return value return inner() diff --git a/tests/convert/test_toggle.py b/tests/convert/test_toggle.py index 23b3032..3a44d78 100644 --- a/tests/convert/test_toggle.py +++ b/tests/convert/test_toggle.py @@ -3,8 +3,8 @@ from collections.abc import Callable from typing import Any -import anyio import pytest +from anyio.lowlevel import checkpoint from async_wrapper import toggle_func @@ -14,7 +14,7 @@ def sample_sync_func(x: Any) -> Any: async def sample_async_func(x: Any) -> Any: - await anyio.sleep(0) + await checkpoint() return x diff --git a/tests/task_group/test_task_group.py b/tests/task_group/test_task_group.py index 9dbd55a..e109eb8 100644 --- a/tests/task_group/test_task_group.py +++ b/tests/task_group/test_task_group.py @@ -12,7 +12,7 @@ from async_wrapper.task_group import SoonValue -@pytest.mark.anyio() +@pytest.mark.anyio class TestTaskGroupWrapper: epsilon: Final[float] = 0.1 diff --git a/tests/task_group/test_value.py b/tests/task_group/test_value.py index deed3f8..185fa5c 100644 --- a/tests/task_group/test_value.py +++ b/tests/task_group/test_value.py @@ -30,7 +30,7 @@ def test_value_setattr(x: int): assert maybe == x -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(4)) async def test_value_setattr_async(x: int): value: SoonValue[Any] = SoonValue() diff --git a/tests/test_pipe.py b/tests/test_pipe.py index 18936a5..6421d37 100644 --- a/tests/test_pipe.py +++ b/tests/test_pipe.py @@ -9,6 +9,7 @@ import anyio import pytest +from anyio.lowlevel import checkpoint from typing_extensions import TypeVar, override from .base import Timer @@ -41,12 +42,12 @@ def is_disposed(self) -> bool: return self.disposed async def next(self, value: Any) -> Any: - await anyio.sleep(0) + await checkpoint() self.value = value return value async def dispose(self) -> Any: - await anyio.sleep(0) + await checkpoint() self.disposed = True if self._dispose is not None: @@ -121,12 +122,12 @@ def subscribable_type(request: pytest.FixtureRequest) -> type[Subscribable[Any, async def as_tuple(value: ValueT) -> tuple[ValueT]: - await anyio.sleep(0) + await checkpoint() return (value,) async def return_self(value: ValueT) -> ValueT: - await anyio.sleep(0) + await checkpoint() return value @@ -134,12 +135,12 @@ def use_value(): result = None async def getter() -> Any: - await anyio.sleep(0) + await checkpoint() return result async def setter(value: Any) -> None: nonlocal result - await anyio.sleep(0) + await checkpoint() result = value return getter, setter @@ -154,7 +155,7 @@ def check_hit() -> None: async def hit(value: Any) -> None: # noqa: ARG001 nonlocal flag - await anyio.sleep(0) + await checkpoint() flag = True pipe = Pipe(hit) @@ -213,7 +214,7 @@ async def test_subscribe_many(x: int, subscribable_type: type[Subscribable[Any, async def hit(value: Any, index: int) -> None: nonlocal check - await anyio.sleep(0) + await checkpoint() check[index] = value pipe: Subscribable[int, tuple[Any, ...]] = _construct_subcribable( @@ -304,7 +305,7 @@ async def test_dispose(subscribable_type: type[Subscribable[Any, Any]]): async def hit() -> None: nonlocal flag - await anyio.sleep(0) + await checkpoint() flag = True pipe: Subscribable[Any, Any] @@ -329,7 +330,7 @@ async def test_dispose_many(subscribable_type: type[Subscribable[Any, Any]]): async def hit(index: int) -> None: nonlocal check - await anyio.sleep(0) + await checkpoint() check[index] = True pipe: Subscribable[Any, Any] = _construct_subcribable( @@ -369,7 +370,7 @@ async def test_pipe_dispose_only_once(): async def hit() -> None: nonlocal count - await anyio.sleep(0) + await checkpoint() count += 1 pipe = Pipe(return_self, dispose=hit) @@ -384,7 +385,7 @@ async def test_do_not_dispose(subscribable_type: type[Subscribable[Any, Any]]): async def hit() -> None: nonlocal flag - await anyio.sleep(0) + await checkpoint() flag = True pipe: Subscribable[int, int] = _construct_subcribable( @@ -467,7 +468,7 @@ async def test_pipe_next_after_disposed(): async def hit(value: Any) -> None: # noqa: ARG001 nonlocal flag - await anyio.sleep(0) + await checkpoint() flag = True pipe = Pipe(hit) diff --git a/tests/test_queue.py b/tests/test_queue.py index 21b4c42..a0ab6c2 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -30,7 +30,7 @@ def test_negative_max_buffer() -> None: Queue(-1) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aget_then_aput() -> None: queue: Queue[str] = create_queue() result: list[str] = [] @@ -48,7 +48,7 @@ async def getter() -> None: assert result == ["hello", "anyio"] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aget_then_put() -> None: queue: Queue[str] = create_queue() result: list[str] = [] @@ -66,7 +66,7 @@ async def getter() -> None: assert sorted(result, reverse=True) == ["hello", "anyio"] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aput_then_get() -> None: queue: Queue[str] = create_queue() async with create_task_group() as task_group: @@ -75,7 +75,7 @@ async def test_aput_then_get() -> None: assert queue.get() == "hello" -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aput_is_unblocked_after_get() -> None: queue: Queue[str] = create_queue() queue.put("hello") @@ -89,7 +89,7 @@ async def test_aput_is_unblocked_after_get() -> None: assert queue.get() == "anyio" -@pytest.mark.anyio() +@pytest.mark.anyio async def test_put_then_get() -> None: queue: Queue[str] = create_queue() queue.put("hello") @@ -99,7 +99,7 @@ async def test_put_then_get() -> None: assert queue.get() == "anyio" -@pytest.mark.anyio() +@pytest.mark.anyio async def test_iterate() -> None: queue: Queue[str] = create_queue() result: list[str] = [] @@ -119,7 +119,7 @@ async def getter() -> None: assert result == ["hello", "anyio"] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aget_aput_closed_queue() -> None: queue: Queue[Any] = create_queue() @@ -137,7 +137,7 @@ async def test_aget_aput_closed_queue() -> None: await queue.aput(None) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_clone() -> None: queue: Queue[str] = create_queue(1) putter = queue.clone.putter @@ -148,7 +148,7 @@ async def test_clone() -> None: assert getter.get() == "hello" -@pytest.mark.anyio() +@pytest.mark.anyio async def test_clone_closed() -> None: queue: Queue[str] = create_queue(1) await queue.aclose() @@ -160,7 +160,7 @@ async def test_clone_closed() -> None: _ = queue.clone.putter -@pytest.mark.anyio() +@pytest.mark.anyio async def test_clone_closed_using_cloning() -> None: queue: Queue[str] = create_queue(1) await queue.aclose() @@ -168,7 +168,7 @@ async def test_clone_closed_using_cloning() -> None: _ = queue.clone -@pytest.mark.anyio() +@pytest.mark.anyio async def test_clone_closed_using_cloning_after_create() -> None: queue: Queue[str] = create_queue(1) clone = queue.clone @@ -177,7 +177,7 @@ async def test_clone_closed_using_cloning_after_create() -> None: _ = clone.getter -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aget_when_cancelled() -> None: queue: Queue[str] = create_queue() async with create_task_group() as task_group: @@ -194,7 +194,7 @@ async def test_aget_when_cancelled() -> None: assert await queue.aget() == "world" -@pytest.mark.anyio() +@pytest.mark.anyio async def test_aput_when_cancelled() -> None: queue: Queue[str] = create_queue() result: list[str] = [] @@ -212,7 +212,7 @@ async def getter() -> None: assert result == ["world"] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_cancel_during_aget() -> None: receiver_scope: CancelScope | None = None queue: Queue[str] = create_queue() @@ -235,7 +235,7 @@ async def scoped_getter() -> None: assert result == ["hello"] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_close_queue_after_aput() -> None: queue: Queue[str] = create_queue() @@ -252,7 +252,7 @@ async def get() -> None: task_group.start_soon(get) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_statistics() -> None: queue: Queue[None] = create_queue(1) streams = queue._putter, queue._getter # noqa: SLF001 @@ -308,7 +308,7 @@ async def test_statistics() -> None: assert stream.statistics().tasks_waiting_receive == 0 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_sync_close() -> None: queue: Queue[None] = create_queue(1) with queue: @@ -321,7 +321,7 @@ async def test_sync_close() -> None: queue.get() -@pytest.mark.anyio() +@pytest.mark.anyio async def test_clone_each(): queue: Queue[Any] = create_queue(1) @@ -347,7 +347,7 @@ async def test_get(q: Queue[Any]) -> None: assert status.open_send_streams == 1 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_queue_async_iterator_aputter(): queue: Queue[Any] = create_queue(10) @@ -366,7 +366,7 @@ async def put(value: Any, queue: Queue[Any]) -> None: assert set(result) == set(range(10)) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_queue_iterator_aputter(): queue: Queue[Any] = create_queue(10) @@ -385,7 +385,7 @@ async def put(value: Any, queue: Queue[Any]) -> None: assert set(result) == set(range(10)) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_queue_async_iterator(): queue: Queue[Any] = create_queue(10) @@ -407,7 +407,7 @@ async def put(value: Any, queue: Queue[Any]) -> None: assert queue._closed # noqa: SLF001 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_queue_iterator(): queue: Queue[Any] = create_queue(10) @@ -429,7 +429,7 @@ async def put(value: Any, queue: Queue[Any]) -> None: assert queue._closed # noqa: SLF001 -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_queue_empty(x: int): queue: Queue[Any] = create_queue(x) @@ -441,7 +441,7 @@ async def test_queue_empty(x: int): assert queue.empty() -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_queue_full(x: int): queue: Queue[Any] = create_queue(x) @@ -457,7 +457,7 @@ async def test_queue_full(x: int): assert queue.full() -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_queue_size(x: int): queue: Queue[Any] = create_queue(x) @@ -471,7 +471,7 @@ async def test_queue_size(x: int): assert queue.qsize() == x - 1 -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_queue_size_using_len(x: int): queue: Queue[Any] = create_queue(x) @@ -485,14 +485,14 @@ async def test_queue_size_using_len(x: int): assert len(queue) == x - 1 -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", range(1, 4)) async def test_queue_length(x: int): queue: Queue[Any] = create_queue(x) assert queue.maxsize == x -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", chain((None,), range(1, 4))) async def test_queue_repr(x: int | None): queue: Queue[Any] = create_queue(x) @@ -507,7 +507,7 @@ async def test_queue_repr(x: int | None): assert repr(queue) == expected_repr -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", chain((None,), range(1, 4))) async def test_cloning_repr(x: int | None): queue: Queue[Any] = create_queue(x) @@ -522,7 +522,7 @@ async def test_cloning_repr(x: int | None): assert repr(queue.clone) == expected_repr -@pytest.mark.anyio() +@pytest.mark.anyio @pytest.mark.parametrize("x", chain((None,), range(1, 4))) async def test_restricted_queue_repr(x: int | None): queue: Queue[Any] = create_queue(x) @@ -539,7 +539,7 @@ async def test_restricted_queue_repr(x: int | None): assert repr(clone) == expected_repr.format(where) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_restricted_queue_error(): queue = create_queue() clone = queue.clone @@ -562,7 +562,7 @@ async def test_restricted_queue_error(): _ = putter.clone -@pytest.mark.anyio() +@pytest.mark.anyio async def test_restricted_queue_eixt(): queue = create_queue() result: list[Any] = [] diff --git a/tests/test_wait.py b/tests/test_wait.py index 0f3f80f..a815de1 100644 --- a/tests/test_wait.py +++ b/tests/test_wait.py @@ -14,7 +14,7 @@ TIC = 0.01 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_waiter(): soon: SoonValue[Any] = SoonValue() @@ -27,7 +27,7 @@ async def test_waiter(): assert soon.value == 1 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_waiter_reuse(): soon: SoonValue[Any] = SoonValue() new_soon: SoonValue[Any] = SoonValue() @@ -46,7 +46,7 @@ async def test_waiter_reuse(): assert new_soon.value == 2 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_waiter_reuse_overwrite(): soon: SoonValue[Any] = SoonValue() @@ -61,7 +61,7 @@ async def test_waiter_reuse_overwrite(): assert soon.value == 3 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_wait_for(): event = anyio.Event() soon: SoonValue[Any] = SoonValue() @@ -74,7 +74,7 @@ async def test_wait_for(): assert soon.value == 1 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_wait_many(): events = [anyio.Event() for _ in range(10)] soon: SoonValue[Any] = SoonValue() @@ -89,7 +89,7 @@ async def test_wait_many(): assert soon.value == 1 -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_with_task_group(): result: list[int] = [] async with anyio.create_task_group() as task_group: @@ -103,7 +103,7 @@ async def test_completed_with_task_group(): assert result == [3, 2, 1] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_without_task_group(): result: list[int] = [] async with anyio.create_task_group() as task_group: @@ -117,7 +117,7 @@ async def test_completed_without_task_group(): assert result == [3, 2, 1] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_overwrite_task_group(): result: list[int] = [] async with anyio.create_task_group() as task_group: @@ -131,7 +131,7 @@ async def test_completed_overwrite_task_group(): assert result == [3, 2, 1] -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_overwrite_diff_task_group(): async with anyio.create_task_group() as task_group: async with Completed(task_group) as completed: @@ -140,7 +140,7 @@ async def test_completed_overwrite_diff_task_group(): completed.start_soon(new_tg, sample_func2, 2, 2) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_no_task_group(): async with anyio.create_task_group(): async with Completed() as completed: @@ -148,7 +148,7 @@ async def test_completed_no_task_group(): completed.start_soon(None, sample_func2, 2, 2) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_without_enter(): async with anyio.create_task_group() as task_group: completed = Completed(task_group) @@ -156,7 +156,7 @@ async def test_completed_without_enter(): completed.start_soon(None, sample_func2, 2, 2) -@pytest.mark.anyio() +@pytest.mark.anyio async def test_completed_after_exit(): async with anyio.create_task_group() as task_group: async with Completed(task_group) as completed: