Skip to content

Commit

Permalink
Merge pull request #322 from richardsheridan/api_type_hints
Browse files Browse the repository at this point in the history
add type hints for run_sync
  • Loading branch information
richardsheridan committed Sep 17, 2022
2 parents 72db7fc + 2dfd339 commit 28d8d3d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions newsfragments/322.feature.rst
@@ -0,0 +1 @@
Add type hints for `run_sync`
23 changes: 18 additions & 5 deletions trio_parallel/_impl.py
Expand Up @@ -4,14 +4,16 @@
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

from ._proc import WORKER_PROC_MAP
from ._abc import WorkerCache, AbstractWorker, NoPublicConstructor

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")
Expand Down Expand Up @@ -151,8 +153,14 @@ 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):
"""Run ``sync_fn(*args)`` in a separate process and return/raise it's outcome.
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 its outcome.
Behaves according to the customized attributes of the context. See
:func:`trio_parallel.run_sync()` for details.
Expand Down Expand Up @@ -337,8 +345,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:
Expand Down

0 comments on commit 28d8d3d

Please sign in to comment.