Skip to content

Support Python None defaults in object argument declarations - #9557

Merged
youknowone merged 3 commits into
RustPython:mainfrom
youknowdot:fix-derive-none-default-20261103
Nov 3, 2026
Merged

youknowone merged 3 commits into
RustPython:mainfrom
youknowdot:fix-derive-none-default-20261103

Conversation

@youknowone

Copy link
Copy Markdown
Member

Summary

Follow up the default=None design question in #9538. For non-primitive, non-wrapped argument fields, lower #[pyarg(any, default = None)] through the existing lazy Python-object conversion path. The signature already has typed None metadata; the omitted runtime value now uses the None singleton rather than an incompatible Rust Option expression.

  • Preserve existing Option, OptionalArg, and OptionalOption default expressions and omission semantics. This does not reinterpret None as OptionalArg::Missing or remove explicit Clinic display-default overrides.
  • Construct the default only on omission. Supplied-value conversion is unchanged. PyObjectRef uses the singleton without allocating another Python object or invoking user callbacks.
  • Preserve converter validation for typed references: a PyStrRef with a None default rejects omission through its normal converter, just as incompatible literal defaults already do. This does not bypass type checks or add a compile-time Python type checker.
  • Document the behavior and add three pure token/metadata tests covering lazy placement, typed None metadata, wrapper exclusions, parentheses, existing literals, and typed-reference conversion. No Python objects are constructed by these tests.

Validation

Independent exact-head source review and normal pre-commit hooks passed; diff check and clean worktree verified. The two affected files are unchanged between base 016cdff and current main 7f50ac2; merge-tree against current main succeeds.

The three new tests have not run locally. Derive/workspace/C-API tests and workspace/C-API Clippy were attempted offline but stopped before compilation because the pinned parking_lot_core git source is missing from the local cache. Exact-head CI compilation and test execution remain pending; this is a Draft PR.

No Lib files, snippets, dependency changes, global argument-shape ordering changes, or runtime-object tests.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Comment thread crates/derive-impl/src/from_args.rs Outdated
fn literal_object_expr(expr: &Expr) -> Option<TokenStream> {
match expr {
Expr::Path(path) if path.qself.is_none() && path.path.is_ident("None") => {
Some(quote!(vm.ctx.none()))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not this way. Only when the rust type is Option including Option

Generate the None singleton lazily for object defaults while retaining existing Option and OptionalArg Rust-default paths. Cover generated syntax and typed metadata without constructing Python objects.

Assisted-by: OpenAI Codex:model version not exposed
Keep the wrapper fallback expectation unchanged while using quote output instead of a manually spaced closure string.

Assisted-by: OpenAI Codex:model version not exposed
Remove Python None construction for unwrapped fields. Document existing Option defaults and replace the two newly added object-None tests with Option metadata and literal-boundary checks.

Assisted-by: OpenAI Codex:model version not exposed
@youknowdot
youknowdot force-pushed the fix-derive-none-default-20261103 branch from 017500f to fe4a8e5 Compare November 3, 2026 07:03
@youknowone
youknowone marked this pull request as ready for review November 3, 2026 16:36
@youknowone
youknowone enabled auto-merge (squash) November 3, 2026 16:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T16:39:21.399233Z fe4a8e5 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@youknowone
youknowone disabled auto-merge November 3, 2026 16:36
@youknowone
youknowone merged commit a742f81 into RustPython:main Nov 3, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants