← Back to Pull requests

Prototype Python signatures for native callables #1391

Closed
unknown closed this pull request without merging on Nov 19, 2026 12:25 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 11:23 UTC
Author

This prototype lets a native callable declare its Python signature beside its Rust implementation. The macro generates the argument struct and its FromArgs implementation.

#[py_signature("replace(self, old: str, new: str, /, count: SupportsIndex = -1) -> str")]
fn str_replace<'h>(s: &PyStr<'h>, args: &ReplaceArgs, vm: &mut VM<'h>) -> RunResult<Value>

Here old and new borrow the caller’s string data, and count accepts objects implementing __index__. str.replace implementation

Two existing conversions in the prototype still differ from the base branch. math.isclose accepts user-defined numeric conversions such as __float__. For an integer separator, str.split reports must be str or None, not int where the base reports expected str.

I’d like feedback on whether this declaration is easier to maintain than an explicit Rust argument struct. One case where the choice is less clear is str.split: when both arguments are invalid, CPython checks maxsplit before sep. The prototype spells that check_last = sep. Would you keep that rule in the attribute or write the conversion explicitly in the implementation? str.split implementation

This builds on #1387 for scoped cleanup of bound arguments. The upstream diff includes that open PR. Review just the signature changes.

Local validation passed for 127 unit tests and 1,572 Monty and CPython fixture checks.

<!-- This is an auto-generated description by cubic. -->
---

Summary by cubic


Prototypes #[py_signature], letting a native callable declare its Python signature beside its Rust implementation. The macro generates the argument struct, its FromArgs binding, and the descriptor used for runtime inspection.

#[py_signature("replace(self, old: str, new: str, /, count: SupportsIndex = -1) -> str")]
fn str_replace<'h>(s: &PyStr<'h>, args: &ReplaceArgs, vm: &mut VM<'h>) -> RunResult<Value>

  • Migrates print, str.split, str.replace, zip_longest, JSON dump/dumps, Counter.update/subtract, dict.update, math.isclose, and os.urandom to the macro.

  • SupportsIndex parameters now accept objects implementing __index__, and the migrated callables expose __text_signature__ to Python.

  • Repeated attribute options are rejected with a compile error.

  • Guards bound arguments for the call duration with vm.scope / Local (the pending #1387 scoped-cleanup work); the upstream diff includes that PR, so review just the signature changes.
  • Open question: when both sep and maxsplit are invalid, CPython reports maxsplit first. str.split spells that with check_last = sep; I'd like to know whether that rule belongs in the attribute or should be written explicitly in the implementation.

    Local validation passes 127 unit tests and 1,572 Monty and CPython fixture checks.

    <sup>Written for commit 60471effc8f5beb64e5984502b4d6333de7501a0. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1391?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    Diff: 484 lines (+363 / -121) across 6 files Reviewer: jaheba Review duration: 61m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 484 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.370
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1391"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.7% · Safe to merge
    18.7%
    Repository