Skip to content

Commit

Permalink
Make use of type aliases for backwards compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
  • Loading branch information
noxdafox committed Nov 24, 2024
1 parent da8e4c4 commit 3454fad
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 25 deletions.
7 changes: 2 additions & 5 deletions pebble/asynchronous/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@


@overload
def process(
func: Callable[[common.P], common.T]
) -> Callable[[common.P], asyncio.Future[common.T]]:
def process(func: common.CallableType) -> common.AsyncIODecoratorReturnType:
...
@overload
def process(
Expand All @@ -40,8 +38,7 @@ def process(
timeout: Optional[float] = None,
mp_context: Optional[multiprocessing.context.BaseContext] = None,
pool: Optional[ProcessPool] = None
) -> Callable[[Callable[[common.P], common.T]],
Callable[[common.P], asyncio.Future[common.T]]]:
) -> common.AsyncIODecoratorParamsReturnType:
...
def process(*args, **kwargs):
"""Runs the decorated function in a concurrent process,
Expand Down
7 changes: 2 additions & 5 deletions pebble/asynchronous/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@


@overload
def thread(
func: Callable[[common.P], common.T]
) -> Callable[[common.P], asyncio.Future[common.T]]:
def thread(func: common.CallableType) -> common.AsyncIODecoratorReturnType:
...
@overload
def thread(
name: Optional[str] = None,
daemon: bool = True,
pool: Optional[ThreadPool] = None
) -> Callable[[Callable[[common.P], common.T]],
Callable[[common.P], asyncio.Future[common.T]]]:
) -> common.AsyncIODecoratorParamsReturnType:
...
def thread(*args, **kwargs):
"""Runs the decorated function within a concurrent thread,
Expand Down
8 changes: 7 additions & 1 deletion pebble/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from pebble.common.shared import decorate_function, get_asyncio_loop
from pebble.common.types import ProcessExpired, ProcessFuture, PebbleFuture
from pebble.common.types import Result, ResultStatus, RemoteException
from pebble.common.types import FutureStatus, CONSTS, P, T
from pebble.common.types import FutureStatus, CONSTS, CallableType
from pebble.common.types import AsyncIODecoratorReturnType
from pebble.common.types import AsyncIODecoratorParamsReturnType
from pebble.common.types import ThreadDecoratorReturnType
from pebble.common.types import ThreadDecoratorParamsReturnType
from pebble.common.types import ProcessDecoratorReturnType
from pebble.common.types import ProcessDecoratorParamsReturnType
from pebble.common.process import launch_process, stop_process
from pebble.common.process import register_function, maybe_install_trampoline
from pebble.common.process import process_execute, send_result, function_handler
41 changes: 38 additions & 3 deletions pebble/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pebble. If not, see <http://www.gnu.org/licenses/>.

from typing import Any, TypeVar
import asyncio

from enum import Enum, IntEnum
from dataclasses import dataclass
from concurrent.futures import Future
from typing import Any, TypeVar, Callable


P = TypeVar("P")
T = TypeVar("T")


try:
FutureType = Future[T]
except TypeError:
FutureType = Future


class ProcessExpired(OSError):
"""Raised when process dies unexpectedly."""
def __init__(self, msg, code=0, pid=None):
Expand All @@ -32,7 +40,7 @@ def __init__(self, msg, code=0, pid=None):
self.pid = pid


class PebbleFuture(Future):
class PebbleFuture(FutureType):
# Same as base class, removed logline
def set_running_or_notify_cancel(self):
"""Mark the future as running or process any cancel notifications.
Expand Down Expand Up @@ -71,7 +79,13 @@ def set_running_or_notify_cancel(self):
raise RuntimeError('Future in unexpected state')


class ProcessFuture(PebbleFuture):
try:
PebbleFutureType = PebbleFuture[T]
except TypeError:
PebbleFutureType = PebbleFuture


class ProcessFuture(PebbleFutureType):
def cancel(self):
"""Cancel the future.
Expand Down Expand Up @@ -167,4 +181,25 @@ class Consts:
the amount of seconds to wait before issuing a SIGKILL signal."""


try:
CallableType = Callable[[P], T]
AsyncIODecoratorReturnType = Callable[[P], asyncio.Future[T]]
AsyncIODecoratorParamsReturnType = Callable[[Callable[[P], T]],
Callable[[P], asyncio.Future[T]]]
ThreadDecoratorReturnType = Callable[[P], Future[T]]
ThreadDecoratorParamsReturnType = Callable[[Callable[[P], T]],
Callable[[P], Future[T]]]
ProcessDecoratorReturnType = Callable[[P], ProcessFuture[T]]
ProcessDecoratorParamsReturnType = Callable[[Callable[[P], T]],
Callable[[P], ProcessFuture[T]]]
except TypeError:
ReturnType = Callable
AsyncIODecoratorReturnType = Callable
AsyncIODecoratorParamsReturnType = Callable
ThreadDecoratorReturnType = Callable
ThreadDecoratorParamsReturnType = Callable
ProcessDecoratorReturnType = Callable
ProcessDecoratorParamsReturnType = Callable


CONSTS = Consts()
7 changes: 2 additions & 5 deletions pebble/concurrent/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@


@overload
def process(
func: Callable[[common.P], common.T]
) -> Callable[[common.P], common.ProcessFuture[common.T]]:
def process(func: common.CallableType) -> common.ProcessDecoratorReturnType:
...
@overload
def process(
Expand All @@ -40,8 +38,7 @@ def process(
timeout: Optional[float] = None,
mp_context: Optional[multiprocessing.context.BaseContext] = None,
pool: Optional[ProcessPool] = None
) -> Callable[[Callable[[common.P], common.T]],
Callable[[common.P], common.ProcessFuture[common.T]]]:
) -> common.ProcessDecoratorParamsReturnType:
...
def process(*args, **kwargs):
"""Runs the decorated function in a concurrent process,
Expand Down
9 changes: 3 additions & 6 deletions pebble/concurrent/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@

from functools import wraps
from concurrent.futures import Future
from typing import Callable, Optional, Union, overload
from typing import Callable, Optional, overload

from pebble import common
from pebble.pool.thread import ThreadPool

@overload
def thread(
func: Callable[[common.P], common.T]
) -> Callable[[common.P], Future[common.T]]:
def thread(func: common.CallableType) -> common.ThreadDecoratorReturnType:
...
@overload
def thread(
name: Optional[str] = None,
daemon: bool = True,
pool: Optional[ThreadPool] = None
) -> Callable[[Callable[[common.P], common.T]],
Callable[[common.P], Future[common.T]]]:
) -> common.ThreadDecoratorParamsReturnType:
...
def thread(*args, **kwargs):
"""Runs the decorated function within a concurrent thread,
Expand Down

0 comments on commit 3454fad

Please sign in to comment.