|
1 | 1 | import inspect |
2 | 2 | import logging |
3 | | -from typing import Any, Dict, List, Literal, Optional, Tuple, Union, TYPE_CHECKING |
| 3 | +from typing import ( |
| 4 | + Any, |
| 5 | + Dict, |
| 6 | + List, |
| 7 | + Literal, |
| 8 | + Optional, |
| 9 | + Tuple, |
| 10 | + Union, |
| 11 | + TYPE_CHECKING, |
| 12 | + TypeVar, |
| 13 | + Generic, |
| 14 | +) |
4 | 15 |
|
5 | 16 | import ray._private.ray_constants as ray_constants |
6 | 17 | import ray._private.signature as signature |
|
51 | 62 | # Hook to call with (actor, resources, strategy) on each local actor creation. |
52 | 63 | _actor_launch_hook = None |
53 | 64 |
|
| 65 | +# TypeVar for generic ActorHandle |
| 66 | +T = TypeVar("T") |
| 67 | + |
| 68 | +# return type of ActorClass[T].remote() |
| 69 | +ActorProxy = Union["ActorHandle[T]", type[T]] |
| 70 | + |
54 | 71 |
|
55 | 72 | @PublicAPI |
56 | 73 | @client_mode_hook |
@@ -761,7 +778,7 @@ def _process_option_dict(actor_options): |
761 | 778 |
|
762 | 779 |
|
763 | 780 | @PublicAPI |
764 | | -class ActorClass: |
| 781 | +class ActorClass(Generic[T]): |
765 | 782 | """An actor class. |
766 | 783 |
|
767 | 784 | This is a decorated class. It can be used to create actors. |
@@ -901,7 +918,7 @@ def _ray_from_function_descriptor( |
901 | 918 | self._default_options["runtime_env"] = self.__ray_metadata__.runtime_env |
902 | 919 | return self |
903 | 920 |
|
904 | | - def remote(self, *args, **kwargs): |
| 921 | + def remote(self, *args, **kwargs) -> ActorProxy[T]: |
905 | 922 | """Create an actor. |
906 | 923 |
|
907 | 924 | Args: |
@@ -1052,7 +1069,7 @@ class or functions. |
1052 | 1069 |
|
1053 | 1070 | @wrap_auto_init |
1054 | 1071 | @_tracing_actor_creation |
1055 | | - def _remote(self, args=None, kwargs=None, **actor_options): |
| 1072 | + def _remote(self, args=None, kwargs=None, **actor_options) -> ActorProxy[T]: |
1056 | 1073 | """Create an actor. |
1057 | 1074 |
|
1058 | 1075 | This method allows more flexibility than the remote method because |
@@ -1434,7 +1451,7 @@ class or functions. |
1434 | 1451 |
|
1435 | 1452 |
|
1436 | 1453 | @PublicAPI |
1437 | | -class ActorHandle: |
| 1454 | +class ActorHandle(Generic[T]): |
1438 | 1455 | """A handle to an actor. |
1439 | 1456 |
|
1440 | 1457 | The fields in this class are prefixed with _ray_ to hide them from the user |
|
0 commit comments