Skip to content

[Serve] Type bare @serve.deployment as Deployment - #66513

Merged
akyang-anyscale merged 3 commits into
ray-project:masterfrom
codechrl:serve-deployment-overload-v2
Oct 26, 2026
Merged

akyang-anyscale merged 3 commits into
ray-project:masterfrom
codechrl:serve-deployment-overload-v2

Conversation

@codechrl

Copy link
Copy Markdown
Contributor

Description

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 decorator it would return. Every later .bind() or .options() call on it is a type error:

error: Cannot access attribute "bind" for class "FunctionType"
    Attribute "bind" is unknown (reportFunctionMemberAccess)

This adds 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 #34711. serve.deployment(MyClass, num_replicas=2) works at runtime and now types as Deployment too.

check_deployment_typing.py covers the bare, called and direct-call forms. It sits next to check_handle_typing.py and is registered in the mypy (ray serve) and pyrefly (ray serve) hooks and in pyrefly-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 X still types as type[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.py result is the same command with only api.py reverted to master.

# mypy 1.7.0, flags and file set of the "mypy (ray serve)" hook
MYPYPATH=python mypy --no-site-packages --follow-imports=silent --ignore-missing-imports <69 hook files>
Success: no issues found in 69 source files      # master api.py: 7 errors, all in check_deployment_typing.py

# pyrefly 1.1.1
pyrefly check --config python/ray/serve/pyrefly-serve.toml python/ray/serve/tests/typing_files/check_deployment_typing.py
0 errors                                         # master api.py: 7 errors

# pyright 1.1.406, extraPaths = ["python"]
pyright python/ray/serve/tests/typing_files/check_deployment_typing.py
0 errors                                         # master api.py: 7 errors
pyright <reproduction script from #52483>
0 errors                                         # master api.py: 2 errors

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.

@codechrl
codechrl requested a review from a team as a code owner September 27, 2026 00:46

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue community-contribution Contributed by the community labels Sep 27, 2026

@jeffreywang88 jeffreywang88 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for improving typing for Serve!

Comment thread python/ray/serve/api.py Resolved
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Oct 14, 2026
@johntaylor-cell johntaylor-cell self-assigned this Oct 21, 2026
@johntaylor-cell johntaylor-cell added the go add ONLY when ready to merge, run all tests label Oct 21, 2026
@johntaylor-cell

Copy link
Copy Markdown
Contributor

@codechrl Please rebase and resolve conflicts

@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Oct 22, 2026
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]>
@codechrl
codechrl force-pushed the serve-deployment-overload-v2 branch from 4149646 to b042cf9 Compare October 22, 2026 03:31
@codechrl

Copy link
Copy Markdown
Contributor Author

@codechrl Please rebase and resolve conflicts

done

@johntaylor-cell

Copy link
Copy Markdown
Contributor

@codechrl Thank you for rebasing. I have one more request. Please add a test that keeps _DeploymentOptions in sync with deployment(), eg, in TestDeploymentOptions, asserting typing.get_type_hints(_DeploymentOptions) equals typing.get_type_hints(deployment) minus _func_or_class and return. Nothing checks the two today, so an option later added only to deployment() would pass CI but be rejected by users' type checkers.

And a nit, the run command in the check_deployment_typing.py docstring also passes api.py, which has 12 unrelated mypy errors, so it always fails. Dropping api.py fixes it.

@johntaylor-cell

Copy link
Copy Markdown
Contributor

And if interested, please create a follow-on PR that that typesserve.multiplexed

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]>
@codechrl

Copy link
Copy Markdown
Contributor Author

Added the sync test in 705c05b and dropped api.py from the mypy command.

@akyang-anyscale
akyang-anyscale merged commit c28b281 into ray-project:master Oct 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[serve] Cannot access attribute "bind" for class "function"

4 participants