-
Notifications
You must be signed in to change notification settings - Fork 6.7k
[core] Add static type hints for Actor methods #54173
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
[core] Add static type hints for Actor methods #54173
Conversation
There was a problem hiding this 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 andRemoteMethodNoArgs
for precise.remote()
/.bind()
typing. - Imported
ParamSpec
,Concatenate
, andoverload
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 = (
python/ray/actor.py
Outdated
_T9 = TypeVar("_T9") | ||
|
||
|
||
class RemoteMethodNoArgs(Generic[_Ret]): |
There was a problem hiding this comment.
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.
8d34d4e
to
9bc77dd
Compare
python/ray/actor.py
Outdated
) | ||
from typing_extensions import ParamSpec |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in fbfc893
python/ray/actor.py
Outdated
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in becd6de
There was a problem hiding this 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>
9bc77dd
to
becd6de
Compare
Thanks! I will add the actor constructor to the list of issues to fix |
<!-- 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**  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>
<!-- 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**  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>
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

cc @pcmoritz @richardliaw
Related issue number
Checks
git commit -s
) in this PR.scripts/format.sh
to lint the changes in this PR.method in Tune, I've added it in
doc/source/tune/api/
under thecorresponding
.rst
file.