Prototype Python signatures for native callables
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.
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, JSONdump/dumps,Counter.update/subtract,dict.update,math.isclose, andos.urandomto the macro. SupportsIndexparameters 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.