Repository navigation
[data] Convert Dataset iteration, split, and introspection docstring examples to stock doctests - #67207
Conversation
…examples to stock doctests Convert the testcode blocks in Dataset.streaming_split, split, iter_batches, iter_jax_batches, iter_tf_batches, stats, explain, serialize_lineage, and deserialize_lineage to stock >>> doctests, plus the examples in SerializablePreprocessorBase._set_serializable_fields and MemoryProfiler. Output that +MOCK used to hide is now compared, so iter_batches reads the simple image dataset from S3 instead of an example:// path that installed wheels don't ship, and stats and MemoryProfiler use ellipses for timing and memory values. The blocking streaming_split call stays render-only with +SKIP. @rtd-build Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request converts Sphinx .. testcode:: blocks to standard Python doctest format (>>>) across several files, including util.py, dataset.py, and preprocessor.py, to improve docstring examples. A review comment points out that the example for iter_tf_batches in dataset.py incorrectly demonstrates ds.to_tf instead of ds.iter_tf_batches, and suggests updating it to use iter_tf_batches directly.
| >>> import ray | ||
| >>> ds = ray.data.read_csv("s3://anonymous@air-example-data/iris.csv") | ||
| >>> tf_dataset = ds.to_tf( | ||
| ... feature_columns="sepal length (cm)", | ||
| ... label_columns="target", | ||
| ... batch_size=2 | ||
| ... ) | ||
| >>> for features, labels in tf_dataset: | ||
| ... print(features, labels) | ||
| tf.Tensor([5.1 4.9], shape=(2,), dtype=float64) tf.Tensor([0 0], shape=(2,), dtype=int64) | ||
| ... | ||
| tf.Tensor([6.2 5.9], shape=(2,), dtype=float64) tf.Tensor([2 2], shape=(2,), dtype=int64) |
There was a problem hiding this comment.
The docstring example for iter_tf_batches is currently demonstrating ds.to_tf instead of ds.iter_tf_batches. This is misleading for users looking at the documentation for iter_tf_batches. We should update the example to use iter_tf_batches directly, which also aligns it with the iter_jax_batches example above.
| >>> import ray | |
| >>> ds = ray.data.read_csv("s3://anonymous@air-example-data/iris.csv") | |
| >>> tf_dataset = ds.to_tf( | |
| ... feature_columns="sepal length (cm)", | |
| ... label_columns="target", | |
| ... batch_size=2 | |
| ... ) | |
| >>> for features, labels in tf_dataset: | |
| ... print(features, labels) | |
| tf.Tensor([5.1 4.9], shape=(2,), dtype=float64) tf.Tensor([0 0], shape=(2,), dtype=int64) | |
| ... | |
| tf.Tensor([6.2 5.9], shape=(2,), dtype=float64) tf.Tensor([2 2], shape=(2,), dtype=int64) | |
| >>> import ray | |
| >>> ds = ray.data.read_csv("s3://anonymous@air-example-data/iris.csv") | |
| >>> tf_dataset = ds.iter_tf_batches(batch_size=2) | |
| >>> for batch in tf_dataset: | |
| ... print(batch["sepal length (cm)"], batch["target"]) | |
| ... break | |
| tf.Tensor([5.1 4.9], shape=(2,), dtype=float64) tf.Tensor([0 0], shape=(2,), dtype=int64) |
There was a problem hiding this comment.
Applied in 1bf8d06. The example now calls iter_tf_batches and passes with TensorFlow installed.
The iter_tf_batches example called Dataset.to_tf, which has its own examples. It now calls iter_tf_batches and prints the first batch, like the iter_jax_batches example. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
…EXAMPLE doctests (#67209) ## Description Converts the never-run `.. testcode::` / `:skipif: True` examples for Ray Data's external-service sinks and the Hive reader to `>>>` doctests. Each docstring is flagged once with the docstring-level `# doctest: +SKIP_EXAMPLE` from #66753. The change touches only docstrings: - **`python/ray/data/dataset.py`**: 10 blocks in `write_iceberg`, `write_delta`, `write_webdataset`, `write_snowflake`, `write_mongo`, `write_bigquery`, `write_clickhouse`, `write_turbopuffer` (2 blocks), and `write_kafka`. - **`python/ray/data/read_api.py`**: the `read_hive` example. - **`python/ray/data/_internal/datasource/turbopuffer_datasink.py`**: the 3 `TurbopufferDatasink` examples. Each example needs a service, credentials, or a bucket that CI doesn't have, so they stay render-only, as before. The flag is stripped at render time by both Sphinx (`trim_doctest_flags`) and griffe. The conversion follows the rules from #67080: same code in the same order, blank lines inside a block dropped, and top-level comments as `>>> #` lines. Two docstrings needed something other than the flag: - **The `Dataset` class docstring runs its example now.** That docstring also holds 10 runnable `>>>` examples, and `+SKIP_EXAMPLE` skips a whole docstring, so it would have stopped them from running. Instead, only the two lines that use placeholder paths, `read_parquet("s3://bucket/path")` and `write_csv("s3://bucket/output")`, carry `# doctest: +SKIP`. The `range` and `from_items` lines run. - **`write_csv`'s example had an unterminated string**, `"s3://bucket/folder/)`. It was already `+SKIP`, so nothing ever parsed it. The closing quote is now in place, so the docstring parses with the stock doctest parser. The `:skipif: True` blocks left in `dataset.py` after this PR are in #67202 (`join`) and #67207 (`streaming_split`). ## Related issues Related to #66753, #67080, #67202, and #67207. ## Additional information **Not a duplicate.** I searched open PRs for each method name, `turbopuffer`, `SKIP_EXAMPLE`, and `testcode skipif data`. #67202 and #67207 are my own and convert other methods in `dataset.py`. Two open PRs edit text these examples sit next to, so whichever PR lands second needs a small rebase: - #66192 edits the `write_iceberg` example's content, adding a REST catalog write. This PR changes only the example's form. - #66308 adds a deprecation notice to `write_clickhouse`, next to its example. #67114 and #67168 change `read_api.py` but not the `read_hive` docstring example. **Tests run** - `ci/ray_ci/doc/check_docstring_only.py --base upstream/master`: "3 file(s), docstrings only." - `ci/ray_ci/doc/run_docstring_examples.py` on the three files, in an aarch64 image with the Ray nightly at `093faf5583e` and the changed files mounted over the installed ones. None of the three files changed between that commit and this PR's base. Result: 85 docstrings run and 1 failed, with 16 skipped for `+SKIP_EXAMPLE` and 5 for an omitted library (torch, torchvision, TensorFlow, `tensorflow_datasets`). All 13 converted docstrings report `SKIP (+SKIP_EXAMPLE)`, and `Dataset` and `Dataset.write_csv` pass. The one failure is `read_videos`, which this PR doesn't touch. It needs `decord`, which has no aarch64 wheel. The run installed `datasketches`, `Pillow`, `crc32c`, `soundfile`, and `fastavro`, which the base image lacks. Without them, six more untouched docstrings fail on the missing import. - Every `>>>` example in the three files compiles with the stock `doctest` parser once `SKIP_EXAMPLE` is registered. - `pre-commit run --files` on the three files: all hooks pass, including ruff, black, pydoclint, and the Ray docstyle check. 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]>
…ctests (#67208) ## Description Converts the seven `.. testcode::` examples in `python/ray/data/llm.py` to `>>>` doctests. They're in `HttpRequestProcessorConfig`, `vLLMEngineProcessorConfig`, `SGLangEngineProcessorConfig`, `ServeDeploymentProcessorConfig`, and `build_processor`, which holds three of them. The change touches only docstrings. **The examples stay render-only.** Every block was `:skipif: True`, because running them needs GPUs and model downloads, an OpenAI API key, or a running Serve deployment. Each docstring now carries one `# doctest: +SKIP_EXAMPLE` flag on its first line, which skips the whole docstring in both the `//python/ray/data:doctest` target and the `docstring-only` examples step. Sphinx strips the flag from the rendered page. The code itself is unchanged apart from the `>>>` and `...` prompts and the removal of the blank lines a doctest can't contain. This is separate from the other Ray Data docstring conversions, such as #67202 and #67207, because CODEOWNERS assigns `python/ray/data/llm.py` to `@ray-project/ray-llm`. ## Related issues Related to #67080, #67202, and #67207. ## Additional information **Not a duplicate.** I searched open PRs that mention `llm.py` or converting `ray.data.llm` docstring examples. None touches these examples. **Tests run** - `ci/ray_ci/doc/check_docstring_only.py --base upstream/master`: "1 file(s), docstrings only." - `ci/ray_ci/doc/run_docstring_examples.py python/ray/data/llm.py` against the macOS arm64 Ray nightly at `43b706d733c`, whose `llm.py` matches this PR's base, with the converted file swapped in: 0 run, 0 failed, 5 skipped for `+SKIP_EXAMPLE`. - `pytest --doctest-modules -p doctest_pytest_plugin` on the same file, with the Ray Data doctest plugin: 5 skipped as "render-only example (+SKIP_EXAMPLE)", no collection errors. - Every converted example parses with `doctest.DocTestParser` and compiles. - `pre-commit` on commit: all hooks pass, including black, ruff, and the Ray docstyle check. 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]>
…ock doctests (#67238) ## 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](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Description
Converts the remaining runnable
.. testcode::examples in nineDatasetmethods to stock>>>doctests:streaming_split,split,iter_batches,iter_jax_batches,iter_tf_batches,stats,explain,serialize_lineage, anddeserialize_lineage. It also converts the one example each inSerializablePreprocessorBase._set_serializable_fields(preprocessor.py) andMemoryProfiler(_internal/util.py). Stock doctests render without Sphinx-only directives, and thedocstring-onlylabel's examples step runs them. The change touches only docstrings.Three examples used
:options: +MOCK, which hid their output from comparison. Comparing it surfaced the following fixes:iter_batchesreadexample://image-datasets/simple, which resolves inside the installed package, and wheels don't shipray/data/examples/data/. It now reads the same images froms3://anonymous@ray-example-data/image-datasets/simple, asDataset.mapdoes. The three images make exactly two batches, so the expected output drops the...line between them, which wouldn't have matched.statsshows the current output format, with ellipses for counts, timings, and the read operator's name, which becomesReadRange->SplitBlocks(N)depending on the CPU count. Theassert ds.stats() == ""is now>>> ds.stats()with''as the output.MemoryProfilerreplaces the...placeholder statement withpass. In a doctest, a bare...expression echoesEllipsison every loop iteration. The USS values use an ellipsis.iter_tf_batchescalledDataset.to_tf, which has its own examples. It now callsiter_tf_batchesand prints the first batch, matching theiter_jax_batchesexample.The fourth
streaming_splitexample deliberately blocks, so it stays render-only, now with# doctest: +SKIPon that one line in place of:skipif: True. The rest of the docstring runs.explain,split, and the lineage examples matched as written.iter_jax_batchesanditer_tf_batchesrun only in postmerge under the label. The docstring examples image omits JAX and TensorFlow, so the step skips both docstrings by name. They still run in//python/ray/data:doctest.Related issues
Related to #66814, #67083, and #67202.
Additional information
Not a duplicate. I searched open PRs that touch
python/ray/data/dataset.py,streaming_split,serialize_lineage, orMemoryProfiler. #67202 converts theDatasettransform methods (map,map_batches,flat_map,groupby,aggregate,join) in the same file, and the two PRs edit different methods. #64869 and #66340 don't touch these examples. #66746, #66812, #67070, and #67080 converted other Data modules and have merged.Tests run
ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "3 file(s), docstrings only."ci/ray_ci/doc/run_docstring_examples.pyon the three files in an aarch64 image built frompython/deplocks/docs/docstring_examples_depset_py3.10.lock, with the Ray nightly at093faf5583eand the changed files mounted over the installed ones. The three files are unchanged between that commit and this PR's base. Result: 61 docstrings run and 0 failed.iter_jax_batches,iter_tf_batches,to_tf,write_tfrecords, anditer_torch_batchesare skipped for libraries the image omits.doctestin the same image after installingjax0.6.2 andtensorflow2.21.0: 0 failures each. Those aren't CI's pinned versions, so I'll check the postmergedata: docs example testsresult after merge.pre-commit run --fileson the three files: all hooks pass, including ruff, black, pydoclint, and the Ray docstyle check.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