Skip to content

Commit dc6cd2c

Browse files
committed
Use ParamSpec to improve typing for actors
Previously, the actor decorator was returning `typing.Any` objects. This commit introduces a `ParamSpec` and a `TypeVar`, to improve typing of actor params, and returned type. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 9a83ded commit dc6cd2c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/frequenz/sdk/actor/_decorator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import asyncio
1313
import inspect
1414
import logging
15-
from typing import Any, Optional, Type, TypeVar
15+
from typing import Any, Callable, Optional, Type, TypeVar
16+
17+
from typing_extensions import ParamSpec
1618

1719
from frequenz.sdk._internal._asyncio import cancel_and_await
1820

@@ -55,7 +57,11 @@ class BaseActor:
5557
restart_limit: Optional[int] = None
5658

5759

58-
def actor(cls: Type[Any]) -> Type[Any]:
60+
_P = ParamSpec("_P")
61+
_R = TypeVar("_R")
62+
63+
64+
def actor(cls: Callable[_P, _R]) -> Type[_R]:
5965
"""Decorate a class into a simple composable actor.
6066
6167
A actor using the `actor` decorator should define an `async def run(self)`
@@ -181,7 +187,7 @@ async def run(self) -> None:
181187
class ActorClass(cls, BaseActor): # type: ignore
182188
"""A wrapper class to make an actor."""
183189

184-
def __init__(self, *args: Any, **kwargs: Any) -> None:
190+
def __init__(self, *args: _P.args, **kwargs: _P.kwargs) -> None:
185191
"""Create an `ActorClass` instance.
186192
187193
Also call __init__ on `cls`.

0 commit comments

Comments
 (0)