Skip to content

Commit

Permalink
Fix decorator typing issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogiraoserrao committed Dec 13, 2023
1 parent a85edb7 commit 5c91828
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/textual/_work_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@

from functools import partial, wraps
from inspect import iscoroutinefunction
from typing import TYPE_CHECKING, Callable, Coroutine, TypeVar, Union, cast, overload
from typing import (
TYPE_CHECKING,
Any,
Callable,
Coroutine,
TypeVar,
Union,
cast,
overload,
)

from typing_extensions import ParamSpec, TypeAlias

if TYPE_CHECKING:
from .worker import Worker


FactoryParamSpec = ParamSpec("FactoryParamSpec")
DecoratorParamSpec = ParamSpec("DecoratorParamSpec")
ReturnType = TypeVar("ReturnType")

Decorator: TypeAlias = Callable[
[
Union[
Callable[DecoratorParamSpec, ReturnType],
Callable[DecoratorParamSpec, Coroutine[None, None, ReturnType]],
Callable[DecoratorParamSpec, Coroutine[Any, Any, ReturnType]],
]
],
Callable[DecoratorParamSpec, "Worker[ReturnType]"],
Expand All @@ -36,29 +44,29 @@ class WorkerDeclarationError(Exception):

@overload
def work(
method: Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]],
method: Callable[DecoratorParamSpec, Coroutine[Any, Any, ReturnType]],
*,
name: str = "",
group: str = "default",
exit_on_error: bool = True,
exclusive: bool = False,
description: str | None = None,
thread: bool = False,
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]:
) -> Callable[DecoratorParamSpec, Worker[ReturnType]]:
...


@overload
def work(
method: Callable[FactoryParamSpec, ReturnType],
method: Callable[DecoratorParamSpec, ReturnType],
*,
name: str = "",
group: str = "default",
exit_on_error: bool = True,
exclusive: bool = False,
description: str | None = None,
thread: bool = False,
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]:
) -> Callable[DecoratorParamSpec, Worker[ReturnType]]:
...


Expand All @@ -71,13 +79,13 @@ def work(
exclusive: bool = False,
description: str | None = None,
thread: bool = False,
) -> Decorator[..., ReturnType]:
) -> Decorator[DecoratorParamSpec, ReturnType]:
...


def work(
method: Callable[FactoryParamSpec, ReturnType]
| Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]]
method: Callable[DecoratorParamSpec, ReturnType]
| Callable[DecoratorParamSpec, Coroutine[Any, Any, ReturnType]]
| None = None,
*,
name: str = "",
Expand All @@ -86,7 +94,10 @@ def work(
exclusive: bool = False,
description: str | None = None,
thread: bool = False,
) -> Callable[FactoryParamSpec, Worker[ReturnType]] | Decorator:
) -> (
Callable[DecoratorParamSpec, Worker[ReturnType]]
| Decorator[DecoratorParamSpec, ReturnType]
):
"""A decorator used to create [workers](/guide/workers).
Args:
Expand All @@ -104,7 +115,7 @@ def work(
def decorator(
method: (
Callable[DecoratorParamSpec, ReturnType]
| Callable[DecoratorParamSpec, Coroutine[None, None, ReturnType]]
| Callable[DecoratorParamSpec, Coroutine[Any, Any, ReturnType]]
)
) -> Callable[DecoratorParamSpec, Worker[ReturnType]]:
"""The decorator."""
Expand Down

0 comments on commit 5c91828

Please sign in to comment.