Skip to content

[rllib] Add type hints and docstrings to untyped public RLlib methods - #67631

Merged
dstrodtman merged 4 commits into
masterfrom
doc-1914-rllib-typed-docstrings
Nov 3, 2026
Merged

dstrodtman merged 4 commits into
masterfrom
doc-1914-rllib-typed-docstrings

Conversation

@dstrodtman

@dstrodtman dstrodtman commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

These public RLlib methods and classes had no docstring, or no Args: or Returns: section, and their signatures had no type hints. Ray runs pydoclint with --arg-type-hints-in-signature, which fails an Args: section on an unannotated signature. So documenting them means adding parameter hints too, and they can't go in a docstring-only PR the way #67629 and #67630 do. This PR adds both. It's the companion to those two.

What changed:

  • RLlibCallback.on_workers_recreated: Type hints and a docstring. It marks the hook as deprecated in favor of on_env_runners_recreated(), as the existing source comment says, and notes that Algorithm still calls it.
  • AgentToModuleMapping: A hint for agent_to_module_mapping_fn, using the existing AgentToModuleMappingFn alias, and Args: for all constructor arguments, including what as_learner_connector changes.
  • ConnectorPipelineV2.__call__ and NormalizeAndClipActions.__call__: **kwargs: Any, plus Args: and Returns: sections.
  • Learner.apply: Hints and a docstring.
  • RLModuleSpec.from_dict and MultiRLModuleSpec.from_dict: d: Dict[str, Any], plus Args: and Returns: sections.
  • MetricsLogger.log_dict: value_dict: Dict[str, Any] and an Args: section.
  • one_hot_multidiscrete: Hints and a docstring.
  • FlexDict.assertSpace, Repeated.contains, Simplex, and Simplex.contains, to_jsonable, and from_jsonable: Hints and docstrings.
  • SampleBatch.get: Hints and Args: and Returns: sections.

Apart from the one_hot_multidiscrete and AgentToModuleMapping fixes below, nothing changes at runtime. Stripping docstrings, annotations, and imports, the AST of every changed file except utils/numpy.py matches the base commit.

Three fixes to existing mistakes

  • one_hot_multidiscrete(x, depths=List[int]) made the typing.List[int] object the default value of depths instead of its annotation. This PR makes it depths: List[int]. A call that left out depths already raised TypeError when the function iterated the default, so this only changes the error such a call gets.
  • MultiRLModule.values() returns self._rl_modules.values(), a view of RLModules, but it was annotated ValuesView[ModuleID] and its summary said it iterates over module IDs. This PR corrects both. The docstring fix moved here from [rllib] Add missing docstrings and Returns to Algorithm, Learner, RLModule, and connector pipeline APIs #67629, so the two PRs don't edit adjacent lines.
  • The AgentToModuleMapping docstring example returned {} instead of the output its check() expects. Without as_learner_connector=True, the connector keeps only the agents that act next in episodes, and the example passes episodes=[]. The example now passes as_learner_connector=True and runs as written.

Related issue number

None. The work comes from the docs team's inventory of @PublicAPI symbols with missing docstrings, and it completes the RLlib list with #67626, #67628, #67629, and #67630. I searched open PRs for these files and for RLlib docstring work 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.
  • pytest against this branch's rllib on the current nightly wheel, covering test_metrics_logger.py, test_serialization.py, test_sample_batch.py, test_multi_agent_batch.py, and test_multi_agent_env_runner.py: 63 passed and 2 failed. The failures are TestSampleBatch::test_count and test_interceptors, and they fail the same way without this PR's changes. They look like a mismatch with the NumPy 2.5 in my local environment.
  • A smoke test of every changed symbol: one_hot_multidiscrete with numpy and torch inputs, Simplex sample, contains, and the JSON round trip, Repeated.contains, FlexDict.sample, SampleBatch.get, MetricsLogger.log_dict with a key prefix, Learner.apply, and on_workers_recreated.
  • The fixed AgentToModuleMapping docstring example, run as written: passes.

AI assistance (Claude Code) was used to draft the docstrings and type hints. I reviewed every changed line.

🤖 Generated with Claude Code

Document public RLlib methods and classes whose signatures had no type
hints, which Ray's pydoclint configuration needs before it accepts an Args
section. Add the parameter hints along with the docstrings.

Also fix two existing mistakes: one_hot_multidiscrete declared
`depths=List[int]`, which made the typing object the default instead of an
annotation, and the AgentToModuleMapping docstring example returned an
empty dict because it didn't pass as_learner_connector=True.

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

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

values() returns self._rl_modules.values(), a view of RLModules, but was
annotated ValuesView[ModuleID] and described as a view of module IDs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@ray-gardener ray-gardener Bot added rllib RLlib related issues docs An issue or change related to documentation labels Oct 30, 2026
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Nov 3, 2026
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
pseudo-rnd-thoughts enabled auto-merge (squash) November 3, 2026 14:56
@dstrodtman dstrodtman added the skip-gpu-tests Skips GPU tests for PRs that trigger them due to overly broad rules. label Nov 3, 2026
@github-actions
github-actions Bot disabled auto-merge November 3, 2026 17:12
@dstrodtman
dstrodtman merged commit a62110c into master Nov 3, 2026
6 checks passed
@dstrodtman
dstrodtman deleted the doc-1914-rllib-typed-docstrings branch November 3, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs An issue or change related to documentation go add ONLY when ready to merge, run all tests rllib RLlib related issues skip-gpu-tests Skips GPU tests for PRs that trigger them due to overly broad rules.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants