-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement property.__name__ attribute #6230
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
Implement property.__name__ attribute #6230
Conversation
Add getter and setter for the __name__ attribute on property objects. The getter returns the explicitly set name if available, otherwise falls back to the getter function's __name__. Raises AttributeError if no name is available, matching CPython 3.13 behavior. The implementation handles edge cases: - Returns None when explicitly set to None - Propagates non-AttributeError exceptions from getter's __getattr__ - Raises property-specific AttributeError when getter lacks __name__ This fix enables test_property_name in test_property.py to pass.
WalkthroughChange makes PyProperty's name resolution return a fallible result (PyResult<Option>), treats AttributeError from underlying getters as "no name", adds public Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant PyCode as Python code
participant PyProperty as PyProperty (pygetset)
participant VM as VirtualMachine
participant Getter as underlying getter (optional)
PyCode->>PyProperty: access __name__ (get)
PyProperty->>VM: name_getter(vm)
alt stored name present
VM-->>PyProperty: return stored name
PyProperty-->>PyCode: name
else no stored name
PyProperty->>Getter: inspect/call Getter.__name__
alt Getter returns name
Getter-->>PyProperty: name
PyProperty-->>PyCode: name
else Getter raises AttributeError
Getter-->>PyProperty: AttributeError
PyProperty-->>PyCode: treat as "no name" -> raise AttributeError or return None per API
else Getter raises other error
Getter-->>PyProperty: Other error
PyProperty-->>PyCode: propagate error
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (2)**/*.rs📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
{vm,stdlib}/**/*.rs📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
🧬 Code graph analysis (1)vm/src/builtins/property.rs (2)
🔇 Additional comments (3)
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 |
|
Thank you! I thought we already had name handling on pyproperty, but it was |
Consolidate duplicate logic by making name_getter() use the existing get_property_name() helper method. This eliminates code duplication and improves maintainability. Changes: - Update get_property_name() to return PyResult<Option<PyObjectRef>> to properly handle and propagate non-AttributeError exceptions - Simplify name_getter() to delegate to get_property_name() - Update format_property_error() to handle the new return type This addresses review feedback about the relationship between get_property_name() and __name__ implementation.
youknowone
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. I left a style suggestion. Please apply it if it looks good
vm/src/builtins/property.rs
Outdated
| if let Some(getter) = self.getter.read().as_ref() { | ||
| match getter.get_attr("__name__", vm) { | ||
| Ok(name) => Ok(Some(name)), | ||
| Err(e) => { | ||
| // If it's an AttributeError from the getter, return None | ||
| // Otherwise, propagate the original exception (e.g., RuntimeError) | ||
| if e.class().is(vm.ctx.exceptions.attribute_error) { | ||
| Ok(None) | ||
| } else { | ||
| Err(e) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| Ok(None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if let Some(getter) = self.getter.read().as_ref() { | |
| match getter.get_attr("__name__", vm) { | |
| Ok(name) => Ok(Some(name)), | |
| Err(e) => { | |
| // If it's an AttributeError from the getter, return None | |
| // Otherwise, propagate the original exception (e.g., RuntimeError) | |
| if e.class().is(vm.ctx.exceptions.attribute_error) { | |
| Ok(None) | |
| } else { | |
| Err(e) | |
| } | |
| } | |
| } | |
| } else { | |
| Ok(None) | |
| let Some(getter) = self.getter.read().as_ref() { | |
| return Ok(None); | |
| }; | |
| match getter.get_attr("__name__", vm) { | |
| Ok(name) => Ok(Some(name)), | |
| Err(e) => { | |
| // If it's an AttributeError from the getter, return None | |
| // Otherwise, propagate the original exception (e.g., RuntimeError) | |
| if e.class().is(vm.ctx.exceptions.attribute_error) { | |
| Ok(None) | |
| } else { | |
| Err(e) | |
| } | |
| } |
|
I've applied the review. thanks! |
| { | ||
| return Some(name); | ||
| } | ||
| let getter = self.getter.read(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an extra variable because a temporary value issue prevented a single-line implementation.
Add getter and setter for the name attribute on property objects. The getter returns the explicitly set name if available, otherwise falls back to the getter function's name. Raises AttributeError if no name is available, matching CPython 3.13 behavior.
The implementation handles edge cases:
This fix enables test_property_name in test_property.py to pass.
Summary by CodeRabbit
New Features
Bug Fixes