Repository navigation
[ci/doc] Guard against annotated subpackages that escape the API walk - #64514
dstrodtman wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request optimizes the API documentation consistency check by generating only the necessary autosummary stub files via a new standalone script (api_autogen.py), eliminating the need to build the entire HTML documentation. It also introduces Policy 02 (ensuring documented APIs resolve to public code) and Policy 04 (preventing duplicate documentation), along with a coverage guard to catch unwalked annotated subpackages. The feedback suggests adding exception handling around dynamic attribute inspection and module iteration in cmd_check_api_discrepancy.py to prevent crashes from lazy imports or invalid paths.
| for attr in dir(mod): | ||
| obj = getattr(mod, attr, None) | ||
| origin = getattr(obj, "__module__", None) | ||
| if not origin or (origin != module and not origin.startswith(f"{module}.")): | ||
| continue | ||
| if hasattr(obj, "_annotated"): | ||
| return (True, True) |
There was a problem hiding this comment.
Accessing attributes dynamically via getattr on a module can trigger lazy imports or properties that raise exceptions (such as ModuleNotFoundError for missing optional dependencies). Wrapping the attribute inspection in a try-except block ensures that optional dependency failures or buggy attributes do not crash the entire API consistency check.
| for attr in dir(mod): | |
| obj = getattr(mod, attr, None) | |
| origin = getattr(obj, "__module__", None) | |
| if not origin or (origin != module and not origin.startswith(f"{module}.")): | |
| continue | |
| if hasattr(obj, "_annotated"): | |
| return (True, True) | |
| for attr in dir(mod): | |
| try: | |
| obj = getattr(mod, attr, None) | |
| origin = getattr(obj, "__module__", None) | |
| if not origin or (origin != module and not origin.startswith(f"{module}.")): | |
| continue | |
| if hasattr(obj, "_annotated"): | |
| return (True, True) | |
| except Exception: | |
| continue |
| children = [] | ||
| for info in pkgutil.iter_modules(pkg.__path__, prefix=f"{package}."): | ||
| if info.name.rsplit(".", 1)[-1].startswith("_"): | ||
| continue | ||
| children.append(info.name) |
There was a problem hiding this comment.
Calling pkgutil.iter_modules on pkg.__path__ can raise exceptions if any of the paths in __path__ are invalid, non-existent, or inaccessible (e.g., due to permission issues or virtual environment configurations). Wrapping this in a try-except block makes the subpackage discovery more robust.
| children = [] | |
| for info in pkgutil.iter_modules(pkg.__path__, prefix=f"{package}."): | |
| if info.name.rsplit(".", 1)[-1].startswith("_"): | |
| continue | |
| children.append(info.name) | |
| children = [] | |
| try: | |
| for info in pkgutil.iter_modules(pkg.__path__, prefix=f"{package}."): | |
| if info.name.rsplit(".", 1)[-1].startswith("_"): | |
| continue | |
| children.append(info.name) | |
| except Exception: | |
| pass |
17028a4 to
02f54be
Compare
|
Thanks for the review. Two notes: Scope: this PR only adds three things — Both inline suggestions adopted in the latest push:
Both are consistent with the guard's own principle: an optional-dependency failure should downgrade coverage of one module, never crash the whole consistency check. |
6fa7e38 to
87c4429
Compare
|
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. |
The code<->docs API consistency check only sees APIs that its module walk reaches, and the walk only follows submodules a parent `__init__` actually imports. A public, `@PublicAPI`-annotated subpackage that its parent does not import is invisible to the check, so its APIs can silently rot undocumented. `ray.serve.llm` and `ray.data.llm` are both such surfaces today: they are documented and annotated, but no walk reaches them. - Add `ray.serve.llm` to the serve `head_modules` so its public surface is walked. It is import-safe in the docbuild image (transformers is a soft `try_import`, vLLM is only imported lazily). - Add a cross-team guard that enumerates one level of subpackages under every configured head module and fails when an annotated subpackage is neither reached by any walk nor knowingly excluded. A reviewed allowlist records the intentional exclusions and why. - Allowlist `ray.data.llm`: it eagerly imports the vLLM/SGLang engine processor configs, which do a hard `import transformers` at module load, and transformers is absent from the docbuild image, so it cannot be imported by this check. Its surface stays documented in `doc/source/data/api/llm.rst`. - Track reachable module names on `Module` so the guard can tell "walked" from "merely name-prefixed" (`ray.data.llm` is prefixed by `ray.data` yet never imported). - Unit-test the guard's decision core and helpers against the mock module. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
87c4429 to
1875119
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a cross-team coverage guard to detect annotated public subpackages that are not reached by any team's walk during the API consistency check, along with corresponding unit tests and tracking updates in the Module class. The reviewer identified a critical issue where running the entire coverage guard outside of the backend mock environment could cause head modules with optional dependencies to fail to import, silently disabling the guard for their subpackages. To resolve this, the reviewer suggested collecting the covered modules and immediate children inside the mock block while keeping the actual import status checks outside of it.
| def _check_unwalked_annotated_subpackages() -> bool: | ||
| """Guard: fail when an annotated public subpackage escapes every team's walk. | ||
|
|
||
| The code<->docs consistency check only sees APIs the walk reaches, and the walk | ||
| only follows submodules a parent __init__ actually imports. A public subpackage | ||
| that its parent does not import (e.g. ray.data.llm, ray.serve.llm) is invisible to | ||
| the check -- its APIs can silently rot undocumented. This guard enumerates one | ||
| level of subpackages under every configured head module and fails on any that is | ||
| neither walked nor knowingly excluded via UNWALKED_ANNOTATED_ALLOWLIST. | ||
| """ | ||
| print( | ||
| "--- Validating that annotated subpackages are reachable by some walk...", | ||
| file=sys.stderr, | ||
| ) | ||
| covered = _covered_module_names() | ||
|
|
||
| child_status: Dict[str, Tuple[bool, bool]] = {} | ||
| for head in _team_head_modules(): | ||
| for child in _immediate_child_modules(head): | ||
| if child not in child_status: | ||
| child_status[child] = _import_status(child) |
There was a problem hiding this comment.
Running the entire coverage guard outside of _mock_uninstalled_backends introduces a silent coverage gap. If a head module itself (e.g., ray.rllib or ray.train) fails to import without the backend mocks, _immediate_child_modules(head) will catch the exception and return []. This silently disables the guard for all of its subpackages, meaning any new unwalked public subpackages under those teams will go completely unnoticed.
To fix this, we should collect the covered module names and children list inside the mock block (where head modules can import successfully), and then pass them to _check_unwalked_annotated_subpackages to perform the actual import status checks outside the mock block.
| def _check_unwalked_annotated_subpackages() -> bool: | |
| """Guard: fail when an annotated public subpackage escapes every team's walk. | |
| The code<->docs consistency check only sees APIs the walk reaches, and the walk | |
| only follows submodules a parent __init__ actually imports. A public subpackage | |
| that its parent does not import (e.g. ray.data.llm, ray.serve.llm) is invisible to | |
| the check -- its APIs can silently rot undocumented. This guard enumerates one | |
| level of subpackages under every configured head module and fails on any that is | |
| neither walked nor knowingly excluded via UNWALKED_ANNOTATED_ALLOWLIST. | |
| """ | |
| print( | |
| "--- Validating that annotated subpackages are reachable by some walk...", | |
| file=sys.stderr, | |
| ) | |
| covered = _covered_module_names() | |
| child_status: Dict[str, Tuple[bool, bool]] = {} | |
| for head in _team_head_modules(): | |
| for child in _immediate_child_modules(head): | |
| if child not in child_status: | |
| child_status[child] = _import_status(child) | |
| def _check_unwalked_annotated_subpackages(covered: Set[str], children: List[str]) -> bool: | |
| """Guard: fail when an annotated public subpackage escapes every team's walk. | |
| The code<->docs consistency check only sees APIs the walk reaches, and the walk | |
| only follows submodules a parent __init__ actually imports. A public subpackage | |
| that its parent does not import (e.g. ray.data.llm, ray.serve.llm) is invisible to | |
| the check -- its APIs can silently rot undocumented. This guard fails on any | |
| unwalked annotated subpackage that is not knowingly excluded via | |
| UNWALKED_ANNOTATED_ALLOWLIST. | |
| """ | |
| print( | |
| "--- Validating that annotated subpackages are reachable by some walk...", | |
| file=sys.stderr, | |
| ) | |
| child_status: Dict[str, Tuple[bool, bool]] = {} | |
| for child in children: | |
| if child not in child_status: | |
| child_status[child] = _import_status(child) |
| continue | ||
| if not _check_team(ray_checkout_dir, team): | ||
| all_pass = False | ||
| if not all_pass: | ||
| exit(1) | ||
|
|
||
| # Cross-team guard: catch annotated subpackages that no team's walk reaches. | ||
| # It runs outside _mock_uninstalled_backends on purpose. The guard reasons | ||
| # about real import behavior in the docbuild image -- an optional-dependency | ||
| # subpackage that genuinely can't be imported (e.g. ray.data.llm, whose eager | ||
| # `import transformers` fails there) is categorized "unverifiable" and recorded | ||
| # in UNWALKED_ANNOTATED_ALLOWLIST. Running it under the backend mock would make | ||
| # those modules import cleanly and silently defeat that reasoning. | ||
| if not _check_unwalked_annotated_subpackages(): | ||
| all_pass = False |
There was a problem hiding this comment.
Collect the covered module names and immediate children inside the _mock_uninstalled_backends block to ensure head modules with optional dependencies can be imported successfully, and then pass them to _check_unwalked_annotated_subpackages outside the mock block.
| continue | |
| if not _check_team(ray_checkout_dir, team): | |
| all_pass = False | |
| if not all_pass: | |
| exit(1) | |
| # Cross-team guard: catch annotated subpackages that no team's walk reaches. | |
| # It runs outside _mock_uninstalled_backends on purpose. The guard reasons | |
| # about real import behavior in the docbuild image -- an optional-dependency | |
| # subpackage that genuinely can't be imported (e.g. ray.data.llm, whose eager | |
| # `import transformers` fails there) is categorized "unverifiable" and recorded | |
| # in UNWALKED_ANNOTATED_ALLOWLIST. Running it under the backend mock would make | |
| # those modules import cleanly and silently defeat that reasoning. | |
| if not _check_unwalked_annotated_subpackages(): | |
| all_pass = False | |
| continue | |
| if not _check_team(ray_checkout_dir, team): | |
| all_pass = False | |
| # Collect covered modules and immediate children under the mock environment | |
| # so that head modules with optional dependencies can be imported successfully. | |
| covered = _covered_module_names() | |
| children = [] | |
| for head in _team_head_modules(): | |
| children.extend(_immediate_child_modules(head)) | |
| # Cross-team guard: catch annotated subpackages that no team's walk reaches. | |
| # It runs outside _mock_uninstalled_backends on purpose. The guard reasons | |
| # about real import behavior in the docbuild image -- an optional-dependency | |
| # subpackage that genuinely can't be imported (e.g. ray.data.llm, whose eager | |
| # `import transformers` fails there) is categorized "unverifiable" and recorded | |
| # in UNWALKED_ANNOTATED_ALLOWLIST. Running it under the backend mock would make | |
| # those modules import cleanly and silently defeat that reasoning. | |
| if not _check_unwalked_annotated_subpackages(covered, children): | |
| all_pass = False |
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 a111945. Configure here.
| # (transformers is a soft try_import; vllm is only imported lazily), so it can | ||
| # be a walk root of its own. The unwalked-subpackage guard would otherwise flag | ||
| # it. (Its sibling ray.data.llm is NOT import-safe -- see UNWALKED_*_ALLOWLIST.) | ||
| "head_modules": {"ray.serve", "ray.serve.llm"}, |
There was a problem hiding this comment.
Serve LLM children trip coverage guard
High Severity
Adding ray.serve.llm as a head_modules entry makes the new guard enumerate its immediate children. Those children (deployment, ingress, openai_api_models, request_router) define @PublicAPI symbols, are import-safe, are never imported by ray.serve.llm’s __init__ (so no walk reaches them), and are absent from UNWALKED_ANNOTATED_ALLOWLIST. The guard therefore fails the consistency check it was meant to pass.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit a111945. Configure here.
…ting it (#65040) ## Why This is an **alternative** to #64514. Same goal — no `@PublicAPI` subpackage silently escapes the code↔docs consistency walk — but it walks `ray.data.llm` instead of allowlisting it. #64514 allowlists `ray.data.llm` on the premise that its eager `import transformers` (via the vLLM/SGLang engine processor configs) can't be imported in the docbuild image. That premise predates the `_mock_uninstalled_backends` wrapper now on `master`, which mocks exactly the backends the docbuild image lacks. Under that mock, `ray.data.llm` imports cleanly and can be a walk root like `ray.serve.llm` — so its documented surface gets checked rather than merely excluded. ## What - Add `ray.data.llm` to the data team `head_modules`. Its surface is documented in `doc/source/data/api/llm.rst`, reachable from `api.rst`'s toctree, so the walk checks the code surface against the real docs. - Run the cross-team coverage guard **inside** `_mock_uninstalled_backends` so its coverage check and import probes see the same mocks as the walks; a promoted head like `ray.data.llm` is counted as covered, not flagged. - Empty `UNWALKED_ANNOTATED_ALLOWLIST`: both known escapees (`ray.serve.llm`, `ray.data.llm`) are now walked. The list stays as the reviewed record for any future subpackage that genuinely can't be a walk root. - Decouple the guard decision-core unit tests from real module names. ## Relationship to #64514 Pick one, not both. This branch carries #64514's guard commit plus one delta commit; if this approach is preferred, the two squash into one before un-drafting. ## Checks The decisive check is **doc: check API doc consistency** — it exercises the mocked `ray.data.llm` import path this approach depends on, against the real docbuild image. Kept as a draft until that check is confirmed green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
|
Superseded by #65040 |
…ting it (ray-project#65040) ## Why This is an **alternative** to ray-project#64514. Same goal — no `@PublicAPI` subpackage silently escapes the code↔docs consistency walk — but it walks `ray.data.llm` instead of allowlisting it. ray-project#64514 allowlists `ray.data.llm` on the premise that its eager `import transformers` (via the vLLM/SGLang engine processor configs) can't be imported in the docbuild image. That premise predates the `_mock_uninstalled_backends` wrapper now on `master`, which mocks exactly the backends the docbuild image lacks. Under that mock, `ray.data.llm` imports cleanly and can be a walk root like `ray.serve.llm` — so its documented surface gets checked rather than merely excluded. ## What - Add `ray.data.llm` to the data team `head_modules`. Its surface is documented in `doc/source/data/api/llm.rst`, reachable from `api.rst`'s toctree, so the walk checks the code surface against the real docs. - Run the cross-team coverage guard **inside** `_mock_uninstalled_backends` so its coverage check and import probes see the same mocks as the walks; a promoted head like `ray.data.llm` is counted as covered, not flagged. - Empty `UNWALKED_ANNOTATED_ALLOWLIST`: both known escapees (`ray.serve.llm`, `ray.data.llm`) are now walked. The list stays as the reviewed record for any future subpackage that genuinely can't be a walk root. - Decouple the guard decision-core unit tests from real module names. ## Relationship to ray-project#64514 Pick one, not both. This branch carries ray-project#64514's guard commit plus one delta commit; if this approach is preferred, the two squash into one before un-drafting. ## Checks The decisive check is **doc: check API doc consistency** — it exercises the mocked `ray.data.llm` import path this approach depends on, against the real docbuild image. Kept as a draft until that check is confirmed green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…ting it (ray-project#65040) ## Why This is an **alternative** to ray-project#64514. Same goal — no `@PublicAPI` subpackage silently escapes the code↔docs consistency walk — but it walks `ray.data.llm` instead of allowlisting it. ray-project#64514 allowlists `ray.data.llm` on the premise that its eager `import transformers` (via the vLLM/SGLang engine processor configs) can't be imported in the docbuild image. That premise predates the `_mock_uninstalled_backends` wrapper now on `master`, which mocks exactly the backends the docbuild image lacks. Under that mock, `ray.data.llm` imports cleanly and can be a walk root like `ray.serve.llm` — so its documented surface gets checked rather than merely excluded. ## What - Add `ray.data.llm` to the data team `head_modules`. Its surface is documented in `doc/source/data/api/llm.rst`, reachable from `api.rst`'s toctree, so the walk checks the code surface against the real docs. - Run the cross-team coverage guard **inside** `_mock_uninstalled_backends` so its coverage check and import probes see the same mocks as the walks; a promoted head like `ray.data.llm` is counted as covered, not flagged. - Empty `UNWALKED_ANNOTATED_ALLOWLIST`: both known escapees (`ray.serve.llm`, `ray.data.llm`) are now walked. The list stays as the reviewed record for any future subpackage that genuinely can't be a walk root. - Decouple the guard decision-core unit tests from real module names. ## Relationship to ray-project#64514 Pick one, not both. This branch carries ray-project#64514's guard commit plus one delta commit; if this approach is preferred, the two squash into one before un-drafting. ## Checks The decisive check is **doc: check API doc consistency** — it exercises the mocked `ray.data.llm` import path this approach depends on, against the real docbuild image. Kept as a draft until that check is confirmed green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…ting it (ray-project#65040) ## Why This is an **alternative** to ray-project#64514. Same goal — no `@PublicAPI` subpackage silently escapes the code↔docs consistency walk — but it walks `ray.data.llm` instead of allowlisting it. ray-project#64514 allowlists `ray.data.llm` on the premise that its eager `import transformers` (via the vLLM/SGLang engine processor configs) can't be imported in the docbuild image. That premise predates the `_mock_uninstalled_backends` wrapper now on `master`, which mocks exactly the backends the docbuild image lacks. Under that mock, `ray.data.llm` imports cleanly and can be a walk root like `ray.serve.llm` — so its documented surface gets checked rather than merely excluded. ## What - Add `ray.data.llm` to the data team `head_modules`. Its surface is documented in `doc/source/data/api/llm.rst`, reachable from `api.rst`'s toctree, so the walk checks the code surface against the real docs. - Run the cross-team coverage guard **inside** `_mock_uninstalled_backends` so its coverage check and import probes see the same mocks as the walks; a promoted head like `ray.data.llm` is counted as covered, not flagged. - Empty `UNWALKED_ANNOTATED_ALLOWLIST`: both known escapees (`ray.serve.llm`, `ray.data.llm`) are now walked. The list stays as the reviewed record for any future subpackage that genuinely can't be a walk root. - Decouple the guard decision-core unit tests from real module names. ## Relationship to ray-project#64514 Pick one, not both. This branch carries ray-project#64514's guard commit plus one delta commit; if this approach is preferred, the two squash into one before un-drafting. ## Checks The decisive check is **doc: check API doc consistency** — it exercises the mocked `ray.data.llm` import path this approach depends on, against the real docbuild image. Kept as a draft until that check is confirmed green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: 400Ping <[email protected]>
…ting it (ray-project#65040) ## Why This is an **alternative** to ray-project#64514. Same goal — no `@PublicAPI` subpackage silently escapes the code↔docs consistency walk — but it walks `ray.data.llm` instead of allowlisting it. ray-project#64514 allowlists `ray.data.llm` on the premise that its eager `import transformers` (via the vLLM/SGLang engine processor configs) can't be imported in the docbuild image. That premise predates the `_mock_uninstalled_backends` wrapper now on `master`, which mocks exactly the backends the docbuild image lacks. Under that mock, `ray.data.llm` imports cleanly and can be a walk root like `ray.serve.llm` — so its documented surface gets checked rather than merely excluded. ## What - Add `ray.data.llm` to the data team `head_modules`. Its surface is documented in `doc/source/data/api/llm.rst`, reachable from `api.rst`'s toctree, so the walk checks the code surface against the real docs. - Run the cross-team coverage guard **inside** `_mock_uninstalled_backends` so its coverage check and import probes see the same mocks as the walks; a promoted head like `ray.data.llm` is counted as covered, not flagged. - Empty `UNWALKED_ANNOTATED_ALLOWLIST`: both known escapees (`ray.serve.llm`, `ray.data.llm`) are now walked. The list stays as the reviewed record for any future subpackage that genuinely can't be a walk root. - Decouple the guard decision-core unit tests from real module names. ## Relationship to ray-project#64514 Pick one, not both. This branch carries ray-project#64514's guard commit plus one delta commit; if this approach is preferred, the two squash into one before un-drafting. ## Checks The decisive check is **doc: check API doc consistency** — it exercises the mocked `ray.data.llm` import path this approach depends on, against the real docbuild image. Kept as a draft until that check is confirmed green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>


Why
The API doc consistency check (
ci/ray_ci/doc/cmd_check_api_discrepancy.py)only sees APIs that its module walk reaches, and the walk only follows
submodules a parent
__init__actually imports. A public,@PublicAPI-annotated subpackage that its parent does not import is invisibleto the check — its APIs can silently rot undocumented.
ray.serve.llmandray.data.llmare both such surfaces today: documented andannotated, but reached by no walk.
What
ray.serve.llmby adding it to the servehead_modules. It isimport-safe in the docbuild image (
transformersis a softtry_import, andvLLM is only imported lazily), so it can be its own walk root.
under every configured head module and fails when an annotated subpackage is
neither reached by any walk nor knowingly excluded. A reviewed allowlist
records the intentional exclusions and the reason for each.
ray.data.llm. Unlikeray.serve.llm, it eagerly imports thevLLM/SGLang engine processor configs, which do a hard
import transformersat module load.
transformersis absent from the docbuild image(
python/deplocks/docs/docbuild_depset_py3.11.lock), soray.data.llmcannot be imported by this check. Its surface stays documented in
doc/source/data/api/llm.rst.Moduleso the guard distinguishes "actuallywalked" from "merely name-prefixed" (
ray.data.llmis prefixed byray.datayet never imported by
ray/data/__init__.py).Stacking
This PR is stacked on top of the API-ref consistency-policy work and should be
reviewed/merged after it (its diff transitively shows those commits until they
land):
Kept as a draft until the base PRs merge; it will be rebased onto
masterafterward.
Checks
The decisive check is doc: check API doc consistency — it exercises the new
guard,
ray.serve.llminhead_modules, and theray.data.llmallowlistagainst the real docbuild image.
🤖 Generated with Claude Code