Skip to content

[tune] Improve stackinfo of warn_if_slow - #58359

Open
Daraan wants to merge 3 commits into
ray-project:masterfrom
Daraan:stacklevel-warn_if_slow
Open

Daraan wants to merge 3 commits into
ray-project:masterfrom
Daraan:stacklevel-warn_if_slow

Conversation

@Daraan

@Daraan Daraan commented Nov 2, 2025

Copy link
Copy Markdown
Contributor

Currently warn_if_slow does now show us where in the code the slow function is executed.
The logger message will just point us to: ray/tune/utils/util.py.
This PR adds a stacklevel of 2 to the logging_warning which helps to better trace where in tune the function was actually used.


pre-commit wanted some formatting as well

@Daraan
Daraan requested a review from a team as a code owner November 2, 2025 17:39

@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 improves the stack information for warn_if_slow by adding stacklevel=2 to the logger.warning call. This is a great enhancement for debuggability, as it will now correctly point to the location where warn_if_slow is used, rather than the utility function itself. The accompanying formatting fixes also improve code style. The changes are correct and well-implemented. I've added one suggestion to consider adding a test to verify the new logging behavior.

Comment on lines +201 to +203
logger.warning(
self.message.format(name=self.name, duration=duration), stacklevel=2
)

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

This change to add stacklevel=2 is a great improvement for debuggability. To ensure this behavior is maintained and to prevent future regressions, it would be beneficial to add a unit test. A test could use caplog to capture the warning and assert that the LogRecord's pathname and lineno correctly point to the call site of with warn_if_slow(...).

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.

Low low priority.

@ray-gardener ray-gardener Bot added tune Tune-related issues observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling community-contribution Contributed by the community labels Nov 2, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 17, 2025
@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Nov 17, 2025
@Daraan

Daraan commented Jan 22, 2026

Copy link
Copy Markdown
Contributor Author

Anti Stale comment

@PhilippWillms

Copy link
Copy Markdown

@matthewdeng : This looks quite beneficial, would you take a look, please?

dstrodtman added a commit that referenced this pull request Oct 30, 2026
…67610)

## Summary

Adds the missing docstrings, `Args:` entries, and `Returns:` sections on
public Ray Tune symbols in `search`, `stopper`, `trainable`, `context`,
`utils`, `execution`, and `logger`. Each describes existing behavior,
written from the code. It follows #67608, which covers
`ray.tune.schedulers`.

Stacked on #67608: this PR targets that branch so the correctness PR
above it can build on both, and retargets to `master` once #67608
merges. The two PRs share no files.

## Before

Across these subpackages, 23 public symbols had no docstring, 43
parameters had no `Args:` entry, and 14 callables that annotate a return
type had no `Returns:` section. The sampling functions such as
`tune.uniform`, `tune.quniform`, and `tune.randint` described their
bounds in prose but had no `Args:` block.

## After

19 of the 23 docstrings, 35 of the 43 parameters, and 10 of the 14
`Returns:` sections are documented. Where a base class lacked the
docstring its overrides inherit, this PR documents the base instead of
each override: `Searcher.get_state`, `Searcher.set_state`,
`Stopper.__call__`, and `Stopper.stop_all`. That covers both
`ConcurrencyLimiter` classes and `Repeater`, and `CombinedStopper`.

Behavior is unchanged. The diff touches only docstrings, which the guard
below confirms.

## Not in this PR

- **Four methods with untyped signatures:**
`BasicVariantGenerator.set_state` and `save_to_dir`,
`FunctionStopper.is_valid_function`, and `Trainable.reset`. pydoclint,
run with `--arg-type-hints-in-signature`, rejects an `Args:` section on
a signature without type hints, so documenting these 6 parameters needs
a signature change. That signature change is in #67612, since this PR is
docstring-only. `BasicVariantGenerator.set_state` and
`FunctionStopper.is_valid_function` get a docstring here, without
sections.
- **`TuneContext.get_trial_name`, `get_trial_id`, `get_trial_resources`,
and `get_trial_dir`:** these take their docstrings at runtime from
`ray.train._internal.session` through `@_copy_doc`, so they render
documented already. A docstring written on the method would be
overwritten. Their `Returns:` sections belong on the `session`
functions, which Ray Train owns.
- **`Repeater.on_trial_complete`:** #67013 changes this method and edits
the same docstring, so its `Args:` entries are left to land after that
PR.

## For reviewers

These docstrings are worth a close read:

- `Verbosity` documents the four levels as the `verbose` argument of
`tune.run` describes the legacy output, and notes that the default
output treats levels 2 and 3 the same (`get_air_verbosity` in
`tune/experimental/output.py`).
- `PlacementGroupFactory.__call__` is deprecated. Its new docstring says
so and points to `to_placement_group()`.
- `BasicVariantGenerator.get_state` documents that it returns `False`
instead of a state when any variants are generated lazily.

## Duplicate check

No open PR documents these symbols. Open PRs #66923 (`stopper.py`),
#66699 (`logger/json.py`), and #58359 (`utils/util.py`) change code in
files this PR touches, and all three apply cleanly on top of this
branch. #67013 (`repeater.py`) also applies cleanly, because this PR
leaves the method it changes alone.

## Testing

- `python ci/ray_ci/doc/check_docstring_only.py --base "$(git merge-base
upstream/master HEAD)"`: `docstring-only guard: 15 file(s), docstrings
only.`
- The full pre-commit suite on the changed files, including ruff, black,
pydoclint, and the docstyle hook: passed.
- `ci/ray_ci/doc/api_param_coverage.py`, run over `python/ray/tune/`
outside `schedulers/`: 43 undocumented parameters on `master`, 8 on this
branch: 6 on the four untyped signatures and 2 on
`Repeater.on_trial_complete`, all listed above.
- The commit carries an `@rtd-build` line, so the Read the Docs preview
renders the changed docstrings.

AI assistance (Claude Code) was used to prepare 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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling tune Tune-related issues unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants