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

Fix decorator typing issue. #3862

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/textual/_work_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,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]],
Comment on lines -25 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coroutines like async def foo() -> T: ... have their type inferred as Coroutine[Any, Any, T], so I swapped all of the Coroutine[None, None, T] for Coroutine[Any, Any, T] under the assumption that the usage of None was a mistake.

]
],
Callable[DecoratorParamSpec, "Worker[ReturnType]"],
Expand All @@ -35,7 +43,7 @@ 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",
Expand All @@ -48,7 +56,7 @@ def work(

@overload
def work(
method: Callable[FactoryParamSpec, ReturnType],
method: Callable[DecoratorParamSpec, ReturnType],
*,
name: str = "",
group: str = "default",
Expand All @@ -68,13 +76,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]]
Callable[DecoratorParamSpec, ReturnType]
| Callable[DecoratorParamSpec, Coroutine[Any, Any, ReturnType]]
| None
) = None,
*,
Expand All @@ -84,7 +92,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 @@ -102,7 +113,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
Loading