Skip to content

[ci] Extend the docstring-only guard to Cython and .pyi files - #67599

Merged
dstrodtman merged 2 commits into
masterfrom
djs-261030-docstring-guard-cython
Oct 30, 2026
Merged

dstrodtman merged 2 commits into
masterfrom
djs-261030-docstring-guard-cython

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Why are these changes needed?

The docstring-only label's scope guard, ci/ray_ci/doc/check_docstring_only.py, rejects every file that isn't .py. Docstring edits to Cython bindings therefore can't use the label, even when they change nothing else. #67592 hit this: its edits to python/ray/includes/gcs_client.pxi and global_state_accessor.pxi only add and rewrite docstrings, and the guard failed them with "isn't a Python file" (build 75788).

This PR extends the guard to two more kinds of file:

  • .pyi stubs are Python, so they go through the existing ast comparison unchanged.
  • Cython files, .pyx, .pxi, and .pxd, can't go through ast, which can't parse cdef. The guard compares their tokenize token streams instead. It drops comments, blank lines, and line breaks inside brackets. It removes a string only where Python allows a docstring: the first statement of the file, or the first statement in the indented body of a def, class, cdef/cpdef function, or cdef class.

Two Cython constructs look like comments or docstrings but are code, and the guard keeps both:

  • # cython: and # distutils: comments set compiler directives and build options. _raylet.pyx, _raylet.pxd, and libcoreworker.pxd carry them, including embedsignature.
  • A string in a cdef extern from block is verbatim C or C++ that Cython compiles. Five files under python/ray/ have one. A cdef extern header has neither a parameter list nor class, so the guard never treats that string as a docstring.

The token comparison fails closed where it differs from the ast rule. It treats replacing a docstring-only body with pass as a code change, and it doesn't strip a docstring written on the same line as its def.

The label still compiles the PR's Cython. A python/ change selects doc: docstring examples, which depends on ray-core-build, so a docstring edit that breaks the Cython build still fails the PR.

ci.md, ci/ray_ci/doc/README.md, and the step comment in .buildkite/lint.rayci.yml now name the covered file types and the two Cython exceptions. ci.md also notes that doc: docstring examples runs only Python modules. No CI step runs >>> examples in Cython docstrings, with or without the label, because no doctest() target lists a Cython file and --doctest-modules collects only .py.

Testing

  • pytest ci/ray_ci/doc/test_check_docstring_only.py passes all 33 tests on Python 3.10, which is the forge image's version, and on 3.12 and 3.13. The new tests cover stub docstrings, the [Docs][Core] Specify if the node id needs to be a hex or byte value  #67592 edit shapes, line rewrapping, indentation changes, the build comments, the extern C block, a non-leading string, an f-string in the docstring position, a Cython cast, an except? clause, and a tokenize error.
  • On [Docs][Core] Specify if the node id needs to be a hex or byte value  #67592's head against its merge base, the new guard reports "5 file(s), docstrings only." and exits 0. The guard on master fails the same diff the way CI did.
  • All 39 .pyx, .pxi, .pxd, and .pyi files on master tokenize on Python 3.10 through 3.13.
  • Mutation test on master's Cython files: the guard caught all 1,520 injected single-token and indentation edits to code, plus every directive-comment change.
  • History replay: I ran the comparison on every modification to a Cython file in the last 940 commits on master that touched one, 1,831 file changes in all. It passed 42, and I read each of those diffs: all are docstring, comment, blank-line, or in-bracket rewrapping changes. The other 1,789 changed code and failed.
  • pre-commit run on the changed files passes.

Duplicate check

No open PR changes check_docstring_only.py or the label's file coverage. I searched open PRs for "docstring-only" and "check_docstring_only".

AI assistance

I wrote this change with Claude Code (Opus 5.5). I reviewed every changed line, and I ran the tests and checks listed above.

🤖 Generated with Claude Code

The docstring-only scope guard rejected every non-.py file, so docstring
edits to Cython bindings couldn't use the label. Compare .pyi stubs with
the existing ast rule, and compare .pyx, .pxi, and .pxd files by their
tokenize token streams with comments and docstrings removed. Keep
`# cython:` and `# distutils:` comments and `cdef extern from` C blocks
as code, since both change the compiled module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman requested review from a team as code owners October 30, 2026 13:37

@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 extends the 'docstring-only' CI guard to support Cython files (.pyx, .pxi, and .pxd) by implementing a token-based comparison using Python's tokenize module, as Cython cannot be parsed by ast. It correctly handles Cython-specific constructs like build comments and verbatim C blocks. The feedback points out a potential issue in _owns_docstring where non-function async blocks (like async with or async for) could be incorrectly matched, and suggests restricting the check to async def blocks.

Comment thread ci/ray_ci/doc/check_docstring_only.py Outdated
Comment on lines +135 to +136
if words[0] in ("def", "class", "async"):
return True

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

The current check words[0] in ("def", "class", "async") will incorrectly return True for other block headers starting with async, such as async with or async for. Since these blocks cannot have docstrings, any string literal at the start of their body could be incorrectly stripped as a docstring, potentially bypassing code checks. We should restrict the async check to only match async def function definitions.

Suggested change
if words[0] in ("def", "class", "async"):
return True
if words[0] in ("def", "class") or (words[0] == "async" and len(words) > 1 and words[1] == "def"):
return True

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 15e0d30: only async def now counts as a docstring owner, so the first string in an async with or async for body is compared as code. test_cython_async_block_string_is_code covers both headers.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit dbae4eb. Configure here.

Comment thread ci/ray_ci/doc/check_docstring_only.py Resolved
`async with` and `async for` headers also start with `async`, but their
bodies have no docstring, so the first string in them is code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Oct 30, 2026
@dstrodtman
dstrodtman merged commit 6bd6d29 into master Oct 30, 2026
7 checks passed
@dstrodtman
dstrodtman deleted the djs-261030-docstring-guard-cython branch October 30, 2026 17:58
pseudo-rnd-thoughts pushed a commit that referenced this pull request Nov 3, 2026
…odule, and connector pipeline APIs (#67629)

## Why are these changes needed?

Several public methods and classes in `ray.rllib.algorithms`,
`ray.rllib.core`, and `ray.rllib.connectors` have no docstring, no
parameter entry, or no `Returns:` section. Most of them render on the
RLlib API reference. This PR documents their existing behavior and
changes only docstrings.

What changed:

- **`Algorithm`:** A docstring and `Returns:` for `get_default_config`.
`Args:` and `Returns:` for `get_default_policy_class`. A docstring,
`Args:`, and `Returns:` for `restore_offline_eval_runners`, modeled on
`restore_env_runners`. A docstring and `Args:` for `load_checkpoint`.
- **`Learner` and `LearnerGroup`:** Docstrings for `Learner.node` and
`Learner.device`, and for `LearnerGroup.is_remote` and `is_local`.
`Returns:` for `LearnerGroup.get_stats`, naming both keys.
- **`RLModule`, `RLModuleSpec`, `MultiRLModule`, and
`MultiRLModuleSpec`:** `Returns:` sections for `build`,
`as_multi_rl_module_spec`, `as_multi_rl_module`, `is_stateful`,
`to_dict`, the three `get_*_action_dist_cls` methods, and
`MultiRLModule.items` and `keys`. Also a docstring, `Args:`, and
`Returns:` for `RLModuleSpec.from_module`.
- **Connector pipelines:** Class docstrings for `EnvToModulePipeline`,
`LearnerConnectorPipeline`, and `ModuleToEnvPipeline`.

Two of these correct an existing summary, so they need a closer look:

- `MultiRLModule.items()` said it iterates "over the module IDs", but it
yields (ModuleID, RLModule) pairs. `values()` has the same mistake in
its docstring and in its `ValuesView[ModuleID]` return annotation.
Fixing an annotation is a code change, so both `values()` fixes are in
#67631.
- `RLModule.as_multi_rl_module()` now says the returned MultiRLModule
holds a newly built module rather than this instance. That's because
`MultiRLModule.setup()` calls `RLModuleSpec.build()`.

Rows left out on purpose:

- Methods whose new `Args:` sections would need parameter type hints
first. Ray's pydoclint configuration fails an `Args:` section on an
unannotated signature. These are `RLlibCallback.on_workers_recreated`,
`AgentToModuleMapping`, `Learner.apply`, both `from_dict` methods, and
the two connector `__call__` methods with untyped `**kwargs`. They go in
a separate PR without the label.
- The `ConnectorV2.input_observation_space` and `input_action_space`
setters. Sphinx renders a property from its getter's docstring only.

## Related issue number

None. The work comes from the docs team's inventory of `@PublicAPI`
symbols with missing docstrings. It follows #67626, which covers
`ray.rllib.env`. I searched open PRs for RLlib docstring work and for
these files and found no overlap.

## Checks

- `pre-commit run --files <changed files>`: all hooks pass, including
ruff, black, and pydoclint 0.9.1 with the repo's arguments.
- `python ci/ray_ci/doc/check_docstring_only.py --base <merge-base>`,
with both the guard from this PR's base and the guard as of #67599:
`docstring-only guard: 8 file(s), docstrings only.`
- No `>>>` examples were added or changed.

The commit carries an `@rtd-build` line so Read the Docs builds a
preview.

AI assistance (Claude Code) was used to draft these docstrings. I
reviewed every changed line against the code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
pseudo-rnd-thoughts pushed a commit that referenced this pull request Nov 3, 2026
…atch, and utils APIs (#67630)

## Why are these changes needed?

Several public methods and classes in `ray.rllib.offline`,
`ray.rllib.policy.sample_batch`, and `ray.rllib.utils` have no
docstring, no parameter entry, or no `Returns:` section. This PR
documents their existing behavior and changes only docstrings.

What changed:

- **Offline:**
- A class docstring with `Args:` for `OfflineData`, plus docstrings for
`OfflineData.sample` and its two `default_*_batches_kwargs` properties.
- `Args:` and `Returns:` for `OfflineSingleAgentEnvRunner.sample`,
including when it writes its buffer.
  - `Args:` on the `OfflinePreLearner` class docstring.
  - A docstring for `get_offline_io_resource_bundles`.
  - Docstrings for `IOContext.input_config` and `output_config`.
- **`SampleBatch` and `MultiAgentBatch`:**
  - `Returns:` for `agent_steps` and `env_steps`.
  - A docstring for `is_training` and `Args:` for `set_training`.
  - `Args:` and `Returns:` for `MultiAgentBatch.timeslices`.
- **Utils:**
  - `Returns:` for `MetricsLogger.get_state`.
- `Args:` and `Returns:` for `huber_loss` and for the torch and
TensorFlow versions of `symlog` and `inverse_symlog`.
  - Docstrings for `sample()` on `FlexDict`, `Repeated`, and `Simplex`.

`OfflineData.sample` and `OfflineSingleAgentEnvRunner.sample` have the
most new content and need the closest read. `Repeated.sample()` is
documented as returning 1 to `max_len` items, which is what it does. The
`Repeated` class docstring's example still says "from 0 to 10 boxes",
because `contains()` does accept an empty list.

Rows left out on purpose:

- Methods whose new `Args:` sections would need parameter type hints
first. Ray's pydoclint configuration fails an `Args:` section on an
unannotated signature. These are `MetricsLogger.log_dict`,
`one_hot_multidiscrete`, `FlexDict.assertSpace`, `Repeated.contains`,
`Simplex` and its `contains`, `to_jsonable`, and `from_jsonable`, and
`SampleBatch.get`. They go in a separate PR without the label.
- `SampleBatch.concat_samples` and `MultiAgentBatch.concat_samples`,
which are `@Deprecated(error=True)` stubs.

## Related issue number

None. The work comes from the docs team's inventory of `@PublicAPI`
symbols with missing docstrings. It follows #67626, and its companion
for `algorithms`, `core`, and `connectors` is #67629. I searched open
PRs for RLlib docstring work and for these files and found no overlap.

## Checks

- `pre-commit run --files <changed files>`: all hooks pass, including
ruff, black, and pydoclint 0.9.1 with the repo's arguments.
- `python ci/ray_ci/doc/check_docstring_only.py --base <merge-base>`,
with both the guard from this PR's base and the guard as of #67599:
`docstring-only guard: 13 file(s), docstrings only.`
- No `>>>` examples were added or changed.

The commit carries an `@rtd-build` line so Read the Docs builds a
preview.

AI assistance (Claude Code) was used to draft these docstrings. I
reviewed every changed line against the code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[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