From b11d725ee3265d7c233e9cbd5a494ba73613009f Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Sat, 17 Sep 2022 12:07:20 -0400 Subject: [PATCH 1/3] add type hints for run_sync --- trio_parallel/_impl.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/trio_parallel/_impl.py b/trio_parallel/_impl.py index a71ccd04..2cf7a97b 100644 --- a/trio_parallel/_impl.py +++ b/trio_parallel/_impl.py @@ -4,7 +4,7 @@ from contextlib import asynccontextmanager from enum import Enum from itertools import count -from typing import Type, Callable, Any +from typing import Type, Callable, Any, TypeVar import attr import trio @@ -12,6 +12,11 @@ from ._proc import WORKER_PROC_MAP from ._abc import WorkerCache, AbstractWorker, NoPublicConstructor +# sphinx needs a little help to silence a warning about the origin of T +# https://github.com/sphinx-doc/sphinx/issues/7722#issuecomment-657231977 +#: T +T = TypeVar("T") + # Sane default might be to expect cpu-bound work DEFAULT_LIMIT = os.cpu_count() or 1 limiter_runvar = trio.lowlevel.RunVar("trio_parallel") @@ -151,7 +156,13 @@ def __attrs_post_init__(self): self.__dict__["_worker_cache"] = cache_class() @trio.lowlevel.enable_ki_protection - async def run_sync(self, sync_fn, *args, cancellable=False, limiter=None): + async def run_sync( + self, + sync_fn: Callable[..., T], + *args, + cancellable: bool = False, + limiter: trio.CapacityLimiter = None, + ) -> T: """Run ``sync_fn(*args)`` in a separate process and return/raise it's outcome. Behaves according to the customized attributes of the context. See @@ -337,8 +348,13 @@ def atexit_shutdown_grace_period(grace_period=-1.0): return ATEXIT_SHUTDOWN_GRACE_PERIOD -async def run_sync(sync_fn, *args, cancellable=False, limiter=None): - """Run ``sync_fn(*args)`` in a separate process and return/raise it's outcome. +async def run_sync( + sync_fn: Callable[..., T], + *args, + cancellable: bool = False, + limiter: trio.CapacityLimiter = None, +) -> T: + """Run ``sync_fn(*args)`` in a separate process and return/raise its outcome. This function is intended to enable the following: From 9061585b53111588f192cec90aec5060e4bc70d6 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Sat, 17 Sep 2022 13:12:25 -0400 Subject: [PATCH 2/3] silence nitpicky warning --- docs/source/conf.py | 1 + trio_parallel/_impl.py | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 04ac3d32..ec1d5f6c 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -42,6 +42,7 @@ # https://github.com/sphinx-doc/sphinx/issues/7722 ("py:class", "SendType"), ("py:class", "ReceiveType"), + ("py:class", "trio_parallel._impl.T"), ] autodoc_inherit_docstrings = False default_role = "obj" diff --git a/trio_parallel/_impl.py b/trio_parallel/_impl.py index 2cf7a97b..047452a2 100644 --- a/trio_parallel/_impl.py +++ b/trio_parallel/_impl.py @@ -12,9 +12,6 @@ from ._proc import WORKER_PROC_MAP from ._abc import WorkerCache, AbstractWorker, NoPublicConstructor -# sphinx needs a little help to silence a warning about the origin of T -# https://github.com/sphinx-doc/sphinx/issues/7722#issuecomment-657231977 -#: T T = TypeVar("T") # Sane default might be to expect cpu-bound work @@ -163,7 +160,7 @@ async def run_sync( cancellable: bool = False, limiter: trio.CapacityLimiter = None, ) -> T: - """Run ``sync_fn(*args)`` in a separate process and return/raise it's outcome. + """Run ``sync_fn(*args)`` in a separate process and return/raise its outcome. Behaves according to the customized attributes of the context. See :func:`trio_parallel.run_sync()` for details. From 2dfd339a558244904968789df381d4e842dc18f7 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Sat, 17 Sep 2022 13:13:48 -0400 Subject: [PATCH 3/3] add newsfragment --- newsfragments/322.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/322.feature.rst diff --git a/newsfragments/322.feature.rst b/newsfragments/322.feature.rst new file mode 100644 index 00000000..83966092 --- /dev/null +++ b/newsfragments/322.feature.rst @@ -0,0 +1 @@ +Add type hints for `run_sync` \ No newline at end of file