Skip to content

[tune] Correct the Callback and ResultGrid docstring examples - #67623

Merged
dstrodtman merged 2 commits into
masterfrom
doc-1930-tune-example-outputs
Nov 3, 2026
Merged

dstrodtman merged 2 commits into
masterfrom
doc-1930-tune-example-outputs

Conversation

@dstrodtman

@dstrodtman dstrodtman commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR changes what three Tune docstring examples check, and corrects defects that converting them exposed. It needs review from Tune maintainers. It isn't a syntax-only conversion.

The Callback, ResultGrid, and ResultGrid.get_dataframe examples were .. testcode:: blocks, which griffe and MkDocs don't render. Each was paired with a hidden testoutput whose body was only ..., which matches any output, so the doctest ran the code but checked nothing it printed. This PR converts them to >>> doctests that show their output, and makes these corrections:

ResultGrid class example

  • Removes the try/except ValueError around tuner.fit(). It never fires. A trial that raises is recorded in the result grid and doesn't propagate out of fit(), as the "Failure handling" paragraph of Tuner.fit's docstring says. In a run of this example, fit() returned normally with 5 of 10 trials errored. If fit() had raised, the loop that follows would have failed with a NameError on result_grid.
  • Adds the missing space in the success message. The two f-strings concatenated to with metrics{'loss': 0.0, ...}.
  • Passes filter_metric and filter_mode to get_dataframe. The example passed metric= and mode=, which get_dataframe doesn't accept. The line is +SKIP, so nothing caught the TypeError.
  • Normalizes the over-indented print calls in the loop to four spaces.

Callback class docstring

  • Replaces the two TrialRunner references. Tune no longer has a TrialRunner class. Callbacks are passed through RunConfig(callbacks=...), as the example itself does, and TuneController calls them (python/ray/tune/execution/tune_controller.py).
  • The example now shows its Got result: 0 through Got result: 9 lines.

Tuner.fit docstring

  • Corrects the Raises section. It said fit() raises RayTaskError whenever the trainable raises, which contradicts the same docstring's "Failure handling" paragraph. TuneController._trial_task_failure re-raises a trial's exception only when fail_fast is "raise", and otherwise logs it and records it on the trial. I ran a trainable that raises ValueError under each FailureConfig(fail_fast=...) setting. With False and True, fit() returned a ResultGrid with num_errors == 1. With "raise", it raised RayTaskError(ValueError), which is also a ValueError. That's probably where the ResultGrid example's except ValueError came from.

How the output is matched. Tune prints its progress tables to stdout, which a stock doctest compares. The examples use the pattern python/ray/tune/stopper/stopper.py already uses: print("[ignore]"); result_grid = tuner.fit() # doctest: +ELLIPSIS, with [ignore]... as the expected output. In the Callback example, the expected output is [ignore]..., then Got result: 0, ..., Got result: 9, and ..., so the callback's own lines are still checked. The ResultGrid example's trainable fails at random, so its loop's expected output is Trial ....

For reviewers

  • Is the [ignore] pattern acceptable in rendered API reference, or would you prefer RunConfig(verbose=0) in these examples? That changes the example code, so I didn't make it here.
  • The ResultGrid example fails half its trials at random, so its printed output can't be pinned down. It could fail trials based on the config instead, so the output is deterministic. I kept the example's behavior.

Related issues

Related to #66748, which converted Callback's method examples and left this class example, and to #67619, the syntax-only conversion of Tune's remaining never-run examples.

Additional information

Not a duplicate. I searched open PRs for Tune doctest and testcode changes. None touches these docstrings. #67611, which has merged, corrected other Tune docstrings.

Tests run

These ran in a Python 3.12 virtualenv with ray[tune]==2.59.0, with this branch's callback.py and result_grid.py copied over the installed ones. The installed files differ from master only in docstrings.

  • pytest --doctest-modules -p default_doctest_pytest_plugin on both files, using bazel/default_doctest_pytest_plugin.py, the way //python/ray/tune:py_doctest[tune] runs them: 4 passed and 1 skipped, three runs in a row. The skip is get_best_result's existing +SKIP example.
  • To confirm the Callback example checks the callback's output, I changed the expected Got result: 9 to Got result: 99. The doctest failed.
  • ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "3 file(s), docstrings only."
  • pre-commit run --files on all three files: all hooks pass.

The PR carries the docstring-only label, and its commit carries an @rtd-build line for the Read the Docs preview.

AI assistance (Claude Code) was used for this PR.

🤖 Generated with Claude Code

Converts the Callback, ResultGrid, and ResultGrid.get_dataframe examples
from `.. testcode::` to `>>>` doctests. Each paired with a hidden
`testoutput` whose body was only `...`, so they checked nothing about
output. The examples now show what they print. This also fixes the defects
that surfaced:

- ResultGrid: drop the `try`/`except ValueError` around `tuner.fit()`,
  which never fires because a failed trial is recorded in the result
  grid, add the missing space after "with metrics", and pass
  `filter_metric`/`filter_mode` to `get_dataframe`, which has no
  `metric`/`mode` parameters.
- Callback: replace the `TrialRunner` references, a class Tune no longer
  has. Callbacks are passed through `RunConfig(callbacks=...)` and called
  by `TuneController`.

@rtd-build

Signed-off-by: Douglas Strodtman <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dstrodtman dstrodtman added tune Tune-related issues docstring-only Skip heavy library tests for Ray API docstring edits. labels Oct 30, 2026
@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!

The Raises section said fit() raises RayTaskError whenever the trainable raises. It does only when FailureConfig(fail_fast="raise") is set; otherwise the error is recorded in the ResultGrid, as the docstring's own "Failure handling" paragraph says.

@rtd-build

Signed-off-by: Douglas Strodtman <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dstrodtman
dstrodtman marked this pull request as ready for review November 3, 2026 14:27
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 14:27
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Nov 3, 2026

@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 updates the docstrings in callback.py, result_grid.py, and tuner.py to use standard doctest formatting instead of Sphinx testcode/testoutput directives. It also corrects an outdated argument usage in a ResultGrid.get_dataframe example (changing metric/mode to filter_metric/filter_mode) and clarifies the exception-raising behavior of Tuner.fit under different failure configurations. No review comments were provided, and the changes look correct.

@dstrodtman
dstrodtman merged commit 2c1feb4 into master Nov 3, 2026
7 checks passed
@dstrodtman
dstrodtman deleted the doc-1930-tune-example-outputs branch November 3, 2026 15:03
dstrodtman added a commit that referenced this pull request Nov 3, 2026
…ctests (#67619)

## Description

Converts the never-run `.. testcode::` examples in the
`TuneReportCheckpointCallback` docstrings of
`python/ray/tune/integration/lightgbm.py` and
`python/ray/tune/integration/xgboost.py` to `>>>` examples. Sphinx
renders `.. testcode::`, but griffe and MkDocs don't, and stock `>>>`
examples render in both.

Both blocks were `:skipif: True`, so they never ran. They now carry the
docstring-level `# doctest: +SKIP_EXAMPLE` flag (#66753), which skips
the whole docstring in the doctest targets and in the docstring examples
step, so they still never run. The example code is unchanged. The
LightGBM docstring's NumPy-style `Examples` header with a dashed
underline becomes the Google-style `Examples:` header that the rest of
Tune uses.

This is a syntax-only change. The three other remaining Tune `testcode`
blocks, in `tune/callback.py` and `tune/result_grid.py`, pair with a
hidden `testoutput` that matches any output. Converting them changes
what each example checks, so they're in #67623 for Tune review rather
than in this one.

Stacked on #67237, which adds `lightgbm` and `xgboost` to the docstring
examples runner's `OMITTED_LIBRARIES`. Both modules import their library
at module level, and the `docstringbuild` image installs neither.
Without that change, the examples step would fail both modules at
import, rather than skip them.

## Related issues

Related to #66748, #66753, and #67237.

## Additional information

**Not a duplicate.** I searched open PRs for Tune doctest and `testcode`
conversions. None touches these docstrings. #66748, which has merged,
converted the `Trainable` and `Callback` method examples.

**Tests run**

- `ci/ray_ci/doc/check_docstring_only.py --base
upstream/doc-1863-docstring-runner-bundle`: "2 file(s), docstrings
only."
- `ci/ray_ci/doc/run_docstring_examples.py` on both files, in a Python
3.12 virtualenv with `ray[tune]==2.59.0` and neither library installed.
With #67237's runner, both modules are skipped by name: "module needs
lightgbm" and "module needs xgboost," 0 failed. With `master`'s runner,
both fail at import.
- Parsed both docstrings with `doctest.DocTestParser` and compiled every
example: 4 examples in `lightgbm.py` and 6 in `xgboost.py`. Both
docstrings carry `SKIP_EXAMPLE`, so
`bazel/default_doctest_pytest_plugin.py` skips each one whole.
- `pre-commit run --files` on both files: all hooks pass.

The PR carries the `docstring-only` label, and its commit carries an
`@rtd-build` line for the Read the Docs preview.

AI assistance (Claude Code) was used for this PR.

🤖 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

docstring-only Skip heavy library tests for Ray API docstring edits. go add ONLY when ready to merge, run all tests tune Tune-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants