Repository navigation
[tune] Correct the Callback and ResultGrid docstring examples - #67623
Merged
Merged
Conversation
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]>
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]>
Contributor
There was a problem hiding this comment.
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.
pseudo-rnd-thoughts
approved these changes
Nov 3, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andResultGrid.get_dataframeexamples were.. testcode::blocks, which griffe and MkDocs don't render. Each was paired with a hiddentestoutputwhose 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:ResultGridclass exampletry/except ValueErroraroundtuner.fit(). It never fires. A trial that raises is recorded in the result grid and doesn't propagate out offit(), as the "Failure handling" paragraph ofTuner.fit's docstring says. In a run of this example,fit()returned normally with 5 of 10 trials errored. Iffit()had raised, the loop that follows would have failed with aNameErroronresult_grid.with metrics{'loss': 0.0, ...}.filter_metricandfilter_modetoget_dataframe. The example passedmetric=andmode=, whichget_dataframedoesn't accept. The line is+SKIP, so nothing caught theTypeError.printcalls in the loop to four spaces.Callbackclass docstringTrialRunnerreferences. Tune no longer has aTrialRunnerclass. Callbacks are passed throughRunConfig(callbacks=...), as the example itself does, andTuneControllercalls them (python/ray/tune/execution/tune_controller.py).Got result: 0throughGot result: 9lines.Tuner.fitdocstringRaisessection. It saidfit()raisesRayTaskErrorwhenever the trainable raises, which contradicts the same docstring's "Failure handling" paragraph.TuneController._trial_task_failurere-raises a trial's exception only whenfail_fastis"raise", and otherwise logs it and records it on the trial. I ran a trainable that raisesValueErrorunder eachFailureConfig(fail_fast=...)setting. WithFalseandTrue,fit()returned aResultGridwithnum_errors == 1. With"raise", it raisedRayTaskError(ValueError), which is also aValueError. That's probably where theResultGridexample'sexcept ValueErrorcame 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.pyalready uses:print("[ignore]"); result_grid = tuner.fit() # doctest: +ELLIPSIS, with[ignore]...as the expected output. In theCallbackexample, the expected output is[ignore]..., thenGot result: 0,...,Got result: 9, and..., so the callback's own lines are still checked. TheResultGridexample's trainable fails at random, so its loop's expected output isTrial ....For reviewers
[ignore]pattern acceptable in rendered API reference, or would you preferRunConfig(verbose=0)in these examples? That changes the example code, so I didn't make it here.ResultGridexample 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
testcodechanges. 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'scallback.pyandresult_grid.pycopied over the installed ones. The installed files differ frommasteronly in docstrings.pytest --doctest-modules -p default_doctest_pytest_pluginon both files, usingbazel/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 isget_best_result's existing+SKIPexample.Callbackexample checks the callback's output, I changed the expectedGot result: 9toGot result: 99. The doctest failed.ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "3 file(s), docstrings only."pre-commit run --fileson all three files: all hooks pass.The PR carries the
docstring-onlylabel, and its commit carries an@rtd-buildline for the Read the Docs preview.AI assistance (Claude Code) was used for this PR.
🤖 Generated with Claude Code