Xuanwo commented on code in PR #9332:
URL: https://github.com/apache/opendal/pull/9332#discussion_r4725967680
##########
dev/src/generate/python.rs:
##########
@@ -626,6 +629,276 @@ const PYTHON_LICENSE_HEADER: &str = "\
# under the License.
";
+/// A Python options class generated from the core options struct of the
+/// same name. The class and field docstrings come from the core doc comments.
+struct PyOptions {
+ /// The class name, e.g. `ListOptions`.
+ name: &'static str,
+ /// The keyword arguments in their Python order: the name, which is also
+ /// the core field and the Rust field it sets, and the kind that core
+ /// field must have. A core field that no keyword argument sets is not
+ /// exposed and keeps its default.
+ options: &'static [(&'static str, OptionKind)],
+}
+
+/// The Python options classes generated from `core/core/src/types/options.rs`.
+/// The other classes in `bindings/python/src/options.rs` are hand-written.
+const GENERATED_OPTIONS: &[PyOptions] = &[PyOptions {
+ name: "ListOptions",
+ options: &[
+ ("limit", OptionKind::OptionUsize),
+ ("start_after", OptionKind::OptionString),
+ ("recursive", OptionKind::Bool),
+ ("versions", OptionKind::Bool),
+ ("deleted", OptionKind::Bool),
+ ],
+}];
+
+/// The class order of `options.py`, which hand-written and generated classes
+/// share.
+const OPTIONS_PY_ORDER: &[&str] = &[
+ "ReadOptions",
+ "WriteOptions",
+ "ListOptions",
+ "StatOptions",
+ "CopyOptions",
+ "RenameOptions",
+ "DeleteOptions",
+];
+
+/// Render model of one generated Python options class.
+#[derive(Debug, Serialize)]
+struct PyClass {
+ name: &'static str,
+ /// The core doc comment, one entry per line.
+ doc: Vec<String>,
+ fields: Vec<PyField>,
+ /// Every field of the core struct in core order, with the expression
+ /// that sets it.
+ conversion: Vec<ConvertField>,
+}
+
+#[derive(Debug, Serialize)]
+struct PyField {
+ name: &'static str,
+ kind: OptionKind,
+ /// The core doc comment, one entry per line.
+ doc: Vec<String>,
+ rust_type: &'static str,
+}
+
+#[derive(Debug, Serialize)]
+struct ConvertField {
+ name: String,
+ expr: String,
+}
+
+/// The Rust field type of a keyword argument and its `options.py` type, for
+/// the core kinds that generated classes expose so far.
+fn option_types(kind: OptionKind) -> Option<(&'static str,
options::OptionType)> {
+ Some(match kind {
+ OptionKind::OptionString => ("Option<String>",
options::OptionType::Str),
+ OptionKind::OptionUsize => ("Option<usize>", options::OptionType::Int),
+ OptionKind::Bool => ("Option<bool>", options::OptionType::Bool),
+ _ => return None,
+ })
+}
+
+/// The value of a core field that Python does not expose. Every core options
+/// struct derives `Default`, so this equals the field's default.
+fn default_expr(kind: OptionKind) -> &'static str {
+ match kind {
+ OptionKind::Bool => "false",
+ OptionKind::Usize => "0",
+ OptionKind::BytesRange => "Default::default()",
+ _ => "None",
+ }
+}
+
+/// The docstring of a generated class or field: the core doc comment, one
+/// entry per line.
+///
+/// Rustdoc links become plain code spans, because Python documentation
+/// cannot resolve Rust paths:
``[`Capability::list_with_limit`](crate::Capability::list_with_limit)``
+/// and ``[`crate::layers::SimulateLayer`]`` keep only their code span.
+/// Code spans that name a core capability then use its Python name (see
+/// [`py_code_span`]); other code spans stay as written.
+fn py_doc(
+ core_doc: &[String],
+ item: &str,
+ capabilities: &[CapabilityField],
+) -> Result<Vec<String>> {
+ if core_doc.iter().all(|line| line.is_empty()) {
+ bail!("core {item} has no doc comment");
+ }
+ let mut lines = Vec::with_capacity(core_doc.len());
+ for line in core_doc {
+ let mut unlinked = String::with_capacity(line.len());
+ let mut rest = line.as_str();
+ while let Some(start) = rest.find("[`") {
+ let Some(len) = rest[start + 1..].find("`]") else {
+ break;
+ };
+ unlinked.push_str(&rest[..start]);
+ unlinked.push_str(&rest[start + 1..start + len + 2]);
+ rest = &rest[start + len + 3..];
+ if let Some(target) = rest.strip_prefix('(')
+ && let Some(end) = target.find(')')
+ {
+ rest = &target[end + 1..];
+ }
+ }
+ unlinked.push_str(rest);
+
+ let mut out = String::with_capacity(unlinked.len());
+ let mut rest = unlinked.as_str();
+ while let Some(start) = rest.find('`') {
+ let Some(len) = rest[start + 1..].find('`') else {
+ break;
+ };
+ let code = &rest[start + 1..start + 1 + len];
+ out.push_str(&rest[..=start]);
+ out.push_str(&py_code_span(code, capabilities).unwrap_or_else(||
code.to_string()));
+ out.push('`');
+ rest = &rest[start + len + 2..];
+ }
+ out.push_str(rest);
+ if out.contains('"') || out.contains('\\') {
+ bail!(
+ "core {item} doc line `{out}` has a character docstrings must
escape; \
Review Comment:
AI-generated follow-up on `2bcb5b7a95`. Both edits to the `limit`
documentation now work. With this PR’s backslash and triple-quote sample as the
core `ListOptions` summary, Python reads the expected text after generation and
after `ruff format`. In `bindings/python`, `just fmt` then runs `ruff check
--unsafe-fixes --fix`. Its D301 fix changes `\d` to two backslashes followed by
`d` in the parsed docstrings for both `ListKwargs` in `options.py` and
`ListOptions` in `_opendal.pyi`. The recipe exits on D210. Could both
docstrings preserve the sample text through stub extraction and formatting,
with a regression test checking their parsed values?
Changing the `recursive` field’s rustdoc link to a plain code span around
`Capability::list_with_recursive` produces byte-identical Python and Rust
output, but `test_list_options_survive_any_core_doc` fails. Could that
assertion check the capability reference in the generated field documentation
so either supported markup passes?
--
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]