Skip to content

[data] Convert the write_sql and write_lance docstring examples to stock doctests - #67238

Merged
dstrodtman merged 1 commit into
masterfrom
doc-1862-data-write-sql-lance-doctests
Nov 3, 2026
Merged

dstrodtman merged 1 commit into
masterfrom
doc-1862-data-write-sql-lance-doctests

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Description

Converts the last three .. testcode:: blocks in python/ray/data/dataset.py to stock >>> doctests, in Dataset.write_sql and Dataset.write_lance. After this, dataset.py has no testcode left. The change touches only docstrings.

write_sql follows read_sql's pattern. The example created example.db in the working directory, and a hidden :hide: testcode block deleted it afterward. Stock doctests have no hidden-cleanup equivalent, so the example now creates the database in a tempfile.TemporaryDirectory() and calls cleanup() at the end, the way ray.data.read_sql's example does.

write_lance was malformed. Its .. testcode:: directive had no blank line before its body. The example also wrote to a fixed /tmp/lance_data/ path. It now writes to a temporary directory too. It needs lance, which the docstring examples image doesn't install, so the examples step reports it as skipped by name, and //python/ray/data:doctest, whose image installs pylance, runs it.

Stacked on #67237, which adds lance to the runner's OMITTED_LIBRARIES. Without that change, the examples step would fail on write_lance rather than skip it.

Related issues

Related to #67202, #67207, #67209, and #67237.

Additional information

Not a duplicate. I searched open PRs for write_sql and write_lance. None converts these examples. #67202, #67207, and #67209 converted the rest of dataset.py and have merged.

Tests run

  • ci/ray_ci/doc/check_docstring_only.py --base upstream/doc-1863-docstring-runner-bundle: "1 file(s), docstrings only."
  • ci/ray_ci/doc/run_docstring_examples.py python/ray/data/dataset.py in an aarch64 image built from python/deplocks/docs/docstring_examples_depset_py3.10.lock, with the Ray nightly at 093faf5583e and this branch's dataset.py mounted over the installed one. Result: 64 docstrings run and 0 failed. write_sql passes, and write_lance is skipped for lance.
  • The same run with pylance==4.0.0 installed, the version Ray Data's CI pins: 65 docstrings run, 0 failed, and write_lance passes. Neither example leaves files in /tmp.
  • pre-commit run --files python/ray/data/dataset.py: 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

@dstrodtman
dstrodtman requested a review from a team as a code owner October 21, 2026 13:21
@dstrodtman dstrodtman added data Ray Data-related issues go add ONLY when ready to merge, run all tests docstring-only Skip heavy library tests for Ray API docstring edits. labels Oct 21, 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 documentation examples for write_sql and write_lance in python/ray/data/dataset.py to use doctest-style syntax and temporary directories instead of hardcoded local files. The feedback suggests using with tempfile.TemporaryDirectory() context managers rather than manual instantiation and cleanup to ensure temporary resources are properly cleaned up even if exceptions occur during doctest execution.

Comment thread python/ray/data/dataset.py Resolved
Comment thread python/ray/data/dataset.py Resolved
@dstrodtman
dstrodtman added this pull request to stack #67289 October 22, 2026 16:38
@dstrodtman
dstrodtman requested review from a team as code owners October 28, 2026 20:53
dstrodtman added a commit that referenced this pull request Oct 29, 2026
…blocks (#67432)

## Description

Converts the last `>>>` doctest examples on documentation pages, so
`>>>` examples remain only in docstrings and on the two pages that teach
the docstring format. #67429 changes the contributor guidance to match.
This PR is independent of that stack and targets `master`.

Before this PR, seven pages under `doc/source/` had `>>>` prompts. Two
of them teach the docstring format and stay as they are:
`ray-contribute/writing-code-snippets.md` and
`ray-contribute/getting-involved.md`. This PR converts the other five:

| Page | Before | After | Test coverage |
|---|---|---|---|
| `data/working-with-tensors.md` | Three `{doctest}` blocks |
`{testcode}` and `{testoutput}` blocks | Unchanged. The `dtype` and
shape checks still run. The two per-image shapes the doctest skipped are
now a `+MOCK` sample. |
| `ray-references/glossary.md` | One `{doctest}` block with `+SKIP` on
every line | `{testcode}` and `{testoutput}` | Gained. The block used to
run nothing and now runs. The expected output is unchanged, because Ray
produces it exactly. |
| `serve/advanced-guides/multi-app-container.md` | `>>>` prompts in a
`python` fence, with every request skipped | Plain `python` and `text`
blocks | None before or after. The requests need the containerized Serve
apps running. The doctest only ran `import requests` and two string
assignments. |
| `core/tasks/nested-tasks.md` | REPL transcript in a `bash` block |
Plain `python` and `text` blocks | None before or after. The page's
`doc_code/` script is tested. |
| `serve/production-guide/fault-tolerance.md` | Interactive `$ python` /
`>>>` transcripts | Plain `python` blocks, with `text` blocks for output
| None before or after. These steps kill actors on a live cluster. |

### Test wiring

- `doc/BUILD.bazel`:
- Removes the Serve `doctest_each` call. Its only file was
`multi-app-container.md`, which no longer has a runnable example, so the
target would pass without testing anything.
- Drops the comment explaining why `nested-tasks.md` isn't listed, since
the page no longer has anything runnable to explain.
- The glossary and tensors entries stay, because both pages still have
runnable examples.
- `doc/test_prose_doctest_targets.py`: removes `nested-tasks.md` and
`fault-tolerance.md` from `UNTESTED`. Neither page has a runnable
example now. The two docstring-teaching pages stay exempt.

No `.buildkite/test.rules.txt` change. All prose routes to `doc_example`
whether or not a doctest target names the page, and the existing routing
cases for these pages still hold.

This PR edits `doc/BUILD.bazel`, so premerge runs every library's docs
example step. The glossary target is tagged `team:none`, which runs only
post-merge or with `docs-example-test`, so this PR carries that label
too.

## Related issues

Companion to #67429, which reserves `>>>` doctest-style examples for
docstrings in the contributor guide.

## Additional information

### Duplicate check

I searched open PRs for each converted page and for `doctest` in titles.
#66705 edits `fault-tolerance.md` around line 53, far from these hunks.
Draft #66851 appends a line to `nested-tasks.md` after these hunks.
Neither converts these examples. The docstring conversion PRs (#67238,
#66745, #67237) touch `python/`, not pages.

### Testing

I reproduced the CI doctest runner locally: Ray 2.59.0, Python 3.11,
`pytest==7.4.4`, `ray-project/pytest-sphinx@c1ab2b33`, the Ray Data
`doctest_pytest_plugin.py`, and the macro's `--doctest-modules
--doctest-glob='*.md' -c NO_PYTEST_CONFIG` arguments.

- `working-with-tensors.md`: passes before (2 items: the doctest blocks
and the testcode blocks) and after (1 item). A copy with a wrong
expected shape fails with `Expected (32, 28, 99) / Got (32, 28, 28)`, so
the new assertions are live.
- `glossary.md`: before, 1 skipped. After, 1 passed. A copy with a wrong
expected value fails.
- `python doc/test_prose_doctest_targets.py`: "73 listed pages; every
runnable page is accounted for."
- `pre-commit run --files` on the changed files passes, including
buildifier and buildifier-lint.
- `vale` on each changed page: no new alerts compared with `master`.
- After this PR, `git grep -l '^\s*>>> '` over `doc/source/*.md` and
`*.rst` returns only the two pages that teach the docstring format.
- The Read the Docs preview covers rendering.

This PR was prepared with AI assistance (Claude Code). I reviewed every
changed line.

🤖 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]>
@dstrodtman
dstrodtman force-pushed the doc-1862-data-write-sql-lance-doctests branch from 8e51870 to bc9ec38 Compare October 29, 2026 20:42
dstrodtman added a commit that referenced this pull request Oct 30, 2026
…API (#67605)

## Summary

Adds the missing docstrings, `Args:` entries, and `Returns:` sections on
public Ray Data symbols in `aggregate`, `dataset`, `iterator`,
`catalog`, `datasource`, `datatype`, and the public classes under
`_internal`: the Databricks and LeRobot datasources and the object and
tensor extensions. Each describes existing behavior, written from the
code. It follows #67603, which covers the preprocessors.

## Before

These symbols had no docstring, or their docstring had no section for
some parameters or for the return value. `ApproximateQuantile` and
`ApproximateTopK` kept their whole docstring on `__init__`, and the
class reference page doesn't render that, so [their
pages](https://docs.ray.io/en/master/data/api/doc/ray.data.aggregate.ApproximateQuantile.html)
show no description.

## After

The symbols are documented. The `ApproximateQuantile` and
`ApproximateTopK` docstrings move, unchanged, from `__init__` to the
class, so their pages show the description, examples, and arguments, and
`__init__` drops out of their method tables.

Where a base class lacked the docstring an override would inherit, this
PR documents the base instead of each override: `Datasource.get_name`,
`Datasource.estimate_inmemory_data_size`, `AggregateFn.output_field`,
and `Catalog.resolve`.

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

## Subsystems

API, IO, shuffle and aggregation, block and data format. Docstrings
only.

## For reviewers

These docstrings state behavior that looks unintended, so they're the
ones worth a close read:

- `PythonObjectArray.to_numpy` ignores `dtype`.
- `ArrowPythonObjectArray.to_numpy` ignores `zero_copy_only` and
`writable`.
- `Dataset.__init__` now documents its arguments and keeps the note that
the constructor isn't part of the public API.

Three methods can't be documented without a signature change, so their
code fixes and docstrings are in #67602:
`ArrowPythonObjectScalar.as_py`, `ArrowTensorScalar.as_py`, and
`ArrowPythonObjectArray.from_objects`. The `ArrowTensorScalar` class
docstring sits directly above one of those signatures, so it's in #67602
too. #67602 also corrects the return annotation of `TensorArray.isna`,
which this PR documents as returning a boolean NumPy array.

## Duplicate check

No open PR documents these symbols. Open PRs #67600, #66927, #67238, and
#67237 change other parts of `dataset.py`, and none of them edit the
docstrings changed here.

## 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.
- 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]>
@dstrodtman
dstrodtman force-pushed the doc-1862-data-write-sql-lance-doctests branch from bc9ec38 to d1c524c Compare November 2, 2026 21:00
Base automatically changed from doc-1863-docstring-runner-bundle to master November 3, 2026 11:49
…ock doctests

write_sql follows read_sql's pattern: it creates the SQLite database in
a temporary directory and cleans it up in the example, in place of the
hidden testcode block that deleted example.db from the working
directory.

write_lance's testcode directive had no blank line before its body and
wrote to a fixed /tmp path. It now writes to a temporary directory too.

These were the last testcode blocks in dataset.py.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
dstrodtman added a commit that referenced this pull request Nov 3, 2026
…ttings in the examples runner (#67237)

## Description

Follow-ups to the `docstring-only` label (#66814, #67083) after its
first real campaign PRs on Ray Data. They're bundled into one PR because
they share files and all need ray-ci review. Stacked on #67204, which
updates the contributor docs this PR edits.


1. **Skip polars examples by name instead of marking them render-only.**
`Dataset.join` needs `polars`, which the light `docstringbuild` image
doesn't install, so #67202 marked the docstring `+SKIP_EXAMPLE`. That
also stopped it from running in `//python/ray/data:doctest`, whose image
installs polars through `test-requirements.txt`. This PR adds `polars`
to `OMITTED_LIBRARIES` and removes the flag. The examples step now
reports `SKIP Dataset.join (needs polars)`, and Data's target runs the
three `join` examples.
- Polars isn't added to the image. `polars==1.40.0` plus
`polars-runtime-32==1.40.0` is a 57 MB x86_64 wheel and about 195 MB
installed, larger than pyarrow at 136 MB, for one docstring. Every
current extra is 21 MB or less.
- **lance, the same way.** `Dataset.write_lance` needs `lance`, from the
`pylance` distribution. It's about 138 MB installed on Linux aarch64,
for one docstring, and Data's doctest image installs it through
`data-test-requirements.txt`. This PR adds `lance` to
`OMITTED_LIBRARIES`, so the examples step reports `SKIP
Dataset.write_lance (needs lance)`. #67238, stacked on this PR, converts
that docstring and `write_sql`'s from `testcode` to `>>>`.
- **LightGBM and XGBoost, the same way.**
`ray.tune.integration.lightgbm` and `.xgboost`, and Ray Train's LightGBM
and XGBoost modules, import these at module level. The image installs
neither, so a `docstring-only` change to one of those modules failed the
examples step at import, even though Tune's examples there are
render-only (`+SKIP_EXAMPLE`) and Train's are still `testcode`. The ML
images install both through
`python/requirements/ml/core-requirements.txt`, which meets the omit
rule in item 2. The step now reports `SKIP
python/ray/tune/integration/lightgbm.py (module needs lightgbm)`, and
likewise for `xgboost`. #67619, stacked on this PR, converts those two
Tune examples.
2. **Write the inclusion rule into
`docstring-examples-requirements.txt`.** The header now states when to
add a library to the image and when to omit it by name. Each entry names
the APIs whose examples need it, so it can go once those stop needing
it. Comment-only. `raydepsets --check` reports the locks unchanged.
3. **Apply Ray Data's doctest settings in the runner.**
`doctest_pytest_plugin.py` sets `preserve_order` and turns off
`print_on_execution_start` for every Data docstring. The runner applied
neither, so an order-dependent example could pass in its target and
flake in the step. It now applies both to `ray.data` modules and
restores them after each docstring.
4. **`ml: data integration tests` under the label.** This PR originally
added a negated `if:` gate to it. #67341 merged the `docstring-only`
keep-only rule in `.buildkite/docs.labels.yaml`, which drops this step
with every other library test step, so the gate is removed and
`ml.rayci.yml` is unchanged.
5. **Count the output a render-only docstring hides.** `+SKIP_EXAMPLE`
skips a whole docstring, so expected output inside it is never checked.
The skip line now names how many such examples there are. Across
`python/ray` at this commit, 26 docstrings carry the flag, and only
`Dataset.join` had expected output, which item 1 makes runnable. So the
count is a signal rather than a failure: a render-only example can
legitimately show sample output, such as for an external service.
6. **`check_docstring_only.py --base` diffs from the merge base.** It
diffed against the ref literally. After master moved past the branch
point, `--base upstream/master` flagged files the PR never touched. The
CI path, which already used the merge base, is unchanged. This matches
`run_docstring_examples.py`, and the `ci.md` instructions drop the
manual `git merge-base` wrapper.
7. **Stale comments.** The `doc_example` prose rule's comment in
`test.rules.txt` said an unlabeled prose PR still builds the opt-in
step's images. Since #67341, the `docs-example-test` label rule drops
the step before dependency marking, so the comment now says that. The
`doc: doctest` comment in `others.rayci.yml` still called
`//doc:doctest` the catch-all target, but #67003 split it into per-page
`team:none` targets. The `docstring examples` comment now names the
omitted libraries. It also said Buildkite drops the step on an unlabeled
PR that selects it, but Buildkite shows it as `broken`, and that the
skipped Ray Data examples run in "postmerge", which now says the
scheduled postmerge builds, since the build each merge starts doesn't
run them.

## Related issues

Related to #66814, #67083, #67202, and #67204. #67238 and #67619 are
stacked on this PR.

## Additional information

**Not a duplicate.** I searched open PRs for `run_docstring_examples`,
`check_docstring_only`, `docstring-only`, `OMITTED_LIBRARIES`, and `data
integration tests`. Only #67204, which this stacks on, touches these
files, and it changes docs only.

**Tests run**

- `bazel test //ci/ray_ci/doc:test_run_docstring_examples
//ci/ray_ci/doc:test_check_docstring_only`: both pass. New cases cover:
  - The `Dataset.join` polars message.
  - The hidden-output count.
- The Data settings, applied and restored, and left alone for other
modules.
  - `--base` against a base that moved past the branch point.
- `missing_library` for `lightgbm` and `xgboost` module-level imports.
For that commit I ran the suite with `pytest` directly rather than
through Bazel: 33 passed.
- `run_docstring_examples.py` on `dataset.py`, `aggregate.py`, and
`read_api.py`. I ran it in an aarch64 image built from
`docstring_examples_depset_py3.10.lock`, without `decord` (no aarch64
wheel) and `ray-haproxy`, with the Ray nightly at `093faf5583e`.
  - 114 docstrings ran, and `Dataset.join` skipped for polars.
- The one failure is `read_videos`, which needs decord. The x86_64 CI
image installs it.
- With polars installed, `Dataset.join` passes, and 115 docstrings run.
- With the Data settings, `Starting execution of Dataset` lines drop to
zero, and all 18 `aggregate.py` docstrings pass.
- `run_docstring_examples.py` on `ray.tune.integration.lightgbm` and
`.xgboost` from #67619, in a Python 3.12 virtualenv with
`ray[tune]==2.59.0` and neither library installed: both modules skipped
by name, 0 failed. Before this change, both failed at import.
- `bazel run //ci/raydepsets:raydepsets -- build
ci/raydepsets/configs/docs.depsets.yaml --check`: lock files are up to
date.
- `pre-commit run` on the changed files: passes.
- `rayci` v0.50.0 pipeline generation for a labeled Data docstring PR on
this branch, as above.

This PR's own premerge runs the Data and ML suites, because it changes
`dataset.py`. That run is also the check that Data's doctest image has
polars.

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]>
@dstrodtman
dstrodtman force-pushed the doc-1862-data-write-sql-lance-doctests branch from d1c524c to 7d1a729 Compare November 3, 2026 11:49
@dstrodtman
dstrodtman merged commit 1a39e71 into master Nov 3, 2026
5 of 6 checks passed
@dstrodtman
dstrodtman deleted the doc-1862-data-write-sql-lance-doctests branch November 3, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants