Repository navigation
Description
What happened + What you expected to happen
Description
Ray Serve's controller code is fully type-annotated but never type-checked. The
mypy pre-commit hook covers an allowlist of exactly 7 files repo-wide — from
all of Serve, only python/ray/serve/handle.py and a handle-typing fixture:
# .pre-commit-config.yaml
- id: mypy
args: ['--follow-imports=skip', '--ignore-missing-imports']
files: |
(?x)^(
python/ray/autoscaler/node_provider.py|
...
python/ray/serve/handle.py|
python/ray/serve/tests/typing_files/check_handle_typing.py
)
Additionally, --follow-imports=skip degrades every cross-module type to
Any, so even allowlisted files get almost no protection from wrong-type
arguments: any parameter annotated with an imported class effectively becomes
untyped.
Motivating case: a production bug mypy flags exactly
The rank-corruption bug behind #63862 (fixed in ) was a one-liner in
deployment_state.py — the lightweight-reconfigure path passed
rank=current_rank.rank (a bare int) where the callee declares
rank: ReplicaRank. The int round-tripped through the replica and broke rank
recovery on every controller restart, permanently corrupting the rank system
("Rank system is in an invalid state" loop).
Running mypy 1.7.0 (the version already pinned in pre-commit) with import
following enabled flags the bug precisely:
deployment_state.py:3749: error: Argument "rank" to "reconfigure" of
"DeploymentReplica" has incompatible type "int"; expected "ReplicaRank"
[arg-type]
With Ray's current flags (--follow-imports=skip), the same run reports 57
unrelated errors in the file and does NOT flag this line — ReplicaRank
collapses to Any. So this isn't just "add the file to the list"; the flags
matter too.
The same run surfaces more latent inconsistencies in the rank system alone:
deployment_state.py: error: Argument "assign_rank_callback" to "start" of
"DeploymentReplica" has incompatible type "Callable[[str, str], ReplicaRank]";
expected "Callable[[ReplicaID], ReplicaRank]" [arg-type] (2 call sites)
deployment_state.py: error: Argument 3 to "recover_rank" of
"DeploymentRankManager" has incompatible type "ReplicaRank | None";
expected "ReplicaRank" [arg-type]
This bug class — a raw value passed where a wrapper object is expected, then
serialized across the controller/replica RPC boundary and only exploding much
later during recovery — is essentially invisible to unit tests that mock the
boundary, and exactly what static typing is for.
Current scope of the debt (measured on master)
mypy --follow-imports=silent --ignore-missing-imports ray/serve/_private/:
- 1028 errors in 65 of 86 files
- 237 of them are
[arg-type](the category that catches bugs like the above)
Proposal (incremental, no big-bang)
- Add a Serve-scoped mypy CI step (or extend the pre-commit hook) with
--follow-imports=silentso imported types resolve, scoped initially to an
error-free subset ofray/serve/_private/(per-file allowlist, same
mechanism as today). - Burn down
[arg-type]errors first in controller-critical modules
(deployment_state.py,replica.py,controller.py,
application_state.py) — these guard the state-checkpoint/recovery paths
where type confusion becomes persistent state corruption. - Ratchet: once a file is clean, add it to the allowlist so it can't regress.
Use case
Prevent recurrence of 64181-style state-corruption bugs in Serve's
controller, where type annotations already encode the contract but nothing
enforces it.
Versions / Dependencies
ray[serve] 2.56.0
Reproduction script
mypy --follow-imports=silent --ignore-missing-imports ray/serve/_private/
Issue Severity
None
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
[serve] Enforce mypy and pyrefly type checking on clean Serve files
[serve] Enforce mypy and pyrefly type checking on clean Serve files
[serve] Enforce mypy and pyrefly type checking on clean Serve files (#…
[serve] Enforce type checking on the controller-critical modules
[serve] Fix type regression in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S
[serve] Fix type regression in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S
[serve] Fix type regression in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S
[serve] Fix type regression in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S (#…
[serve] Enforce type checking on the controller-critical modules
[serve] Enforce type checking on the controller-critical modules
[serve] Enforce type checking on the controller-critical modules
[serve] Enforce type checking on the controller-critical modules (#64753
johntaylor-cell commentedon Aug 10, 2026
#64662
#64753
#65332
#65410
[serve] Enforce type checking on the controller-critical modules (ray…
[serve] Enforce mypy and pyrefly type checking on clean Serve files (r…
[serve] Fix type regression in RAY_SERVE_HAPROXY_CLOSE_SPREAD_TIME_S (r…
[serve] Enforce type checking on the controller-critical modules (ray…
[serve] Enforce type checking on the controller-critical modules (ray…