Repository navigation
[serve] Enforce mypy and pyrefly type checking on clean Serve files - #64662
Conversation
There was a problem hiding this comment.
Code Review
This pull request configures mypy and pyrefly type checking for the Ray Serve codebase, adding pre-commit hooks, linting scripts, and type annotations across multiple files. The reviewer suggests replacing several type-ignore comments with runtime assertions or type casts to improve type safety and code cleanliness. Additionally, the reviewer recommends avoiding generic parameter reassignment in build_app.py and removing a dead, broken close_queue method in direct_ingress_http_util.py that calls a non-existent close() method on asyncio.Queue.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit a07cad7. Configure here.
94c8c07 to
242df7d
Compare
Serve is fully type-annotated but nothing enforces the annotations: the pre-commit mypy hook covers 7 files repo-wide with --follow-imports=skip, which collapses every cross-module type to Any and misses wrong-type arguments entirely. This implements the ratchet proposed in ray-project#64643 and adds pyrefly as a second checker over the same files (the two checkers resolve modules and narrow types differently, so they catch disjoint bug classes). - Fix ~330 type errors across 40 files with type-only changes: Optional defaults, wrong or missing annotations, casts, @overload signatures for the constants_utils get_env_* helpers (constants with non-None defaults are no longer typed Optional), a callback Protocol for CreateRouterCallable (its call site uses keyword arguments, which bare Callable types do not permit), a precise return type for serve.context._get_serve_request_context, and narrowly-scoped ignore comments where a checker cannot see a runtime invariant (actor .remote dynamics, lazily-populated attributes, import-fallback sentinels). No runtime behavior changes; the serve unit test suite passes identically to the base commit. - Add a "mypy (ray serve)" pre-commit hook: --follow-imports=silent over a 61-file allowlist covering all of serve/_private that is currently clean, plus handle.py and the handle typing fixture (moved out of the skip-mode hook). Runs in CI via the existing pre_commit lint step (hook id mypy). Once a file is clean, add it to the list so it cannot regress. - Add a "pyrefly (ray serve)" pre-commit hook (pyrefly==1.1.1) over the same allowlist and wire it into the CI lint HOOKS list. pyrefly.toml maps --ignore-missing-imports semantics via replace-imports-with-any for generated protobuf modules and third-party deps not installed in the hook env. - pyproject.toml: per-module mypy override (follow_imports=skip) for ray.serve.generated.* / ray.core.generated.* so results are identical whether or not generated protobuf files exist in the checkout. Remaining debt for follow-ups: ~630 mypy errors in 27 files, largest deployment_state.py, replica.py, proxy.py, deployment_scheduler.py, controller.py, application_state.py. Related to ray-project#64643. Signed-off-by: john.taylor <[email protected]>
Apply the review suggestions: replace invariant suppression comments with assert-based narrowing (documents the invariant and checks it at runtime), use a typed local in IDDict instead of reassigning the generic key parameter, cast the same-loop shutdown future instead of suppressing, and delete the dead close_queue method (asyncio.Queue has no close(); no callers). Net -18 suppression comments. Both checkers remain clean over the allowlist and the serve unit suite failure set is still identical to the base commit (the new asserts never fire in tests). Signed-off-by: john.taylor <[email protected]>
The ray/data CI job (ci/lint/pyrefly-check.sh) runs pyrefly with no config file; pyrefly discovers config by walking up from each checked file, so a repo-root pyrefly.toml changes that job\x27s behavior (its premerge shard failed on unrelated ray/data files). Move the config to python/ray/serve/pyrefly.toml — serve files still discover it, data files walk past python/ and find nothing, restoring the data job\x27s no-config defaults. Path-valued keys rewritten relative to the new location. Signed-off-by: john.taylor <[email protected]>
The CI lint container builds the pre-commit hook env with python 3.9; pyrefly derives python-version from the hook interpreter, and its resolution of the vendored ray.cloudpickle star-export and (spuriously) types.FunctionType differs at 3.9, so the hook passed locally but failed in CI. Pin python-version = 3.9 (the ray floor) in pyrefly.toml so every environment checks identically, add ray.cloudpickle.* to replace-imports-with-any (vendored, untyped), and suppress the one pyrefly-typeshed false positive on types.FunctionType. Verified: identical zero-error output from 3.9- and 3.11-built hook envs, mypy unaffected. Signed-off-by: john.taylor <[email protected]>
The repo-root pyrefly.toml belongs to the ray/data CI job (ci/lint/pyrefly-check.sh) and predates this branch; the first commit accidentally overwrote it and a later commit removed it, which is what broke the data pyrefly premerge shard (ray.* imports stop resolving without its search-path). Restore it byte-for-byte from the base commit, and keep the serve-scoped config at python/ray/serve/pyrefly-serve.toml, passed explicitly via --config in the pre-commit hook so the two configs cannot collide (that filename is not auto-discovered). Verified: the data job command now produces identical results on this branch and on the base commit; both serve hooks pass from 3.9- and 3.11-built hook envs; mypy unaffected. Signed-off-by: john.taylor <[email protected]>
08fe21f to
94873ab
Compare
Signed-off-by: john.taylor <[email protected]>
abrarsheikh
left a comment
There was a problem hiding this comment.
@elliot-barn let's plan to remove mypy, hate to have two type checkers, hard to keep them in sync and resolve contradictions.
@johntaylor-cell what is the next plan on this? I see that we are a giant file list with allowlist, this will be out of sync. What is the plan to drop it, let's track that work
The plan is to fix the remaining ~600 errors in subsequent PRs, and remove the whitelist after that. |
The merged mypy/pyrefly enforcement (ray-project#64662) runs on the full allowlist, surfacing type issues that per-commit hooks never checked on these files: - AutoscalingContext.prometheus_metrics now accepts a callable returning Optional, matching the cache getter that returns None when there is no data. - get_prometheus_config_by_deployment narrows the Optional queries and address inline so its return type is satisfied. Also narrows a pre-existing int(os.environ.get(...)) in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S (added in ray-project#63996) that the same enforcement flags on master; needed for the shared mypy hook to pass. Signed-off-by: Seiji Eicher <[email protected]>
…ay-project#64662) Serve is fully type-annotated but nothing enforces the annotations: the pre-commit mypy hook covers 7 files repo-wide with --follow-imports=skip, which collapses every cross-module type to Any and misses wrong-type arguments entirely. This implements the ratchet proposed in ray-project#64643 and adds pyrefly as a second checker over the same files (the two checkers resolve modules and narrow types differently, so they catch disjoint bug classes). - Fix ~330 type errors across 40 files with type-only changes: Optional defaults, wrong or missing annotations, casts, @overload signatures for the constants_utils get_env_* helpers (constants with non-None defaults are no longer typed Optional), a callback Protocol for CreateRouterCallable (its call site uses keyword arguments, which bare Callable types do not permit), a precise return type for serve.context._get_serve_request_context, and narrowly-scoped ignore comments where a checker cannot see a runtime invariant (actor .remote dynamics, lazily-populated attributes, import-fallback sentinels). No runtime behavior changes; the serve unit test suite passes identically to the base commit. - Add a "mypy (ray serve)" pre-commit hook: --follow-imports=silent over a 61-file allowlist covering all of serve/_private that is currently clean, plus handle.py and the handle typing fixture (moved out of the skip-mode hook). Runs in CI via the existing pre_commit lint step (hook id mypy). Once a file is clean, add it to the list so it cannot regress. - Add a "pyrefly (ray serve)" pre-commit hook (pyrefly==1.1.1) over the same allowlist and wire it into the CI lint HOOKS list. pyrefly.toml maps --ignore-missing-imports semantics via replace-imports-with-any for generated protobuf modules and third-party deps not installed in the hook env. - pyproject.toml: per-module mypy override (follow_imports=skip) for ray.serve.generated.* / ray.core.generated.* so results are identical whether or not generated protobuf files exist in the checkout. Remaining debt for follow-ups: ~630 mypy errors in 27 files, largest deployment_state.py, replica.py, proxy.py, deployment_scheduler.py, controller.py, application_state.py. Related to ray-project#64643 --------- Signed-off-by: john.taylor <[email protected]> Signed-off-by: 400Ping <[email protected]>

Serve is fully type-annotated but nothing enforces the annotations: the pre-commit mypy hook covers 7 files repo-wide with --follow-imports=skip, which collapses every cross-module type to Any and misses wrong-type arguments entirely. This implements the ratchet proposed in #64643 and adds pyrefly as a second checker over the same files (the two checkers resolve modules and narrow types differently, so they catch disjoint bug classes).
Fix ~330 type errors across 40 files with type-only changes: Optional defaults, wrong or missing annotations, casts, @overload signatures for the constants_utils get_env_* helpers (constants with non-None defaults are no longer typed Optional), a callback Protocol for CreateRouterCallable (its call site uses keyword arguments, which bare Callable types do not permit), a precise return type for serve.context._get_serve_request_context, and narrowly-scoped ignore comments where a checker cannot see a runtime invariant (actor .remote dynamics, lazily-populated attributes, import-fallback sentinels). No runtime behavior changes; the serve unit test suite passes identically to the base commit.
Add a "mypy (ray serve)" pre-commit hook: --follow-imports=silent over a 61-file allowlist covering all of serve/_private that is currently clean, plus handle.py and the handle typing fixture (moved out of the skip-mode hook). Runs in CI via the existing pre_commit lint step (hook id mypy). Once a file is clean, add it to the list so it cannot regress.
Add a "pyrefly (ray serve)" pre-commit hook (pyrefly==1.1.1) over the same allowlist and wire it into the CI lint HOOKS list. pyrefly.toml maps --ignore-missing-imports semantics via replace-imports-with-any for generated protobuf modules and third-party deps not installed in the hook env.
pyproject.toml: per-module mypy override (follow_imports=skip) for ray.serve.generated.* / ray.core.generated.* so results are identical whether or not generated protobuf files exist in the checkout.
Remaining debt for follow-ups: ~630 mypy errors in 27 files, largest deployment_state.py, replica.py, proxy.py, deployment_scheduler.py, controller.py, application_state.py.
Related to #64643