FromArgs with error_msg by youknowone · Pull Request #6804 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR introduces an error_msg attribute feature to the argument derive macro system, allowing custom error messages for argument validation. The derive implementation is enhanced to parse and handle error_msg in attribute declarations, and several builtins are updated to provide more specific error messages for their optional arguments.
Changes
| Cohort / File(s) | Summary |
|---|---|
Derive Macro Error Message Support crates/derive-impl/src/from_args.rs |
Adds error_msg field to ArgAttribute struct and implements parsing logic to extract custom error messages from pyarg(error_msg = "...") annotations. Generates code to map conversion errors to type errors with the provided message, with guards against duplicate definitions. |
Builtin Function Argument Validation crates/vm/src/builtins/function.rs |
Annotates optional fields in PyFunctionNewArgs with error_msg attributes specifying type constraints for name, argdefs, closure, and kwdefaults arguments. |
Builtin Interpolation Argument Handling crates/vm/src/builtins/interpolation.rs |
Refines conversion argument from OptionalArg<PyObjectRef> to Option<PyStrRef> with explicit error_msg attribute and simplified string validation logic. Moves validation to early checks instead of runtime pattern matching. |
Builtin Super Type Argument crates/vm/src/builtins/super.rs |
Narrows py_type argument in InitArgs from OptionalArg<PyObjectRef> to OptionalArg<PyTypeRef> and adds error_msg attribute. Removes runtime downcast logic in favor of compile-time argument type constraint. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
- Pyfunction builtins and constructor #5823: Introduced
PyFunctionNewArgsstruct; this PR augments it with error message annotations on the same fields. - fix fcntl, sslsocket traverse, super , excepthook #6623: Previously modified
InitArgs::py_typein super.rs by widening it toOptionalArg<PyObjectRef>; this PR narrows it back toOptionalArg<PyTypeRef>with stricter validation.
Suggested reviewers
- ShaharNaveh
- arihant2math
Poem
🐰 A macro whispers soft and clear,
"Show the user what they fear!"
No more vague errors in the night,
Just helpful messages, shining bright. ✨
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'FromArgs with error_msg' accurately reflects the main change: adding an error_msg capability to the FromArgs derive macro and its argument parsing system. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
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.
Comment @coderabbitai help to get the list of available commands and usage tips.