type.__new__: preserve caller namespace when reading __qualname__#7524
type.__new__: preserve caller namespace when reading __qualname__#7524youknowone merged 2 commits intoRustPython:mainfrom
Conversation
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [ ] test: cpython/Lib/test/test_descr.py (TODO: 38) dependencies: dependent tests: (no tests depend on descr) Legend:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors PyType::slot_new to stop popping Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/vm/src/builtins/type.rs`:
- Around line 1870-1873: The code calls dict.to_attributes(vm) before extracting
__qualname__, which can force early iteration/validation and panic on non-string
keys; instead, remove and validate the __qualname__ entry from the original dict
prior to converting to attributes. Concretely: call
dict.shift_remove(identifier!(vm, __qualname__)) (or an equivalent safe lookup)
on the incoming dict first and run downcast_qualname on that result, then call
dict.to_attributes(vm) to build attributes; reference symbols:
dict.to_attributes(vm), identifier!(vm, __qualname__), shift_remove,
downcast_qualname, and the type.__new__ namespace handling where this ordering
occurs.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 10c0b0b5-7c9d-4591-ac15-a32d110b1b27
⛔ Files ignored due to path filters (1)
Lib/test/test_descr.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/vm/src/builtins/type.rs
ShaharNaveh
left a comment
There was a problem hiding this comment.
lgmt!
ty, and welcome to the project:)
type(name, bases, ns)currently mutates the caller-provided namespace by popping__qualname__.This changes
PyType::slot_newto read__qualname__without mutating the input namespace, then remove it from the copied attributes. That keeps the caller namespace while still keeping__qualname__out of the created type’s__dict__, which matches CPython.This also removes the
@unittest.expectedFailuremarker fromtest_qualname_dict.Tested with:
cargo run --release -- -m unittest test.test_descr.ClassPropertiesAndMethods.test_qualname_dictcargo run --release -- -c "ns={'__qualname__':'some.name'}; tp=type('Foo',(),ns); print(tp.__qualname__); print('__qualname__' in tp.__dict__); print(ns)"cargo run --release -- -c "ns={1: 2, '__qualname__': 1}; type('Foo', (), ns)"cargo fmtcargo clippySummary by CodeRabbit