Skip to content

Set {min,max}_arity #5994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
Merged

Set {min,max}_arity #5994

merged 2 commits into from
Jul 17, 2025

Conversation

ShaharNaveh
Copy link
Contributor

@ShaharNaveh ShaharNaveh commented Jul 16, 2025

Fixes #5980

Summary by CodeRabbit

  • Refactor

    • Improved error handling and type safety for argument parsing.
    • Enhanced clarity and maintainability of argument extraction logic.
    • Dynamic calculation of required and optional argument counts for better accuracy.
  • Bug Fixes

    • More robust detection and handling of multiple argument attributes, reducing potential errors in argument parsing.

Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

Walkthrough

The code refactors the logic for deriving argument parsing in procedural macros. It introduces trait-based parsing of field attributes, derives defaults for relevant types, and implements dynamic computation of arity bounds based on parsed attributes. Error handling is improved using idiomatic Rust traits, and code organization is clarified for maintainability.

Changes

File(s) Change Summary
derive-impl/src/from_args.rs Refactored attribute parsing using TryFrom traits, derived Default for types, introduced dynamic arity computation with compute_arity_bounds, and reorganized code for clarity.

Sequence Diagram(s)

sequenceDiagram
    participant Macro as Derive Macro
    participant Field as Struct Field
    participant Attr as ArgAttribute
    participant Bounds as ArityBounds

    Macro->>Field: Iterate over fields
    Field->>Attr: Parse attributes (TryFrom<&Field>)
    Macro->>Bounds: compute_arity_bounds(all ArgAttributes)
    Macro->>Macro: Generate FromArgs impl using attributes and bounds
Loading

Assessment against linked issues

Objective Addressed Explanation
Refactor FromArgs derive to compute accurate arity bounds (#5980)
Separate attribute parsing from code generation for access to kind/default info (#5980)
Implement dynamic min/max arity calculation using parsed attributes (#5980)
Use idiomatic error handling and trait-based parsing for attributes (#5980)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

In fields of code where macros play,
A rabbit hopped in, refactoring today.
With traits and bounds, defaults anew,
The arity’s right—no more guesswork to do!
🐇✨
Hop, hop, hooray,
Our arguments parse the correct way!


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 63e6c01 and 186d568.

📒 Files selected for processing (1)
  • derive-impl/src/from_args.rs (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .github/copilot-instructions.md
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Follow Rust best practices for error handling and memory management
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
🔇 Additional comments (6)
derive-impl/src/from_args.rs (6)

9-16: LGTM! Good use of derive macros.

The addition of Default derive with PositionalOrKeyword as the default variant is appropriate and follows Rust best practices.


18-30: Good refactoring to use TryFrom trait.

Converting from a custom method to the standard TryFrom trait implementation improves idiomaticity and follows Rust best practices. The implementation correctly handles all parameter kinds.


32-40: Well-structured attribute parsing with proper error handling.

The TryFrom<&Field> implementation cleanly encapsulates the attribute extraction logic with appropriate error handling for multiple pyarg attributes. Good use of the Default trait to simplify the code.

Also applies to: 105-122


123-189: Clean refactoring using the new TryFrom trait.

The function now leverages the TryFrom<&Field> implementation, making the code more concise and maintainable while preserving all the original functionality.


191-207: Correct implementation of arity bounds computation.

The function properly calculates minimum and maximum arity by considering only positional parameters and accounting for default values. This directly addresses the PR objective.


208-232: Key fix: Dynamic arity computation replaces static field count.

The implementation now correctly computes minimum and maximum arity based on actual parameter requirements rather than just counting fields. This should resolve issue #5980 by providing accurate arity bounds that account for optional parameters.

✨ 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ShaharNaveh ShaharNaveh marked this pull request as ready for review July 16, 2025 18:57
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Thank you!

@youknowone youknowone merged commit 1fcb656 into RustPython:main Jul 17, 2025
12 checks passed
@ShaharNaveh ShaharNaveh deleted the min-arity branch July 17, 2025 10:26
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.

Refactor FromArgs derive to compute accurate arity bounds
2 participants