Skip to content

Conversation

SolitaryThinker
Copy link
Contributor

Why are these changes needed?

Related Issue: #54149

Also allow for actors methods to be decorated with @ray.method (with no args)

After this PR
image

cc @pcmoritz @richardliaw

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@Copilot Copilot AI review requested due to automatic review settings June 27, 2025 10:47
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Enhance the @ray.method decorator with static type hints for actor methods and allow using it without parentheses.

  • Added RemoteMethod0–9 generics and RemoteMethodNoArgs for precise .remote()/.bind() typing.
  • Imported ParamSpec, Concatenate, and overload to support decorator overloads.
  • Updated @ray.method to handle no-argument usage (@ray.method) and refined validation logic.
Comments suppressed due to low confidence (2)

python/ray/actor.py:473

  • Consider adding unit tests to verify the new no-parentheses usage of @ray.method (@ray.method) so this branch is covered and validated.
    if len(args) == 1 and callable(args[0]) and len(kwargs) == 0:

python/ray/actor.py:478

  • [nitpick] The error message is a bit awkward; consider rephrasing to clearly separate the two valid usages, for example: "The @ray.method decorator must be applied as @ray.method or with one or more of the valid keyword args: ...".
    error_string = (

_T9 = TypeVar("_T9")


class RemoteMethodNoArgs(Generic[_Ret]):
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

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

[nitpick] The series of repetitive RemoteMethod0 through RemoteMethod9 classes could be consolidated using Python 3.11’s variadic generics (PEP 646) or a helper factory to reduce duplication and improve maintainability.

Copilot uses AI. Check for mistakes.

@SolitaryThinker SolitaryThinker force-pushed the will/remote_methods branch 3 times, most recently from 8d34d4e to 9bc77dd Compare June 30, 2025 22:57
@richardliaw richardliaw added the go add ONLY when ready to merge, run all tests label Jul 1, 2025
)
from typing_extensions import ParamSpec
Copy link
Contributor

Choose a reason for hiding this comment

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

Ray Core needs to work without typing_extensions. I realize these have only been added in Python 3.10, so how about we first try to import ParamSpec and Concatenate from typing, and if that doesn't succeed, import them from typing_extensions? It means this won't work for Python 3.9 unless people have typing_extensions installed but I feel like that's an acceptable tradeoff :)

(the Python 3.13 unit test is actually failing at the moment because of this)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in fbfc893

retry_exceptions: Optional[Union[bool, list, tuple]] = None,
_generator_backpressure_num_objects: Optional[int] = None,
enable_task_events: Optional[bool] = None,
tensor_transport: Optional[str] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this Optional[TensorTransportEnum] instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in becd6de

@pcmoritz
Copy link
Contributor

pcmoritz commented Jul 1, 2025

One thing I noticed is that the actor constructor currently doesn't autocomplete, e.g.

import ray

@ray.remote
class Actor:
    def __init__(self, a: str):
        pass

    @ray.method
    def f(self, b: int) -> int:
        return b
    
Actor.remote(<CURSOR>)

It looks like this:

Screenshot 2025-07-01 at 1 42 31 PM

Can you have a look at the in a follow up PR? It should show the arguments of the constructor :)

Copy link
Contributor

@pcmoritz pcmoritz left a comment

Choose a reason for hiding this comment

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

Thanks for adding this, it looks great to me! Left some smaller comments.

Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
@SolitaryThinker
Copy link
Contributor Author

Thanks! I will add the actor constructor to the list of issues to fix

@pcmoritz pcmoritz merged commit 5bcb4a1 into ray-project:master Jul 1, 2025
5 checks passed
elliot-barn pushed a commit that referenced this pull request Jul 2, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it,
we will shortly find a reviewer and assign them to your PR. -->

## Why are these changes needed?
Related Issue: #54149

Also allow for actors methods to be decorated with `@ray.method` (with
no args)

**After this PR**

![image](https://github.com/user-attachments/assets/822b91fa-30af-41c0-be85-d680f02ad6b4)

cc @pcmoritz @richardliaw

<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issue number

<!-- For example: "Closes #1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
@SolitaryThinker SolitaryThinker deleted the will/remote_methods branch July 2, 2025 02:51
elliot-barn pushed a commit that referenced this pull request Jul 7, 2025
<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it,
we will shortly find a reviewer and assign them to your PR. -->

## Why are these changes needed?
Related Issue: #54149

Also allow for actors methods to be decorated with `@ray.method` (with
no args)

**After this PR**

![image](https://github.com/user-attachments/assets/822b91fa-30af-41c0-be85-d680f02ad6b4)

cc @pcmoritz @richardliaw 

<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issue number

<!-- For example: "Closes #1234" -->

## Checks

- [ ] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [ ] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [ ] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants