Skip to content

[ci/doc] Green the API-doc consistency check across all libraries - #64808

Merged
edoakes merged 3 commits into
ray-project:masterfrom
dstrodtman:djs-260716-api-check-green-all-libs
Jul 16, 2026
Merged

edoakes merged 3 commits into
ray-project:masterfrom
dstrodtman:djs-260716-api-check-green-all-libs

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Why

The API-doc consistency check (ci/ray_ci/doc) gained reverse-resolution and dedup policies, but it runs only on premerge PRs, never postmerge, so master drifted red undetected: any PR that touches a file mapping to this job now fails on pre-existing discrepancies across data, serve, train, tune, and rllib. This greens the check for all libraries on the existing make -C doc/ html path, so those PRs stop tripping over debt they didn't introduce.

This is deliberately the whitelist + mock half. Decoupling stub generation from the full render is a separate change that builds on this.

What

Mock uninstalled backends

The check imports documented names directly (resolve() / Module.get_apis()), bypassing Sphinx. Optional-dependency modules — ray.data.llm, ray.serve.llm, ray.train.lightning / xgboost / lightgbm, ray.tune.integration.* — eagerly import vLLM / transformers / torch / pytorch_lightning, which are absent on the CPU docbuild runner, so they read as unresolved even though the render shows them fine (it mocks the same libs via autodoc_mock_imports).

A new side-effect-free doc/source/api_mock_imports.py holds the mock list as the single source of truth, imported by both conf.py and the check, split into THIRD_PARTY_MOCK_MODULES (safe for the check) and BUILD_ONLY_MOCK_MODULES (ray.* compiled/generated, render-only). The check applies the third-party mocks via sphinx.ext.autodoc.mock.mock(). ray.* is never mocked, so the resolve/dedup policy keeps its teeth on Ray's own symbols. This alone clears all serve and train failures and the optional-dep tail of data and tune.

Whitelist the rest by intent

  • intentional_duplicate_apis — canonical names cross-listed on purpose: data Dataset.to_* / write_* (class stub + saving-data page), tune AsyncHyperBandScheduler, rllib AlgorithmConfig.build_learner* / Checkpointable.save_to_path etc.
  • doc_only_whitelist (data) — public APIs re-exported from _internal (ActorPoolStrategy, ExecutionOptions, …) that resolve fine but trip the private-name heuristic. Exempted from the resolve check only.
  • tracked_doc_debt — genuine debt owed a document-or-deprecate decision: data's deprecated + private-named accessors, tune's Stopper.__call__, and RLlib's larger surface (unresolved/malformed autosummary entries and documented private/dunder members). RLlib's entries are parked to unblock CI; each still wants a real fix from the RLlib team (fix the doc entry, deprecate, or de-annotate) and is commented as such.

Per-team result: serve/train clear via mock alone; data/tune via mock + a few entries; rllib via whitelisting (it imports fine, so the mock doesn't apply); core was already clean.

Verification

  • pre-commit run --files passes on all changed files (ruff, black, Ray import-order, Ray docstyle, semgrep, not-real-mock-methods).
  • py_compile on the changed modules; TEAM_API_CONFIGS inspected via AST for the expected per-team counts.
  • The in-image python ci/ray_ci/doc/cmd_check_api_discrepancy.py <checkout> ALL run is the decisive gate — it runs as this PR's doc: check API doc consistency premerge job. One thing that run confirms and local static checks can't: that the mocked optional-dep modules resolve to public objects (not deprecated/private canonicals).

Not a duplicate

Part of the API-ref consistency workstream; no competing PR. The whitelist-split change it builds on already merged. No open PR greens the check for these teams.

AI assistance

AI assistance was used to author these changes; every changed line was reviewed by the submitter.

The reverse-resolution and dedup policies added to the API-doc consistency
check surfaced pre-existing discrepancies across every library, but the check
runs only on premerge PRs (never postmerge), so master drifted red undetected:
any PR that triggers the check now fails on data/serve/train/tune/rllib.

This greens the check on the existing `make -C doc/ html` path (no decouple),
without weakening it:

- Mock uninstalled backends. The check imports documented names directly
  (resolve() / Module.get_apis()), bypassing Sphinx, so optional-dependency
  modules (ray.data.llm, ray.serve.llm, ray.train.lightning/xgboost/lightgbm,
  ray.tune.integration.*) that import vLLM/transformers/torch/pytorch_lightning
  read as unresolved even though the render shows them fine. A new
  side-effect-free doc/source/api_mock_imports.py holds the mock list as the
  single source of truth (conf.py and the check both import it), split into
  THIRD_PARTY_MOCK_MODULES (safe for the check) and BUILD_ONLY_MOCK_MODULES
  (ray.* compiled/generated, render-only). The check applies the third-party
  mocks via sphinx.ext.autodoc.mock.mock(); ray.* is never mocked, so the
  resolve/dedup policy keeps its teeth on Ray's own symbols. This alone clears
  all serve and train failures and the optional-dep tail of data and tune.

- Whitelist the remaining discrepancies by intent:
  - intentional_duplicate_apis for cross-listed methods (data Dataset.to_*/
    write_*, tune AsyncHyperBandScheduler, rllib AlgorithmConfig.build_learner*
    / Checkpointable.save_to_path etc.).
  - doc_only_whitelist for public data APIs re-exported from _internal
    (ActorPoolStrategy, ExecutionOptions, ...), which resolve fine but trip the
    private-name heuristic.
  - tracked_doc_debt for genuine debt owed a document-or-deprecate decision:
    data's deprecated + private-named accessors, tune's Stopper.__call__, and
    RLlib's large surface (unresolved/malformed autosummary entries and
    documented private/dunder members). RLlib's entries are parked to unblock
    CI; each still wants a real fix from the RLlib team.

Scope: this is the whitelisting/mock half only. Decoupling stub generation from
the full render (api_autogen.py, lint.sh) is a separate change that builds on
this.

Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman requested review from a team as code owners July 16, 2026 14:12

@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 centralizes the list of mocked third-party modules for API documentation generation into a new file, doc/source/api_mock_imports.py, and updates both conf.py and the API discrepancy check script to use this single source of truth. It also updates various tracked documentation debts and duplicate APIs for ray.data, ray.tune, and RLlib. Feedback on the changes suggests cleaning up sys.modules after dynamically importing api_mock_imports in the discrepancy check script to prevent caching issues and potential test pollution.

Comment on lines +374 to +379
doc_source = os.path.join(ray_checkout_dir, "doc", "source")
sys.path.insert(0, doc_source)
try:
from api_mock_imports import THIRD_PARTY_MOCK_MODULES
finally:
sys.path.remove(doc_source)

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.

medium

When dynamically modifying sys.path to import a local module like api_mock_imports, the imported module gets cached in sys.modules. If this function is executed multiple times (for example, in a unit test suite or when checking multiple directories in the same process), subsequent calls will reuse the cached module from the first path instead of loading the correct one from the new ray_checkout_dir.

To prevent sys.modules pollution and ensure test robustness, we should use os.path.abspath for the path, import the module, and then clean it up from sys.modules in the finally block.

Suggested change
doc_source = os.path.join(ray_checkout_dir, "doc", "source")
sys.path.insert(0, doc_source)
try:
from api_mock_imports import THIRD_PARTY_MOCK_MODULES
finally:
sys.path.remove(doc_source)
doc_source = os.path.abspath(os.path.join(ray_checkout_dir, "doc", "source"))
sys.path.insert(0, doc_source)
try:
import api_mock_imports
THIRD_PARTY_MOCK_MODULES = api_mock_imports.THIRD_PARTY_MOCK_MODULES
finally:
sys.path.remove(doc_source)
sys.modules.pop("api_mock_imports", None)

@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Jul 16, 2026
dstrodtman and others added 2 commits July 16, 2026 11:27
The API-doc mock wrapped the check (and, on the decouple branch, the stub
generator) in the full autodoc_mock_imports list. That list mocks installed
libraries too (pandas, tensorflow, ...), which Sphinx autodoc tolerates but a
raw importlib / autosummary path does not: shadowing installed pandas makes
`import ray.data` fail, so stub generation aborted with
"[api_autogen] ERROR: no module named ray.data" and the check would have
mass-flagged every data entry as unresolved.

Add api_mock_imports.absent_mock_modules(), which keeps only the entries whose
find_spec is None (the genuinely-absent optional backends: vllm, sglang,
pytorch_lightning, ...), and apply it at the raw-import sites. That's all that's
needed to make optional-dependency modules importable, without shadowing
installed libraries. conf.py still mocks the full list for the render. ray.* is
never mocked.

Signed-off-by: Douglas Strodtman <[email protected]>
…its checkout

The full make-html consistency run surfaced one non-public data entry:
ray.data.llm.Processor. It's a @publicapi(stability="beta") class re-exported
through the public ray.data.llm module and documented in data/api/llm.rst, but
its canonical name resolves under ray.llm._internal, so the resolve check's
private-name heuristic flags it. Add it to the data doc_only_whitelist
alongside the existing _internal re-exports.

Also address a review note on _mock_uninstalled_backends: importing the
checkout-local, unqualified api_mock_imports left a copy in sys.modules that a
later invocation with a different checkout would reuse even after doc/source
leaves sys.path. Evict it in finally, capture the mock list inside the try, and
use an absolute doc/source path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@edoakes
edoakes merged commit 5183eb9 into ray-project:master Jul 16, 2026
3 checks passed
elliot-barn pushed a commit that referenced this pull request Jul 17, 2026
…4423)

## Why

The `doc: check API doc consistency` premerge step reads autosummary
stub `.rst` files — the per-class method tables that the hand-written
API pages `.. include::`. Those stubs are produced only as a side effect
of `make -C doc/ html`, so **every Python-touching premerge PR pays for
a full Sphinx render just to generate them**. Separately, the generation
was wrapped in a `try/except` that downgraded any failure to a warning,
so a broken autogen step silently produced an empty fixture the check
then read as "nothing to do."

This PR extracts stub generation into a standalone entry point and makes
it fail loudly, so the check runs against a faithfully generated stub
set without the full render.

## What

Extracts the stub generation into `doc/source/api_autogen.py`, shared by
the render and a standalone entry point:

- **`conf.py`** imports the autogen file list (`AUTOGEN_FILES`), the
filename map, and the custom autosummary Jinja filters from the new
module (importing it registers the filters), keeping the render and the
standalone path in lockstep. No behavior change to the render except the
loud failure below.
- **Fail loudly:** `generate_api_stubs()` raises when generation
produces no files, and `conf.py` no longer swallows failures. A broken
autogen step fails the build instead of emitting an empty fixture.
- **Decouple:** `lint.sh`'s `api_policy_check` runs `python
doc/source/api_autogen.py` instead of `make -C doc/ html`, removing the
full render from the premerge check while preserving the contract
guarantee. Stubs are generated *after* installing Ray so they reflect
the checkout's source.

The standalone path mirrors `sphinx-autogen` (a minimal
`DummyApplication` with the project templates on the path), so it
produces byte-identical stubs without the rest of the build. It applies
the shared absent-backend mock from `doc/source/api_mock_imports.py`
(introduced separately, see below), so the optional-dependency modules
that the check imports are the same ones the generator sees.

## Relationship to #64808

The mock, whitelist, and doc-debt work that greened the check across all
libraries — including `doc/source/api_mock_imports.py` and every
`TEAM_API_CONFIGS` change — landed first in **#64808** and is now in
`master`. This PR is rebased on top of it and **only consumes** the
shared mock; it introduces none of that config. The diff here is exactly
three files: `doc/source/api_autogen.py` (new), `doc/source/conf.py`,
and `ci/lint/lint.sh`.

## Why data-only `AUTOGEN_FILES` is complete

`AUTOGEN_FILES` is `["data/api/_autogen.rst"]`, and that is the full set
the check needs, not a partial one. The consistency check's doc-side
reader (`ci/ray_ci/doc/autodoc.py`) parses `.. autosummary::` directives
**directly from the checked-in source**, following only explicit `..
include::` and `.. toctree::` edges. It descends into a *generated* stub
only where a tracked page includes one — and only the `data` API pages
do that (`dataset.rst` etc. `.. include::` the generated
`ray.data.Dataset.rst` method tables). The other teams list their APIs
with `.. autosummary::` directives in tracked source, which the check
reads without any generated file.

Two facts make this exact, independent of Ray version:

- Running the real `Autodoc` traversal against a clean checkout, the
only generated files it follows are the three `data` class stubs — which
this generator produces. core / train / tune / serve / rllib follow zero
generated files.
- The method-table templates (`_templates/autosummary/class.rst`,
`class_v2.rst`) emit only `.. autosummary::`, never `.. include::`/`..
toctree::`, so a generated stub never adds a new edge the traversal
would follow.

So the stubs `make html` generated for the other teams were never read
by the check; dropping them changes nothing it verifies. (A follow-up
would only be needed if a non-data team later adopts the
include-a-generated-method-stub pattern.)

## Verification

- **Rebase:** reduced to a single commit on current `master`
(post-#64808). Final diff is the three files above, +217/−86.
- **Static (local):** all changed modules `py_compile`; `pre-commit`
passes on the changed files (trailing-whitespace, end-of-file,
shellcheck on `lint.sh`, semgrep — ruff/black skip `doc/source/**` by
Ray's config).
- **Stub parity (local, earlier, against a matching Ray build):** the
standalone script generated the same data stubs as the full render (82
files), the consistency check read them, and a genuine generation
failure exited nonzero where the old code only warned.
- **Decisive gate:** the `doc: check API doc consistency` premerge job
runs `python ci/ray_ci/doc/cmd_check_api_discrepancy.py <checkout> ALL`
on the docbuild image. Confirming its result on this PR before leaving
draft.

## Risk / coordination

`lint.sh` is a shared premerge surface and `conf.py` is the production
render config. The functional change is the stub-generation source for
the check (see the scope note above); the set of stubs the check
actually reads is unchanged. Requesting doc-CI-owner review before this
leaves draft.

AI assistance was used to author these changes; every changed line was
reviewed by the submitter.

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…y-project#64808)

## Why

The API-doc consistency check (`ci/ray_ci/doc`) gained
reverse-resolution and dedup policies, but it runs **only on premerge
PRs, never postmerge**, so master drifted red undetected: any PR that
touches a file mapping to this job now fails on pre-existing
discrepancies across **data, serve, train, tune, and rllib**. This
greens the check for all libraries on the existing `make -C doc/ html`
path, so those PRs stop tripping over debt they didn't introduce.

This is deliberately the **whitelist + mock half**. Decoupling stub
generation from the full render is a separate change that builds on
this.

## What

### Mock uninstalled backends

The check imports documented names directly (`resolve()` /
`Module.get_apis()`), bypassing Sphinx. Optional-dependency modules —
`ray.data.llm`, `ray.serve.llm`, `ray.train.lightning` / `xgboost` /
`lightgbm`, `ray.tune.integration.*` — eagerly import vLLM /
transformers / torch / pytorch_lightning, which are absent on the CPU
docbuild runner, so they read as *unresolved* even though the render
shows them fine (it mocks the same libs via `autodoc_mock_imports`).

A new side-effect-free `doc/source/api_mock_imports.py` holds the mock
list as the single source of truth, imported by both `conf.py` and the
check, split into `THIRD_PARTY_MOCK_MODULES` (safe for the check) and
`BUILD_ONLY_MOCK_MODULES` (`ray.*` compiled/generated, render-only). The
check applies the third-party mocks via
`sphinx.ext.autodoc.mock.mock()`. **`ray.*` is never mocked**, so the
resolve/dedup policy keeps its teeth on Ray's own symbols. This alone
clears **all serve and train failures** and the optional-dep tail of
data and tune.

### Whitelist the rest by intent

- **`intentional_duplicate_apis`** — canonical names cross-listed on
purpose: data `Dataset.to_*` / `write_*` (class stub + saving-data
page), tune `AsyncHyperBandScheduler`, rllib
`AlgorithmConfig.build_learner*` / `Checkpointable.save_to_path` etc.
- **`doc_only_whitelist`** (data) — public APIs re-exported from
`_internal` (`ActorPoolStrategy`, `ExecutionOptions`, …) that resolve
fine but trip the private-name heuristic. Exempted from the resolve
check only.
- **`tracked_doc_debt`** — genuine debt owed a document-or-deprecate
decision: data's deprecated + private-named accessors, tune's
`Stopper.__call__`, and RLlib's larger surface (unresolved/malformed
autosummary entries and documented private/dunder members). RLlib's
entries are **parked to unblock CI; each still wants a real fix from the
RLlib team** (fix the doc entry, deprecate, or de-annotate) and is
commented as such.

Per-team result: serve/train clear via mock alone; data/tune via mock +
a few entries; rllib via whitelisting (it imports fine, so the mock
doesn't apply); core was already clean.

## Verification

- `pre-commit run --files` passes on all changed files (ruff, black, Ray
import-order, Ray docstyle, semgrep, not-real-mock-methods).
- `py_compile` on the changed modules; `TEAM_API_CONFIGS` inspected via
AST for the expected per-team counts.
- The in-image `python ci/ray_ci/doc/cmd_check_api_discrepancy.py
<checkout> ALL` run is the decisive gate — it runs as this PR's `doc:
check API doc consistency` premerge job. One thing that run confirms and
local static checks can't: that the mocked optional-dep modules resolve
to *public* objects (not deprecated/private canonicals).

## Not a duplicate

Part of the API-ref consistency workstream; no competing PR. The
whitelist-split change it builds on already merged. No open PR greens
the check for these teams.

## AI assistance

AI assistance was used to author these changes; every changed line was
reviewed by the submitter.

---------

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…y-project#64423)

## Why

The `doc: check API doc consistency` premerge step reads autosummary
stub `.rst` files — the per-class method tables that the hand-written
API pages `.. include::`. Those stubs are produced only as a side effect
of `make -C doc/ html`, so **every Python-touching premerge PR pays for
a full Sphinx render just to generate them**. Separately, the generation
was wrapped in a `try/except` that downgraded any failure to a warning,
so a broken autogen step silently produced an empty fixture the check
then read as "nothing to do."

This PR extracts stub generation into a standalone entry point and makes
it fail loudly, so the check runs against a faithfully generated stub
set without the full render.

## What

Extracts the stub generation into `doc/source/api_autogen.py`, shared by
the render and a standalone entry point:

- **`conf.py`** imports the autogen file list (`AUTOGEN_FILES`), the
filename map, and the custom autosummary Jinja filters from the new
module (importing it registers the filters), keeping the render and the
standalone path in lockstep. No behavior change to the render except the
loud failure below.
- **Fail loudly:** `generate_api_stubs()` raises when generation
produces no files, and `conf.py` no longer swallows failures. A broken
autogen step fails the build instead of emitting an empty fixture.
- **Decouple:** `lint.sh`'s `api_policy_check` runs `python
doc/source/api_autogen.py` instead of `make -C doc/ html`, removing the
full render from the premerge check while preserving the contract
guarantee. Stubs are generated *after* installing Ray so they reflect
the checkout's source.

The standalone path mirrors `sphinx-autogen` (a minimal
`DummyApplication` with the project templates on the path), so it
produces byte-identical stubs without the rest of the build. It applies
the shared absent-backend mock from `doc/source/api_mock_imports.py`
(introduced separately, see below), so the optional-dependency modules
that the check imports are the same ones the generator sees.

## Relationship to ray-project#64808

The mock, whitelist, and doc-debt work that greened the check across all
libraries — including `doc/source/api_mock_imports.py` and every
`TEAM_API_CONFIGS` change — landed first in **ray-project#64808** and is now in
`master`. This PR is rebased on top of it and **only consumes** the
shared mock; it introduces none of that config. The diff here is exactly
three files: `doc/source/api_autogen.py` (new), `doc/source/conf.py`,
and `ci/lint/lint.sh`.

## Why data-only `AUTOGEN_FILES` is complete

`AUTOGEN_FILES` is `["data/api/_autogen.rst"]`, and that is the full set
the check needs, not a partial one. The consistency check's doc-side
reader (`ci/ray_ci/doc/autodoc.py`) parses `.. autosummary::` directives
**directly from the checked-in source**, following only explicit `..
include::` and `.. toctree::` edges. It descends into a *generated* stub
only where a tracked page includes one — and only the `data` API pages
do that (`dataset.rst` etc. `.. include::` the generated
`ray.data.Dataset.rst` method tables). The other teams list their APIs
with `.. autosummary::` directives in tracked source, which the check
reads without any generated file.

Two facts make this exact, independent of Ray version:

- Running the real `Autodoc` traversal against a clean checkout, the
only generated files it follows are the three `data` class stubs — which
this generator produces. core / train / tune / serve / rllib follow zero
generated files.
- The method-table templates (`_templates/autosummary/class.rst`,
`class_v2.rst`) emit only `.. autosummary::`, never `.. include::`/`..
toctree::`, so a generated stub never adds a new edge the traversal
would follow.

So the stubs `make html` generated for the other teams were never read
by the check; dropping them changes nothing it verifies. (A follow-up
would only be needed if a non-data team later adopts the
include-a-generated-method-stub pattern.)

## Verification

- **Rebase:** reduced to a single commit on current `master`
(post-ray-project#64808). Final diff is the three files above, +217/−86.
- **Static (local):** all changed modules `py_compile`; `pre-commit`
passes on the changed files (trailing-whitespace, end-of-file,
shellcheck on `lint.sh`, semgrep — ruff/black skip `doc/source/**` by
Ray's config).
- **Stub parity (local, earlier, against a matching Ray build):** the
standalone script generated the same data stubs as the full render (82
files), the consistency check read them, and a genuine generation
failure exited nonzero where the old code only warned.
- **Decisive gate:** the `doc: check API doc consistency` premerge job
runs `python ci/ray_ci/doc/cmd_check_api_discrepancy.py <checkout> ALL`
on the docbuild image. Confirming its result on this PR before leaving
draft.

## Risk / coordination

`lint.sh` is a shared premerge surface and `conf.py` is the production
render config. The functional change is the stub-generation source for
the check (see the scope note above); the set of stubs the check
actually reads is unchanged. Requesting doc-CI-owner review before this
leaves draft.

AI assistance was used to author these changes; every changed line was
reviewed by the submitter.

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants