◐ Shell
clean mode source ↗

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

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 ⚠️ Warning 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.