Skip to content

Reuse checked signatures for deque, timezone and type - #9773

Closed
youknowdot wants to merge 5 commits into
RustPython:mainfrom
youknowdot:fix/legacy-native-signatures-20261110
Closed

youknowdot wants to merge 5 commits into
RustPython:mainfrom
youknowdot:fix/legacy-native-signatures-20261110

Conversation

@youknowdot

@youknowdot youknowdot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Consolidated into #9756

At the user's request, all commits and tests from this PR are now included unchanged in #9756 at the same combined head f51d264b2228321f444a10ddc4d97e9ac42826b0. This PR is closed only to keep one active PR; its review discussion and original commits are preserved. The description below records this PR's implementation and validation before consolidation.

Summary

This is a dependent follow-up to #9756. Current head f51d264b2228321f444a10ddc4d97e9ac42826b0 is stacked on the actual #9756 head e30a3bddbc79044cadabe36c378aa038a2c99c1c, with #9758's receiver changes already in the underlying main. The two follow-up commits distinguish declared omitted defaults from unknown defaults and reuse checked argument signatures for class metadata.

  • Keep production documentation bodies in the existing rustpython-doc database. No copied Rust docstrings or implicit docstring-header bypass remains.
  • Reuse Add checked Python 3.15 text-signature overrides #9756's shared FromArgs::TEXT_SIGNATURE for initializer/constructor class metadata instead of adding another class-only override mechanism. Class signatures have no receiver marker; method/classmethod markers are checked by the common macro-time validation.
  • Remove the old implicit-receiver counting workarounds. Python argument metadata no longer includes the receiver after Match Clinic signatures and unify native self/module receivers #9758.
  • Distinguish a declared omitted default (DefaultRepr::Unrepresentable) from an unknown default (DefaultRepr::Unknown). This lets timezone obtain (offset, name=<unrepresentable>) through ordinary signature generation, without a timezone override. Unknown defaults and unnamed leaf metadata remain rejected by the class representability check.
  • Extend checked overrides to CPython's deque bracket notation without /. Apply explicit overrides only to deque, deque.index, and type.__prepare__.
  • Preserve the existing declared metadata contract for known unrestricted raw argument binders such as type.__prepare__; unknown binding metadata remains rejected. Actual argument conversion and dispatch are unchanged.
  • Add metadata/parser regression coverage and signature/doc/inspect checks, including type.__prepare__ ignoring arbitrary arguments and returning a fresh namespace.

CPython 3.15 basis

  • _collectionsmodule.c: deque constructor and index explicitly use Argument Clinic @text_signature overrides.
  • _datetimemodule.c: datetime.timezone.__new__ is Clinic-generated with name: unicode = NULL, displayed as <unrepresentable>.
  • typeobject.c: type_prepare ignores its arguments; its manually declared method header publishes $cls, name, bases, /, **kwds.

Validation

Verified on 2026-11-17:

  • Current head's hosted checks: 16 successful, 3 skipped, none failed or pending. Successful checks include Linux/macOS/Windows snippet and CPython tests, Rust tests on all three platforms, Ubuntu Clippy, Miri, WASM package/demo, WASI tests, cargo doc, lint, cargo shear, and CodSpeed. The skipped checks are the draft parity workflow and two conditional matrix placeholders; skipped checks are not claimed as passes.
  • Local derive tests on the implementation before the dependency-only rebases: 41 passed. The dependency rebases retained the changes, with the final conflict resolved by keeping both the upstream map_or_else lint correction and this PR's validator selection.
  • Normal configured pre-commit checks and the CPython 3.15.2 builtin_signature.py snippet passed again after the latest rebase.
  • Local full-workspace validation was interrupted for shared build-resource scheduling and is not claimed complete for this revision. The hosted results above are for the exact current head, not an earlier revision.

AI assistance

OpenAI Codex assisted with review, implementation, and validation.

@coderabbitai

coderabbitai Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

Comment thread crates/stdlib/src/_datetime.rs Outdated

// timezone

/// timezone(offset, name=<unrepresentable>)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

dont' manually add docs

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The mechanism looks right: has_internal_doc_signature follows find_signature + skip_signature in Objects/typeobject.c (the doc must start with name(, the scan looks for )\n--\n\n, and it gives up at a blank line). These four objects get their text signatures from hand-written docstring headers in CPython as well. A few things should change before merging, though.

Observed before → after

Before: a build of 45a012cda (main at the time). After: the values asserted in this PR's snippet, which match CPython 3.15.

object __text_signature__ before after (= CPython) inspect.signature
deque None '([iterable[, maxlen]])' ValueError both
deque.index '($self, needle, start=0, stop=<unrepresentable>, /)' '($self, value, [start, [stop]])' ValueError both
timezone None '(offset, name=<unrepresentable>)' ValueError both
type.__dict__['__prepare__'] '($type, name, bases, /, **kwds)' '($cls, name, bases, /, **kwds)' (type, …) → (cls, …)

In practice this changes raw metadata only. The only inspect-visible difference is the receiver name of the unbound __prepare__.

1. Drop "legacy" from the naming; one comment is factually wrong

The feature is "an explicit internal-doc signature in the docstring is used as-is", and has_internal_doc_signature already says that. "Legacy" in the title, branch, code comment, test name and snippet comment does not describe it:

  • The comment in signature.rs, "Explicit Clinic headers cover legacy signatures that are not Python parameter lists", is wrong for __prepare__: ($cls, name, bases, /, **kwds) is an ordinary parameter list. Only the deque headers use optional groups. Something like: "A doc that starts with a complete name(...)\n--\n\n header supplies its own text signature."
  • explicit_internal_doc_preserves_legacy_signatures → e.g. explicit_internal_doc_signature_is_kept.
  • Snippet comment "Legacy Clinic headers …" and the legacy signature should remain unrepresentable message → describe them as explicit internal-doc signatures.
  • PR title → e.g. "Honor explicit internal-doc signatures for deque, timezone and type.prepare".

#9757 went through the same rename (legacy_signature → signature_from_flags) for the same reason.

2. The header is used implicitly and never checked against the Rust binding

Any /// doc that happens to be header-shaped now silently replaces the generated signature, and nothing checks the hand-written header against the real arguments. The drift is already there: deque.index binds needle in Rust, while the header says value. The displayed text is correct today, but a later change to the Rust signature would go unnoticed.

#9757 takes the opposite approach: an explicit #[pyfunction(signature_from_flags)] opt-in, plus const assertions that reject a mismatched arity at compile time. CPython does not validate hand-written headers either, so this is not a parity bug. It is still inconsistent within RustPython. Please consider at least a const check that the header's positional arity agrees with the binding metadata, or an explicit opt-in so the override cannot happen by accident.

3. Docs are now split between two sources

Because a /// doc overrides the crates/doc DB entry, the bodies of these four docstrings are now hard-coded in Rust, and regenerating crates/doc/src/data.inc.rs will no longer update them. crates/doc/generate.py stores only __doc__, which has the header stripped. A broader fix would be to have the generator also capture __text_signature__. That can be a follow-up, but it should be acknowledged here.

4. Conflict with #9757

Both PRs append to crates/vm/src/function/signature.rs and to the end of extra_tests/snippets/builtin_signature.py, so whichever lands second needs a rebase.

— commented by Claude

@youknowdot
youknowdot force-pushed the fix/legacy-native-signatures-20261110 branch 3 times, most recently from a6fadd3 to 847ee0b Compare November 11, 2026 14:42
@youknowdot
youknowdot force-pushed the fix/legacy-native-signatures-20261110 branch from 847ee0b to 632530b Compare November 12, 2026 14:48
@youknowdot youknowdot changed the title Preserve legacy deque, timezone and type signatures Reuse checked signatures for deque, timezone and type Nov 12, 2026
@youknowone

Copy link
Copy Markdown
Member

Status check after #9758 (merged 2026-11-15, receivers moved out of FuncArgs; ReceiverFunction ARGS now describe only the Python arguments):

  1. Rebase onto Match Clinic signatures and unify native self/module receivers #9758 and stack properly on Add checked Python 3.15 text-signature overrides #9756. This branch carries its own copy of Add checked Python 3.15 text-signature overrides #9756's commit (308226c79, a different SHA from Add checked Python 3.15 text-signature overrides #9756's 103273851), so the two will drift as soon as Add checked Python 3.15 text-signature overrides #9756 is rebased.
  2. The receiver workarounds are likely obsolete. 92f439eed (avoid counting an implicit scalar receiver twice) and ab11db399 (keep unknown receiver metadata rejected) exist because the receiver used to travel in-band. After Match Clinic signatures and unify native self/module receivers #9758 the receiver kind is known from the item kind (method, classmethod, module function, class), so receiver markers in an override can be checked at macro time instead of against SigArg markers. Please re-evaluate these against the new structure rather than carrying them over.
  3. timezone should not use an override. In CPython it is Clinic-generated (datetime.timezone.__new__ with name: unicode = NULL in _datetimemodule.c), so under our policy it belongs on the generator path. What blocks it is real_signature/params_representable, which drops a class signature whenever any default is DefaultRepr::Unrepresentable. That variant currently means two things: a declared NULL default (which Clinic renders as <unrepresentable>) and a default we don't know. Separating the two fixes timezone through generation. deque, deque.index (@text_signature in _collectionsmodule.c) and type.__prepare__ (hand-written header) remain legitimate overrides.
  4. The "legacy" naming is still in the commits and the title.

As agreed, the override work is the last piece of the text-signature series.

— commented by Claude

Validate receiver markers from the macro item kind and keep only explicit Python arguments in the const binding checks. Share Clinic-style overrides through FromArgs instead of repeating them on cloned methods.

Assisted-by: Codex:unavailable
Keep the field-option list contiguous and describe the shared argument-spec override in its own paragraph, fixing strict Clippy doc-lazy-continuation diagnostics.

Assisted-by: Codex:unavailable
@youknowdot
youknowdot force-pushed the fix/legacy-native-signatures-20261110 branch from f69a767 to 700b160 Compare November 16, 2026 16:02
Replace map plus unwrap_or_default with the equivalent map_or_else expression to satisfy Clippy 1.100 while keeping Rust 1.99 compatibility and the generated token stream unchanged.

Assisted-by: Codex:unavailable
Generate Clinic NULL defaults without text signature overrides, while rejecting unnamed constructor metadata.

Assisted-by: OpenAI Codex:model version not exposed
Use the actual RustPython#9756 shared override, validate receivers at macro expansion, and keep timezone on the generated NULL-default path. Preserve centralized documentation.

Assisted-by: OpenAI Codex:model version not exposed
@youknowdot
youknowdot force-pushed the fix/legacy-native-signatures-20261110 branch from 700b160 to f51d264 Compare November 16, 2026 17:08
@youknowdot youknowdot closed this Nov 17, 2026
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.

3 participants