Repository navigation
[Serve] Type bare @serve.deployment as Deployment - #66513
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces typing overloads for the @serve.deployment decorator in python/ray/serve/api.py to correctly type-check both bare and called decorator usages. It also adds a new type-checking test file check_deployment_typing.py to verify these signatures with mypy, and updates the pre-commit and pyrefly configurations to include this new test file. There are no review comments, and I have no feedback to provide.
jeffreywang88
left a comment
There was a problem hiding this comment.
Thanks for improving typing for Serve!
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
|
@codechrl Please rebase and resolve conflicts |
serve.deployment dispatches on whether _func_or_class is passed. Its only return annotation is Callable[[Callable], Deployment]. A bare @serve.deployment therefore types as the returned decorator. Type checkers then reject every later .bind() or .options() call on it. Add two @overload signatures. A callable first argument returns Deployment, with or without options. No callable returns the decorator. The first overload also accepts the options, unlike the pair removed in types as Deployment too. mypy (python/mypy#3135) and pyrefly ignore the return type of a class decorator. For them the change covers functions and direct calls. pyright applies class decorators, so the reproduction in ray-project#52483 now type checks without errors. Add check_deployment_typing.py next to check_handle_typing.py. List it in the "mypy (ray serve)" and "pyrefly (ray serve)" hooks and in pyrefly-serve.toml. Fixes ray-project#52483 Signed-off-by: codechrl <[email protected]>
The two serve.deployment overloads each repeated the full list of keyword options. Collect the options in a total=False TypedDict and take them as **options: Unpack[_DeploymentOptions] in both overloads. The TypedDict reuses the Default[...] annotations of the implementation signature, so the accepted option types do not change. The options are keyword-only in the overloads and the first parameter is positional only. The implementation signature is not touched. The class is private because check_api_annotations.py requires a stability annotation on public classes reachable from ray.serve. Signed-off-by: codechrl <[email protected]>
4149646 to
b042cf9
Compare
done |
|
@codechrl Thank you for rebasing. I have one more request. Please add a test that keeps And a nit, the run command in the |
|
And if interested, please create a follow-on PR that that types |
Assert that the TypedDict's type hints equal those of deployment() minus _func_or_class and return, so an option added only to the signature fails CI instead of being rejected by type checkers. Drop api.py from the mypy command in check_deployment_typing.py. It carries unrelated mypy errors, so the documented command always fails. Signed-off-by: codechrl <[email protected]>
|
Added the sync test in 705c05b and dropped api.py from the mypy command. |
Description
serve.deploymentdispatches on whether_func_or_classis passed. Its only return annotation isCallable[[Callable], Deployment]. A bare@serve.deploymenttherefore types as the decorator it would return. Every later.bind()or.options()call on it is a type error:This adds two
@overloadsignatures. A callable first argument returnsDeployment, with or without options. No callable returns the decorator. The first overload also accepts the options, unlike the pair removed in #34711.serve.deployment(MyClass, num_replicas=2)works at runtime and now types asDeploymenttoo.check_deployment_typing.pycovers the bare, called and direct-call forms. It sits next tocheck_handle_typing.pyand is registered in themypy (ray serve)andpyrefly (ray serve)hooks and inpyrefly-serve.toml.Related issues
Fixes #52483
Additional information
mypy (python/mypy#3135) and pyrefly ignore the return type of a class decorator. For them this fixes functions and direct calls.
@serve.deployment class Xstill types astype[X]there. pyright applies class decorators, so the reproduction script in #52483 now reports 0 errors.Not a duplicate: #52483 has no linked PR.
gh pr list --repo ray-project/ray --state open --search "52483 in:body"returns nothing.Tests run. Each
master api.pyresult is the same command with onlyapi.pyreverted to master.Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.