Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Jan 2, 2026

Either #6485 or this one is selectable.

Summary by CodeRabbit

  • Refactor
    • Improved error messages for special method implementations, providing enhanced guidance on correct implementation approaches.

✏️ Tip: You can customize this high-level summary in your review settings.

@youknowone youknowone requested a review from ShaharNaveh January 2, 2026 03:18
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 2026

📝 Walkthrough

Walkthrough

Replaces hard-coded restrictions on __new__ and __init__ methods with a generic FORBIDDEN_SLOT_METHODS list that maps slot-method names to their corresponding traits, enabling expanded method validation and improved error messaging.

Changes

Cohort / File(s) Summary
Slot method validation refactoring
crates/derive-impl/src/pyclass.rs
Replaced hard-coded disallowance of __new__ and __init__ with generic FORBIDDEN_SLOT_METHODS map linking method names to trait names; error messages now include method name and associated trait with guidance to implement via trait; slot methods noted as automatically exposed as wrapper_descriptor

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A mapping so clever, from slots to their traits,
Forbidden methods now documented with care,
Error messages guide with a helpful repair,
"Implement the trait!" rings clear through the air,
Validation grows stronger, no method's too rare! 🎯

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 'Warn user not to override special magic methods' directly matches the PR's objective to prevent users from overriding special magic methods, as evidenced by the code changes expanding the FORBIDDEN_SLOT_METHODS list and improving error messages.
✨ 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.

@youknowone youknowone marked this pull request as ready for review January 2, 2026 05:59
@youknowone
Copy link
Member Author

hmm, actually both selectable.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
crates/derive-impl/src/pyclass.rs (1)

915-1000: Document rationale for commented-out forbidden methods.

Several slot methods are commented out from FORBIDDEN_SLOT_METHODS without clear explanation:

  1. Line 920: __repr__ is commented with no explanation
  2. Line 921: __str__ has a placeholder "???" for the trait name
  3. Lines 930-932: Descriptor methods (__get__, __set__, __delete__) are commented out
  4. Lines 984-992: Sequence/mapping/iterator methods (__len__, __contains__, __getitem__, __setitem__, __delitem__, __iter__, __next__) are commented out

While line 927-928 provides excellent documentation for why __getattribute__, __setattr__, and __delattr__ are intentionally not forbidden, the other commented-out entries lack similar justification.

Please either:

  • Add explanatory comments for each commented-out entry explaining why these methods are allowed to be implemented via #[pymethod]
  • Uncomment entries that should be forbidden and provide the correct trait names
  • Remove commented-out entries if they're not relevant to keep the code clean
Example documentation pattern
 // Representable trait
-// ("__repr__", "Representable"),
-// ("__str__", "???"), // allow __str__
+// NOTE: __repr__ and __str__ are intentionally allowed via pymethod because [reason]
+// ("__repr__", "Representable"),
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2e29248 and d0252ad.

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

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • crates/derive-impl/src/pyclass.rs
🔇 Additional comments (1)
crates/derive-impl/src/pyclass.rs (1)

1002-1014: Excellent error message for forbidden slot methods.

The check logic and error message are well-designed:

  • Clear identification of which method is forbidden
  • Actionable guidance to use trait implementation instead
  • Educational note that slot methods are exposed as wrapper_descriptor automatically
  • Correctly scoped to non-trait implementations only

This provides a good developer experience by guiding users toward the correct pattern.

Copy link
Collaborator

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

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

LGTM!

@youknowone youknowone merged commit 346519b into RustPython:main Jan 2, 2026
13 checks passed
@youknowone youknowone deleted the forbid-special-methods branch January 2, 2026 08:49
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.

2 participants