Update test_types.py to 3.14.5#7912
Conversation
📝 WalkthroughWalkthroughThis PR adds a runtime type check in PyNamespace::replace to ensure the object returned by calling the original namespace class has the same runtime type as the original object, raising a TypeError with expected and actual type names on mismatch. ChangesNamespace replace type validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/types.py dependencies:
dependent tests: (55 tests)
Legend:
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/vm/src/builtins/namespace.rs`:
- Line 60: The error message in __replace__ is using
Self::class(&vm.ctx).slot_name() which always yields the base "SimpleNamespace";
change it to use the runtime object's class name instead (e.g., call
class(&vm.ctx).slot_name() on the instance variable used in __replace__, such as
the `obj`/`value` parameter) so the message reports the actual subclass name at
runtime.
- Around line 61-65: The code currently calls
zelf.class().__qualname__(vm).downcast_ref::<PyStr>().map(|n|
n.as_wtf8()).unwrap(), which can panic if __qualname__ is not a PyStr; change
this to handle the downcast failure safely by falling back to
zelf.class().name(vm) (or otherwise returning an Err/PyErr) instead of
unwrapping: attempt the downcast_ref::<PyStr>(), map to as_wtf8() when present,
and if None use name() (or return a Python exception via the VM) so the VM
raises a proper error rather than panicking.
🪄 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: 3ffd5eda-543b-42ee-ac9c-87503eee6d43
⛔ Files ignored due to path filters (1)
Lib/test/test_types.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/vm/src/builtins/namespace.rs
|
coderabbits comments seems worth to check |
There was a problem hiding this comment.
♻️ Duplicate comments (1)
crates/vm/src/builtins/namespace.rs (1)
60-60:⚠️ Potential issue | 🟠 Major | ⚡ Quick winUse the runtime class name instead of the base class name in the error message.
Line 60 uses
Self::class(&vm.ctx).slot_name(), which always returns"SimpleNamespace"regardless of the actual runtime type. If__replace__is called on a subclass instance, the error message will incorrectly state it expectedSimpleNamespacewhen it should reference the subclass name.🔧 Proposed fix
return Err(vm.new_type_error(format!( "expect {} type, but {}() returned '{}' object", - Self::class(&vm.ctx).slot_name(), + zelf.class().slot_name(), zelf.class()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/vm/src/builtins/namespace.rs` at line 60, The error message in the __replace__ implementation uses Self::class(&vm.ctx).slot_name(), which always yields the base class name ("SimpleNamespace") instead of the actual runtime type; change the code to fetch the instance's runtime class name (e.g., use value.class(&vm.ctx).slot_name() or the equivalent method on the instance being checked) and include that runtime name in the process error/TypeError message so subclasses are reported correctly; update the __replace__ error branch in namespace.rs to reference the instance's class name rather than Self::class(&vm.ctx).slot_name().
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@crates/vm/src/builtins/namespace.rs`:
- Line 60: The error message in the __replace__ implementation uses
Self::class(&vm.ctx).slot_name(), which always yields the base class name
("SimpleNamespace") instead of the actual runtime type; change the code to fetch
the instance's runtime class name (e.g., use value.class(&vm.ctx).slot_name() or
the equivalent method on the instance being checked) and include that runtime
name in the process error/TypeError message so subclasses are reported
correctly; update the __replace__ error branch in namespace.rs to reference the
instance's class name rather than Self::class(&vm.ctx).slot_name().
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: f77484af-ecd2-46e0-886d-495e0f4b76e2
📒 Files selected for processing (1)
crates/vm/src/builtins/namespace.rs
Summary by CodeRabbit