__slots__ xor __dict__ , name mangling by youknowone · Pull Request #6392 · RustPython/RustPython
Repository navigation
__slots__ xor __dict__ , name mangling - #6392
Merged
youknowone merged 2 commits into RustPython:main from youknowone:slots Dec 10, 2025
Merged
__slots__ xor __dict__ , name mangling #6392 youknowone merged 2 commits into RustPython:main from youknowone:slots
Conversation
youknowone
commented
Dec 10, 2025
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
Member
Summary by CodeRabbit
- Bug Fixes
- Improved object attribute initialization based on class configuration
- Fixed type inheritance to properly handle base class attributes
- Enhanced support for generic typing and parameterized types
- Corrected Python name mangling for private attributes in class definitions
✏️ Tip: You can customize this high-level summary in your review settings.
coderabbitai
Bot
commented
Dec 10, 2025
·
edited
Loading
Copy link Copy Markdown
Contributor
WalkthroughThe pull request modifies object and type construction in RustPython. It replaces a hardcoded object-type check with a HAS_DICT flag check for dict allocation, inherits HAS_DICT from all bases in the MRO during type creation, introduces Python name mangling for private slot attributes, and adds new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
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. ❤️ ShareComment |
d8cdc72 to
8165766
Compare
December 10, 2025 11:58 youknowone changed the title __slots__ xor __dict__ __slots__ xor __dict__ , name mangling Dec 10, 2025 youknowone added 2 commits December 10, 2025 21:42
__slots__ xor __dict__
f893da6
mangle_name for __ prefixed members
8718dc4
youknowone
force-pushed
the
slots
branch
from
8165766 to
8718dc4
Compare
December 10, 2025 12:42 youknowone marked this pull request as ready for review December 10, 2025 13:00 coderabbitai
Bot
reviewed Dec 10, 2025
View reviewed changes
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 .
Actionable comments posted: 0
🧹 Nitpick comments (1)crates/vm/src/builtins/type.rs (1)📜 Review details
1798-1808: Handle edge case when class name consists entirely of underscores.When
class_nameconsists only of underscores (e.g.,_,__,___),trim_start_matches('_')returns an empty string. This causes the code to produce"___x"for a name like"__x"in such a class, which diverges from CPython's behavior -- CPython does not mangle names in classes whose names consist only of underscores and returns the original name unchanged.Add a check after stripping the leading underscores:
fn mangle_name(class_name: &str, name: &str) -> String { // Only mangle names starting with __ and not ending with __ if !name.starts_with("__") || name.ends_with("__") || name.contains('.') { return name.to_string(); } // Strip leading underscores from class name let class_name = class_name.trim_start_matches('_'); + if class_name.is_empty() { + return name.to_string(); + } format!("_{}{}", class_name, name) }
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between 98fff96 and 8718dc4 .
⛔ Files ignored due to path filters (10)Lib/test/datetimetester.pyis excluded by!Lib/**Lib/test/test_contextlib.pyis excluded by!Lib/**Lib/test/test_contextlib_async.pyis excluded by!Lib/**Lib/test/test_dataclasses.pyis excluded by!Lib/**Lib/test/test_decimal.pyis excluded by!Lib/**Lib/test/test_fractions.pyis excluded by!Lib/**Lib/test/test_functools.pyis excluded by!Lib/**Lib/test/test_os.pyis excluded by!Lib/**Lib/test/test_statistics.pyis excluded by!Lib/**Lib/test/test_typing.pyis excluded by!Lib/**
crates/vm/src/builtins/object.rs(1 hunks)crates/vm/src/builtins/type.rs(4 hunks)crates/vm/src/stdlib/typevar.rs(2 hunks)
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint Rust 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/vm/src/builtins/object.rscrates/vm/src/stdlib/typevar.rscrates/vm/src/builtins/type.rs
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
crates/vm/src/builtins/type.rs
- GitHub Check: Run snippets and cpython tests (windows-latest)
crates/vm/src/builtins/type.rs (3)youknowone merged commitcrates/vm/src/builtins/object.rs (1)
310-317: LGTM! Correct HAS_DICT inheritance from MRO.The change from checking only the first base to checking any base in the MRO aligns with CPython's behavior. If any base class in the MRO has
HAS_DICT, the subclass should inherit it too since it needs to support instance dictionaries from any base.
1188-1213: LGTM! Name mangling correctly applied to slot-backed member descriptors.The implementation properly:
- Extracts the class name before processing slots
- Applies mangling to each slot attribute name
- Uses the mangled name consistently for both the
PyMemberDefand the attribute key on the typeThis matches CPython's behavior where
__xin__slots__becomes_ClassName__xas the actual attribute.
1233-1246: LGTM! Correct conditional addition of__dict__descriptor.The updated condition ensures the
__dict__descriptor is only added when:
- The base is not
type(type subclasses inherit__dict__from type)- The class has the
HAS_DICTflag (meaning__slots__was not defined, or__dict__was explicitly in__slots__)This correctly implements the
__slots__ xor __dict__semantics.crates/vm/src/stdlib/typevar.rs (3)
69-79: LGTM! Dict creation now driven byHAS_DICTflag.The change correctly ties instance dict creation to the
HAS_DICTflag rather than a hardcoded type check. This ensures:
- Classes with
__slots__(no__dict__in slots) → no instance dict- Classes without
__slots__or with__dict__in__slots__→ instance dict createdThis aligns with the type initialization changes and properly implements Python's
__slots__semantics.
3-3: LGTM!The
Contextimport is required for the new__slots__method signature.
975-978: LGTM! Empty__slots__on Generic matches CPython.Defining
__slots__ = ()onGenericprevents instances from having an instance dictionary, which is the expected behavior for this typing construct. Using#[pyattr]correctly defines this as a class attribute.
986-988: LGTM!__init_subclass__delegation to typing module.The implementation correctly delegates to
_generic_init_subclassin the typing module, consistent with the existing__class_getitem__pattern and CPython's behavior for handling Generic subclass initialization.
1b17587
into
RustPython : main
Dec 10, 2025
youknowone
deleted the
slots
branch
December 10, 2025 14:30
youknowone
mentioned this pull request
Dec 12, 2025
Add regression test for #4505
#6417
Merged
This was referenced Dec 26, 2025
Prevent __class__ reassignment across incompatible layouts
#6521
Merged
impl basicsize
#6557
Merged
This was referenced Mar 4, 2026
Object header slimming: prefix allocation for ObjExt
#7334
Merged
Harden CALL specialization guards and cache callables
#7360
Merged
Separate WeakRefList from ObjExt prefix
#7365
Merged
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment