Skip to content

Use KwArgs newtype struct#8120

Open
ShaharNaveh wants to merge 1 commit into
RustPython:mainfrom
ShaharNaveh:kwargs-newtype
Open

Use KwArgs newtype struct#8120
ShaharNaveh wants to merge 1 commit into
RustPython:mainfrom
ShaharNaveh:kwargs-newtype

Conversation

@ShaharNaveh

@ShaharNaveh ShaharNaveh commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Summary by CodeRabbit

  • Refactor
    • Improved internal handling of function arguments for better code organization and maintainability.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

KwArgs is promoted from a bare #[derive(Clone)] tuple struct into a full newtype by adding Default, Deref, DerefMut, Debug, and IntoIterator (both by-ref and by-value) implementations. FuncArgs.kwargs changes from a raw IndexMap<String, PyObjectRef> to KwArgs, and all constructors, converters, and call sites in frame.rs and _operator.rs are updated accordingly.

Changes

KwArgs newtype and FuncArgs migration

Layer / File(s) Summary
KwArgs newtype: traits, FuncArgs field, and constructors
crates/vm/src/function/argument.rs
KwArgs gains Default, Deref, DerefMut, Debug, and IntoIterator (by-ref and by-value) implementations. FuncArgs.kwargs field type changes from IndexMap<String, PyObjectRef> to KwArgs. All FuncArgs constructors (new, with_kwargs_names, from_vectorcall, from_vectorcall_owned) and From conversions are updated to build KwArgs instead of raw IndexMap. pop_kwarg uses deref-based swap_remove.
Call-site migration
crates/vm/src/frame.rs, crates/vm/src/stdlib/_operator.rs
frame.rs adds KwArgs import, removes the standalone IndexMap import, and replaces IndexMap::new() with KwArgs::default() in collect_positional_args and collect_ex_args. _operator.rs drops the KwArgs import and passes zelf.args.kwargs directly to FuncArgs::new instead of wrapping it with KwArgs::new(...).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A map in a tuple, once bare and plain,
Now Deref and Default help it reign.
KwArgs::default() — no more new,
The inner IndexMap shines right through.
Hop hop hooray, the newtype's complete! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 47.37% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Use KwArgs newtype struct' directly and accurately describes the main change across all modified files: introducing and using a KwArgs newtype for keyword-argument storage throughout the codebase.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
crates/vm/src/function/argument.rs (1)

452-467: Add the mutable by-reference iterator variant for API completeness.

The newtype preserves owned iteration and for ... in &kwargs, but not the previous IndexMap pattern for ... in &mut kwargs. While DerefMut provides mutable access to the underlying map, it does not automatically enable IntoIterator desugaring for &mut KwArgs<T>. Adding IntoIterator for &mut KwArgs<T> restores compatibility with the prior iteration pattern.

♻️ Proposed iterator completion
 impl<'a, T> IntoIterator for &'a KwArgs<T> {
     type Item = (&'a String, &'a T);
     type IntoIter = indexmap::map::Iter<'a, String, T>;

     fn into_iter(self) -> Self::IntoIter {
         self.0.iter()
     }
 }

+impl<'a, T> IntoIterator for &'a mut KwArgs<T> {
+    type Item = (&'a String, &'a mut T);
+    type IntoIter = indexmap::map::IterMut<'a, String, T>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        self.0.iter_mut()
+    }
+}
+
 impl<T> IntoIterator for KwArgs<T> {
     type Item = (String, T);
     type IntoIter = indexmap::map::IntoIter<String, T>;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/vm/src/function/argument.rs` around lines 452 - 467, The KwArgs type
currently has IntoIterator implementations for immutable references and owned
values, but is missing the mutable by-reference variant. Add a third
IntoIterator implementation for &'a mut KwArgs<T> that returns an iterator over
mutable references to the key-value pairs from the underlying IndexMap. This
should follow the same pattern as the existing implementations, delegating to
self.0.iter_mut() to provide iteration over mutable references while maintaining
API compatibility with the previous IndexMap iteration pattern.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/vm/src/function/argument.rs`:
- Around line 452-467: The KwArgs type currently has IntoIterator
implementations for immutable references and owned values, but is missing the
mutable by-reference variant. Add a third IntoIterator implementation for &'a
mut KwArgs<T> that returns an iterator over mutable references to the key-value
pairs from the underlying IndexMap. This should follow the same pattern as the
existing implementations, delegating to self.0.iter_mut() to provide iteration
over mutable references while maintaining API compatibility with the previous
IndexMap iteration pattern.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: c968e56d-243c-4f0d-b125-a9bfac09ab58

📥 Commits

Reviewing files that changed from the base of the PR and between 808b071 and 7534375.

📒 Files selected for processing (3)
  • crates/vm/src/frame.rs
  • crates/vm/src/function/argument.rs
  • crates/vm/src/stdlib/_operator.rs

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.

1 participant