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

add type hints for run_sync #322

Merged
merged 3 commits into from Sep 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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