From dbc56a59272511b30ebf4f5ba4f1a42366a980cf Mon Sep 17 00:00:00 2001 From: phi Date: Tue, 30 Jul 2024 21:44:58 +0900 Subject: [PATCH] fix: ruff, pyright errors --- src/timeout_executor/executor.py | 19 +++++++++++++------ src/timeout_executor/main.py | 11 ++++++++++- src/timeout_executor/result.py | 4 +++- src/timeout_executor/serde.py | 14 +++++++------- src/timeout_executor/subprocess.py | 7 +++++-- src/timeout_executor/terminate.py | 6 +++++- src/timeout_executor/types.py | 5 +++-- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/timeout_executor/executor.py b/src/timeout_executor/executor.py index b28ec2d..7a2c97b 100644 --- a/src/timeout_executor/executor.py +++ b/src/timeout_executor/executor.py @@ -11,7 +11,7 @@ from itertools import chain from pathlib import Path from types import FunctionType -from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generic, Iterable, overload +from typing import TYPE_CHECKING, Any, Callable, Generic, overload from uuid import UUID, uuid4 import anyio @@ -25,6 +25,8 @@ from timeout_executor.types import Callback, CallbackArgs, ExecutorArgs, ProcessCallback if TYPE_CHECKING: + from collections.abc import Coroutine, Iterable + from timeout_executor.main import TimeoutExecutor __all__ = ["apply_func", "delay_func"] @@ -75,7 +77,7 @@ def _dump_args( ) -> bytes: input_args = (self._func, args, kwargs, output_file) logger.debug("%r before dump input args", self) - input_args_as_bytes = cloudpickle.dumps(input_args) # pyright: ignore[reportUnknownMemberType] + input_args_as_bytes = cloudpickle.dumps(input_args) logger.debug( "%r after dump input args :: size: %d", self, len(input_args_as_bytes) ) @@ -86,8 +88,8 @@ def _create_process( ) -> subprocess.Popen[str]: command = self._command(stacklevel=stacklevel + 1) logger.debug("%r before create new process", self, stacklevel=stacklevel) - process = subprocess.Popen( - command, # noqa: S603 + process = subprocess.Popen( # noqa: S603 + command, env={TIMEOUT_EXECUTOR_INPUT_FILE: input_file.as_posix()}, stdout=subprocess.PIPE, stderr=subprocess.PIPE, @@ -152,6 +154,7 @@ async def delay(self, *args: P.args, **kwargs: P.kwargs) -> AsyncResult[P, T]: return self._init_process(input_file, output_file) + @override def __repr__(self) -> str: return f"<{type(self).__name__}: {self._func_name}>" @@ -200,8 +203,10 @@ def apply_func( """run function with deadline Args: - timeout: deadline + timeout_or_executor: deadline func: func(sync or async) + *args: func args + **kwargs: func kwargs Returns: async result container @@ -242,8 +247,10 @@ async def delay_func( """run function with deadline Args: - timeout: deadline + timeout_or_executor: deadline func: func(sync or async) + *args: func args + **kwargs: func kwargs Returns: async result container diff --git a/src/timeout_executor/main.py b/src/timeout_executor/main.py index e79aa24..1af4c01 100644 --- a/src/timeout_executor/main.py +++ b/src/timeout_executor/main.py @@ -2,7 +2,7 @@ from collections import deque from contextlib import suppress -from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generic, Iterable, overload +from typing import TYPE_CHECKING, Any, Callable, Generic, overload from typing_extensions import ParamSpec, Self, TypeVar, override @@ -10,6 +10,8 @@ from timeout_executor.types import Callback, ProcessCallback if TYPE_CHECKING: + from collections.abc import Coroutine, Iterable + from timeout_executor.result import AsyncResult __all__ = ["TimeoutExecutor"] @@ -49,6 +51,8 @@ def apply( Args: func: func(sync or async) + *args: func args + **kwargs: func kwargs Returns: async result container @@ -73,6 +77,8 @@ async def delay( Args: func: func(sync or async) + *args: func args + **kwargs: func kwargs Returns: async result container @@ -99,12 +105,15 @@ async def apply_async( Args: func: func(sync or async) + *args: func args + **kwargs: func kwargs Returns: async result container """ return await self.delay(func, *args, **kwargs) + @override def __repr__(self) -> str: return f"<{type(self).__name__}, timeout: {self.timeout:.2f}s>" diff --git a/src/timeout_executor/result.py b/src/timeout_executor/result.py index 7ff2236..22a6f34 100644 --- a/src/timeout_executor/result.py +++ b/src/timeout_executor/result.py @@ -2,7 +2,7 @@ import subprocess from functools import partial -from typing import TYPE_CHECKING, Any, Generic, Iterable +from typing import TYPE_CHECKING, Any, Generic import anyio import cloudpickle @@ -14,6 +14,7 @@ from timeout_executor.types import Callback, ProcessCallback if TYPE_CHECKING: + from collections.abc import Iterable from pathlib import Path from timeout_executor.terminate import Terminator @@ -125,6 +126,7 @@ async def _load_output(self) -> T: await self._output.parent.rmdir() return await self._load_output() + @override def __repr__(self) -> str: return f"<{type(self).__name__}: {self._func_name}>" diff --git a/src/timeout_executor/serde.py b/src/timeout_executor/serde.py index 045923f..65b7e3d 100644 --- a/src/timeout_executor/serde.py +++ b/src/timeout_executor/serde.py @@ -9,10 +9,10 @@ import cloudpickle from tblib.pickling_support import ( - pickle_exception, # pyright: ignore[reportUnknownVariableType] - pickle_traceback, # pyright: ignore[reportUnknownVariableType] - unpickle_exception, # pyright: ignore[reportUnknownVariableType] - unpickle_traceback, # pyright: ignore[reportUnknownVariableType] + pickle_exception, + pickle_traceback, + unpickle_exception, + unpickle_traceback, ) __all__ = ["dumps_error", "loads_error", "serialize_error", "deserialize_error"] @@ -34,7 +34,7 @@ class SerializedError: def serialize_traceback(traceback: TracebackType) -> tuple[Any, ...]: - return pickle_traceback(traceback) # pyright: ignore[reportUnknownVariableType] + return pickle_traceback(traceback) def serialize_error(error: BaseException) -> SerializedError: @@ -84,7 +84,7 @@ def deserialize_error(error: SerializedError) -> BaseException: traceback = unpickle_traceback(*value) result.insert(index + salt, traceback) - return unpickle_exception(*arg_exception, *exception) # pyright: ignore[reportUnknownVariableType] + return unpickle_exception(*arg_exception, *exception) def dumps_error(error: BaseException | SerializedError) -> bytes: @@ -92,7 +92,7 @@ def dumps_error(error: BaseException | SerializedError) -> bytes: if not isinstance(error, SerializedError): error = serialize_error(error) - return cloudpickle.dumps(error) # pyright: ignore[reportUnknownMemberType] + return cloudpickle.dumps(error) def loads_error(error: bytes | SerializedError) -> BaseException: diff --git a/src/timeout_executor/subprocess.py b/src/timeout_executor/subprocess.py index 33858f7..2c49076 100644 --- a/src/timeout_executor/subprocess.py +++ b/src/timeout_executor/subprocess.py @@ -5,7 +5,7 @@ from inspect import iscoroutinefunction from os import environ from pathlib import Path -from typing import Any, Callable, Coroutine +from typing import TYPE_CHECKING, Any, Callable import anyio import cloudpickle @@ -14,6 +14,9 @@ from timeout_executor.const import TIMEOUT_EXECUTOR_INPUT_FILE from timeout_executor.serde import dumps_error +if TYPE_CHECKING: + from collections.abc import Coroutine + __all__ = [] P = ParamSpec("P") @@ -37,7 +40,7 @@ def run_in_subprocess() -> None: def dumps_value(value: Any) -> bytes: if isinstance(value, BaseException): return dumps_error(value) - return cloudpickle.dumps(value) # pyright: ignore[reportUnknownMemberType] + return cloudpickle.dumps(value) def output_to_file( diff --git a/src/timeout_executor/terminate.py b/src/timeout_executor/terminate.py index fc722ed..89c9062 100644 --- a/src/timeout_executor/terminate.py +++ b/src/timeout_executor/terminate.py @@ -6,7 +6,7 @@ from collections import deque from contextlib import suppress from itertools import chain -from typing import Any, Callable, Generic, Iterable +from typing import TYPE_CHECKING, Any, Callable, Generic from psutil import pid_exists from typing_extensions import ParamSpec, Self, TypeVar, override @@ -14,6 +14,9 @@ from timeout_executor.logging import logger from timeout_executor.types import Callback, CallbackArgs, ExecutorArgs, ProcessCallback +if TYPE_CHECKING: + from collections.abc import Iterable + __all__ = [] P = ParamSpec("P") @@ -142,6 +145,7 @@ def close(self, name: str | None = None) -> None: if text: sys.stderr.write(text) + @override def __repr__(self) -> str: return f"<{type(self).__name__}: {self.func_name}>" diff --git a/src/timeout_executor/types.py b/src/timeout_executor/types.py index d93e8fa..d513f2f 100644 --- a/src/timeout_executor/types.py +++ b/src/timeout_executor/types.py @@ -4,17 +4,18 @@ from abc import ABC, abstractmethod from collections import deque from dataclasses import dataclass, field -from typing import TYPE_CHECKING, Any, Callable, Generic, Iterable +from typing import TYPE_CHECKING, Any, Callable, Generic from typing_extensions import ParamSpec, TypeVar from timeout_executor.logging import logger if sys.version_info < (3, 11): - from exceptiongroup import ExceptionGroup # type: ignore + from exceptiongroup import ExceptionGroup if TYPE_CHECKING: import subprocess + from collections.abc import Iterable from pathlib import Path import anyio