Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions doc/source/ray-core/actors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,42 @@ If you instantiate an actor, you can pass the handle around to various tasks.
}


Type hints and static typing for actors
---------------------------------------

Ray supports Python type hints for both remote functions and actors, enabling better IDE support and static type checking. To get the best type inference and pass type checkers when working with actors, follow these patterns:

- **Prefer ``ray.remote(MyClass)`` over ``@ray.remote`` for actors**: Instead of decorating your class with ``@ray.remote``, use ``ActorClass = ray.remote(MyClass)``. This preserves the original class type and allows type checkers and IDEs to infer the correct types.

- **Use ``@ray.method`` for actor methods**: Decorate actor methods with ``@ray.method`` to enable type hints for remote method calls on actor handles.

- **Use the ``ActorClass`` and ``ActorProxy`` types**: When you instantiate an actor, annotate the handle as ``ActorProxy[MyClass]`` to get type hints for remote methods.

**Example:**

.. testcode::

import ray
from ray.actor import ActorClass, ActorProxy

class Counter:
def __init__(self):
self.value = 0

@ray.method
def increment(self) -> int:
self.value += 1
return self.value

CounterActor: ActorClass[Counter] = ray.remote(Counter)
counter: ActorProxy[Counter] = CounterActor.remote()

# Type checkers and IDEs will now provide type hints for remote methods
obj_ref: ray.ObjectRef[int] = counter.increment.remote()
print(ray.get(obj_ref))

Comment on lines +415 to +416
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To make this example more complete and verifiable, it's a good practice to include the expected output using a .. testoutput:: block, similar to other examples in this file. This ensures the example code produces the correct result when the documentation is tested.

The output of print(ray.get(obj_ref)) will be 1.

Suggested change
print(ray.get(obj_ref))
print(ray.get(obj_ref))
.. testoutput::
1

For more details and advanced patterns, see :ref:`Type hints in Ray <core-type-hint>`.


Generators
----------
Expand Down
2 changes: 2 additions & 0 deletions doc/source/ray-core/type-hint.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(core-type-hint)=

# Type hints in Ray

As of Ray 2.48, Ray provides comprehensive support for Python type hints with both remote functions and actors. This enables better IDE support, static type checking, and improved code maintainability in distributed Ray applications.
Expand Down