chitralverma opened a new pull request, #9377:
URL: https://github.com/apache/opendal/pull/9377
# Which issue does this PR close?
Part of #9321 (tracker #9320).
# Rationale for this change
After `ListOptions` (#9332), `StatOptions` (#9351), and `DeleteOptions`
(#9370), this PR generates the Python `ReadOptions` from
`core/core/src/types/options.rs` with the same generator, so its docs follow
core. The class backs `read`, `presign_read`, and `open` in read mode. It
exposes no new option: core's `limit`, `if_version_match`,
`if_version_not_match`, `content_length_hint`, and `checksum` stay unexposed,
and so do suffix ranges.
This PR depends on #9375 for the shared core `ReadOptions` doc corrections;
see "Depends on #9375" below.
# What changes are included in this PR?
- `GENERATED_OPTIONS` in `dev/src/generate/python.rs` adds `ReadOptions`
with its 14 keyword arguments in main's order. Three row forms are new:
- `offset` and `size` both set core `range`, a `BytesRange`. The generated
conversion builds it with `ReadOptions::make_range`, which stays hand-written
in `options.rs` because `open` and the `prefetch` path of `read` also use it.
Only `offset` and `size` can set a range, and only together.
- `prefetch` names `ReaderOptions::prefetch`, a field of another core
struct. Generation checks that this field exists with kind `Usize`, and the
conversion to core `ReadOptions` leaves it out. `From<ReadOptions> for
ReaderOptions`, which `open` and a positive `prefetch` in `read` use, stays
hand-written and unchanged.
- `concurrent` is a core `usize` that Python takes as `Option<usize>`. An
omitted value is `0`, as before.
A table entry can also replace a core doc that does not hold for Python
(`docs`). Generation fails when `offset` or `size` has no Python doc, when a
doc names an unlisted keyword, or when a `Struct::field` names the class's own
struct. `options_rs.j2` is unchanged.
- `bindings/python/src/options/generated.rs` now also holds `ReadOptions`.
The hand-written struct, its `FromPyObject`, and `From<ReadOptions> for
ocore::options::ReadOptions` are gone from `options.rs`. Ignoring doc comments,
the generated code differs from main's in these places:
- The conversion lists `limit: None`, `if_version_match: None`,
`if_version_not_match: None`, and `checksum: false` instead of
`..Default::default()`.
- `FromPyObject` no longer calls `reject_unknown_kwargs(&dict,
READ_OPTION_KEYS, false)`, like the other generated classes. Nothing on main
extracted `ReadOptions` that way: `read` and `presign_read` build it from their
keyword arguments, and `open` used `from_open_kwargs`.
- `from_open_kwargs` keeps its allowlist check against
`OPEN_READ_OPTION_KEYS`, unchanged, and then extracts with the generated
`FromPyObject` instead of repeating the field list. After that check the three
overrides are absent or `None`, so they stay unset, and conversion errors come
in the same order.
- `#[derive(Default, Debug)]` instead of `#[derive(Default)]`.
- The docstrings follow the earlier rules, plus:
- A paragraph that links the performance guide is dropped, because the
guide covers the Rust API only.
- A `Some(value)` code span reads as the Python value: "`0` has the same
behavior as `None`".
- Python docs replace core's for four keyword arguments. Core documents
`range` in Rust range syntax, so `offset` and `size` get Python docs. Core
documents `gap` for `Reader::fetch`, which Python does not expose, so the `gap`
doc says it has no effect. Core documents `prefetch` for the Rust `Reader`, so
the Python doc keeps its meaning and adds the `ValueError` that `read` raises
when it is combined with an override.
- Hand-written docs that contradicted the generated ones:
- `read`, sync and async: the `concurrent`, `gap`, `offset`, and `size`
parameter descriptions. For example, `size` read "The size of the file." and
now reads "The number of bytes to read from `offset`. Defaults to the rest of
the file."
- `open`, sync and async, under `Raises`: `Unsupported` now covers read
options too, and a new `OSError` entry says that in read mode, a missing file
or a failed condition raises `OSError` from the file's read methods.
- `OpenKwargs` in `options.py` says the same, because it inherits the
`ReadKwargs` docs, whose exceptions hold for `read` but reach a file's reader
as `OSError`.
- New and extended tests in `dev/src/generate/python.rs`:
- `ReadOptions` field names, Rust types, extraction, conversion, and
member order against literals copied from main. Each docstring must equal its
core doc after the rendering rules, or its Python doc, line for line in the
pyclass doc comments too. Each field's docs must name the capability that
`OpRead::check` enforces for it and its exceptions, with no leftover Rust names.
- `ReadKwargs` keys and types; Python's `ast` reads back each docstring.
- `read` and `presign_read` keyword arguments and defaults against main
and against the table, and `OPEN_READ_OPTION_KEYS` and `READ_OPTION_KEYS`
against main and against the table.
- The `read` docstrings and `use_prefetch_reader` agree with the
`prefetch` and `gap` docs.
- Core drift for `ReadOptions` and `ReaderOptions::prefetch` (missing
fields, kind changes, missing doc), invalid table rows, new core fields
rendering as defaults, a core reword that uses either markup, and the two new
doc rules.
- `dev/src/generate/options.rs`: its test no longer expects `ReadOptions`
among the hand-written structs, and it checks the `Option<u64>` and timestamp
types on a sample struct, which the removed `ReadOptions` assertions covered.
# Are there any user-facing changes?
Docstring text only: `ReadOptions` in `_opendal.pyi`, `ReadKwargs` and
`OpenKwargs` in `options.py`, and `read` and `open` in `operator.pyi`. Names,
types, defaults, order, accepted inputs, errors, and exports are unchanged.
| Item | main | this PR (first line; later lines from core or Python) |
| --- | --- | --- |
| `ReadOptions` / `ReadKwargs` | Options for `read` operations. | Options
for read operations. Then how conditions combine, with
`opendal.exceptions.NotFound`, `ConditionNotMatch`, and `Unsupported`, the
empty-range shortcut, and the conditional operation specification. |
| `version` | The version of the file. | Read the given version of the file
instead of the current one. Then `NotFound` and
`op.capability().read_with_version`. |
| `concurrent` | The number of concurrent readers. | Set `concurrent` for
the operation. (core) |
| `chunk` | The size of each chunk. | Set `chunk` for the operation. Then
"`0` has the same behavior as `None`". (core) |
| `gap` | The gap between each chunk. | Each call to `read()` reads a single
byte range, and so does each read from a file that `open()` returns, so `gap`
has no effect. (Python) |
| `offset` | The offset of the file. | Read from this byte offset instead of
from the start of the file. Then empty bytes or `RangeNotSatisfied` at or past
the end. (Python) |
| `prefetch` | The number of completed chunks to buffer during concurrent
reads. | The number of completed chunks that a read with `chunk` and a
`concurrent` above one can buffer in memory before they are consumed. Then the
`0` default and the `ValueError` with overrides in `read()`. (Python) |
| `size` | The size of the file. | Read this many bytes from `offset`. Then
the empty `size=0` read and the failure past the end. (Python) |
| `if_match`, `if_none_match`, `if_modified_since`, `if_unmodified_since` |
The ETag of the file. / The last modified time of the file. | Read only when
the condition holds. Then `ConditionNotMatch`, `NotFound`, and
`op.capability().read_with_*`. |
| `content_type`, `cache_control`, `content_disposition` | The Content-Type
(and so on) response header override, where supported. | Core's override text.
(core) |
## Depends on #9375
#9375 corrects the shared core `ReadOptions` docs, and Xuanwo's review there
asks for more corrections to `chunk` and the missing-file `NotFound` sentences.
Until it lands, this PR renders these core sentences from main, each checked on
a build of this branch:
- The class doc says "A missing file fails the read with `NotFound` no
matter which conditions are set". On memory, `read("missing", if_match=...)`
raises `opendal.exceptions.Unsupported`.
- `concurrent` says setting it reads files concurrently. Without `chunk`,
Silo S3 gets one `GET`, also with `concurrent=3`.
- `chunk` says "OpenDAL will use services' preferred chunk size by default".
Without `chunk`, or with `chunk=0`, Silo gets one `GET`. With `chunk=4`, it
gets a `HEAD` and three ranged `GET`s.
- The overrides say they are only meaningful with presign. A plain `read`
with the three overrides sends `GET
/test/f?response-cache-control=...&response-content-disposition=...&response-content-type=...`
to Silo.
- From the #9375 review: an open-ended read with a positive `chunk` calls
`stat` first and forwards the conditions to it. On fs, `read("f", chunk=2,
if_modified_since=...)` raises `Unsupported` from `stat` for an existing file,
and the same read with `size=4` succeeds. `open()` behaves the same. On Silo,
the `HEAD` carries the conditions and `versionId`, and a failing `if_match`
raises `ConditionNotMatch` from `stat`.
Once #9375 lands, this PR merges main, regenerates, and updates the
references that its tests pin. With the core text of #9375 at `d9f4a4b05`
applied in a scratch edit, generation, `just stub-gen`, `just fmt`, and `just
lint` pass, and three Python generator tests fail as intended: its new `chunk`
paragraph names `op.capability().stat_with_if_modified_since` and
`opendal.exceptions.Unsupported`. That paragraph also names
`content_length_hint` and suffix ranges, which Python does not expose, so the
Python `chunk` doc will need a look then.
# Verification
Verification on this branch's head, which is based on main at `78f6cf689`:
- Doc claims, on a build of this branch, sync and async, on memory, fs, and
versioned Silo S3 behind a logging proxy (206 checks):
- `read` with a missing file raises `NotFound` without options and with
supported conditions (fs: `if_modified_since`, `if_unmodified_since`; Silo: all
four), and `Unsupported` for a condition the service lacks.
- On Silo, each holding condition returns the data with one `GET`, and
each failing one raises `ConditionNotMatch`. `version` reads that version, and
a well-formed unknown version raises `NotFound`. On fs, the two date conditions
behave the same.
- `size=0` returns `b""` without a request on Silo, also for a missing
file, a failing `if_match`, a version, and a positive `prefetch`. On memory,
`size=0` with `if_match` raises `Unsupported`. `open()` with `size=0` behaves
the same.
- An `offset` at the end (10 of 10 bytes) returns `b""` on memory and fs,
and raises `RangeNotSatisfied` on Silo without `chunk` but returns `b""` with
`chunk=4`. An `offset` of 15 raises `RangeNotSatisfied` with `chunk=4` on all
three. Through `open()`, both return `b""`.
- A `size` that runs past the end raises `Unexpected` or
`RangeNotSatisfied`, depending on the service and on `chunk`, through `read`
and as `OSError` through `open()`.
- `gap=0` and `gap=1000000` send the same requests with `chunk=4`, through
`read` and `open()`. In core, `gap` is only read by `Reader::merge_ranges`,
which only `Reader::fetch` calls; the binding does not call `fetch`.
- A positive `prefetch` with any override raises `ValueError` without a
request; `prefetch=0` with the overrides sends them.
- `presign_read` with the overrides puts the three `response-*` keys in
the URL query; with `if_match` and `version`, the request carries an `if-match`
header and `versionId`.
- In read mode, `open()` raises `opendal.exceptions.Unsupported` for an
option the service lacks, while a missing file or a failing condition raises
`OSError` from `read()` on the file, sync and async.
- On a Rust build of core against memory and Silo: a `limit` with a
positive `chunk`, with `concurrent=2`, or when presigning returns
`ConfigInvalid`; `chunk=0` or `concurrent=1` do not. A zero `limit` with an
unsupported `if_match` returns `Unsupported`, and otherwise returns an empty
buffer without a request. Python does not expose `limit` yet.
- Against main, `options.py`, `_opendal.pyi`, `operator.pyi`, `__init__.py`,
and `file.pyi` parse to identical Python ASTs once docstrings are removed, and
`lib.rs` is byte-identical. The Sphinx pages for both `read` and `open` methods
show the new text.
- An old-consumer probe gives the same 8712 lines on builds of main
(`f47ec7c1f`) and this branch, after S3 request IDs, dates, and UUIDs are
normalized. It covers the `opendal._opendal.ReadOptions` class and its missing
constructor; the `__text_signature__` of `read`, `open`, and `presign_read`;
the keys, types, and totality of `ReadKwargs` and `OpenKwargs`; and 3336 calls
on memory, fs, and Silo against a file and a missing path. They are 1512 `read`
and 1512 `open` calls, half of each async, that pass 73 single-key values
(wrong types, negative and oversized integers, naive datetimes, and others), an
`offset`/`size` grid, a `chunk`/`concurrent`/`prefetch` grid, `prefetch` with
each override, unknown keys (`limit`, `range`, `override_content_type`), and
all-`None`; 60 `open` calls with write-only keys; and 252 `presign_read` calls,
recording each result or exception.
- mypy `--strict` and pyright report the same three expected errors on a
`ReadKwargs` probe against the stubs from main and from this branch.
- The memory suite gives 501 passed and 91 skipped, and the fs suite 550
passed and 42 skipped, the same as the build of main.
- `just stub-gen` (run twice), `just fmt`, `just lint`, `just test-typing`,
`cargo check --features abi3`, and every `ci_odev.yml` generator plus `cargo
fmt` leave `git diff --exit-code` clean, with hawkeye 7.2.0. The `examples` job
steps pass, including a clean Sphinx build with `-W -n`. In an Ubuntu 24.04
container, the dev tests (142) and clippy, every `ci_odev.yml` generator plus
`cargo fmt` and `git diff --exit-code` (with `protobuf-compiler` installed for
`gcs-grpc`), the `lint` job steps, `test-typing`, the memory and fs suites
(same counts as above), and the `examples` job steps pass. The stubs there
regenerate identically apart from the license headers and Rust formatting,
which `just fmt` adds with hawkeye and `cargo fmt`.
- With core's `ReadOptions` summary set to `"Options" for read operations,
with \d, \\ and """ inside.` in a scratch edit, `just stub-gen`, a second `just
fmt`, and `just lint` exit 0. Python's `ast` reads that summary back from
`ReadKwargs` in `options.py` and from `ReadOptions` in `_opendal.pyi`, and the
runtime `__doc__` of both classes matches.
- Scratch core edits through `generate -l python`: changing `concurrent` to
`Option<usize>` fails with `core ReadOptions::concurrent has kind OptionUsize;
Python ReadOptions.concurrent requires Usize`, and removing `range` or
`ReaderOptions::prefetch` fails too. Writing the `read_with_if_match` link as a
plain code span leaves the generated files byte-identical. Adding a core field
adds only its default to the generated conversion. With `src/options/` moved
aside, generation recreates it.
- Mutations: 31 scratch edits each fail at least one test:
- Table: dropping `prefetch`, swapping `offset` and `size`, exposing
`checksum`, swapping two override targets, `prefetch` from
`ReaderOptions::concurrent`, and `concurrent` as `Option<u64>`.
- Code paths: no `unwrap_or_default` for counts, the range as a default, a
range set by one keyword, any name setting a range, no own-struct, Python-doc,
or unknown-doc check, and renames that include the range.
- The docs: dropping the `gap` or `prefetch` Python doc, editing the
`offset` doc, cutting the `size` doc to its first paragraph, keeping
performance guide paragraphs, keeping `Some(..)`, stripping any `Some(`, and
dropping the `OSError` paragraph of `OpenKwargs`.
- `options.rs`: `READ_OPTION_KEYS` without `gap`, `open` accepting
`content_type`, and the `prefetch` check without `cache_control`.
- `operator.rs`: `presign_read` taking `offset`, `read` defaulting
`prefetch` to `0`, and main's `gap` docstring.
- Core: naming the wrong capability for `if_match`, and dropping the
`NotFound` sentence of `version`.
- The hand-written options parser: joining doc lines with spaces.
# Breaking changes
None.
# AI Usage Statement
- **Harness:** Claude Code via T3 Code. **Model:** Claude Opus 5.5.
**Effort:** xhigh. **Role:** Implemented the generator change, tests, and doc
changes; ran the doc-claim checks on memory, fs, and Silo S3, the core `limit`
checks, the surface diff, the probes, the mutation checks, and the local and
Linux container CI steps; drafted this description. The author reviews every
change and claim.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]