Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@ShaharNaveh
Copy link
Collaborator

@ShaharNaveh ShaharNaveh commented Nov 29, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of super() when given mismatched type/instance combinations to avoid spurious checks and preserve existing subclass/instance routing.
    • Enhanced error messages for incompatible super(type, obj) calls, providing clearer descriptions of whether obj is a type or instance and naming the offending types for easier troubleshooting.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 29, 2025

Walkthrough

Reworked super_check in crates/vm/src/builtins/super.rs to add an initial typ-based path for PyType detection, preserve existing isinstance/class routing, and produce a more descriptive error when obj is not an instance or subtype of the provided type.

Changes

Cohort / File(s) Summary
Enhanced super() type checking
crates/vm/src/builtins/super.rs
Add an initial typ extraction path to detect PyType without immediate subclass check; keep existing isinstance and __class__ routing; build a clearer error message reporting whether obj is a type or an instance and include type names; no other control-flow changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Inspect the new typ-extraction logic and its interaction with the existing subclass checks.
  • Verify correctness and safety of downcasts used to obtain type names for error messages.
  • Confirm that existing isinstance and __class__-based paths behave identically after the change.

Suggested reviewers

  • youknowone

Poem

🐰 I hopped through code where types entwine,

peered at classes, instance and sign.
Now errors chant what once was blind,
“Type or instance?” — the answer aligned.
A joyful twitch, a clearer design.

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title mentions updating test files from 3.13.9, but the actual changes are in super.rs with logic modifications to super_check and error handling. Revise the title to reflect the actual changes: e.g., 'Improve super_check error reporting and type validation logic' or 'Refactor super_check downcast handling and error messages'.
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 (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent 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 c2ea003 and 7c86523.

📒 Files selected for processing (1)
  • crates/vm/src/builtins/super.rs (1 hunks)
🧰 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 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/super.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Check Rust code with clippy
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Ensure compilation on various targets
🔇 Additional comments (1)
crates/vm/src/builtins/super.rs (1)

242-270: I'm unable to verify the review comment because the repository failed to clone. Without access to the actual file crates/vm/src/builtins/super.rs, I cannot confirm:

  1. Whether the code snippet provided matches the current state of the file
  2. Whether unsafe downcasts were actually eliminated as claimed
  3. Whether the error message construction properly reuses the stored typ value
  4. Whether previous review concerns were genuinely addressed

Additionally, the review comment requests verification using cargo fmt and cargo clippy, which cannot be executed in the sandbox environment (Rust toolchain is unavailable).

Please provide the repository or file contents directly so I can complete the verification. Alternatively, if you have access to run the verification commands in your environment, please share those results.


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.

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: 1

📜 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 6782fa2 and c2ea003.

⛔ Files ignored due to path filters (2)
  • Lib/test/test_descr.py is excluded by !Lib/**
  • Lib/test/test_super.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/builtins/super.rs (1 hunks)
🧰 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 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/super.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check Rust code with clippy
🔇 Additional comments (1)
crates/vm/src/builtins/super.rs (1)

269-274: LGTM! Enhanced error message provides better debugging information.

The error message now clearly distinguishes between type and instance arguments and includes their names, making it much easier to diagnose super() misuse.

Comment on lines 242 to 267
let is_type = match obj.clone().downcast::<PyType>() {
Ok(cls) if cls.fast_issubclass(&ty) => return Ok(cls),
Ok(_) => true,
Err(_) => false,
};

if obj.fast_isinstance(&ty) {
return Ok(obj.class().to_owned());
}

let class_attr = obj.get_attr("__class__", vm)?;
if let Ok(cls) = class_attr.downcast::<PyType>()
&& !cls.is(&ty)
&& cls.fast_issubclass(&ty)
{
return Ok(cls);
}
Err(vm.new_type_error("super(type, obj): obj must be an instance or subtype of type"))

let (type_or_instance, obj_str) = if is_type {
// SAFETY: This is will always be successful as we already checked at the begining of
// the function.
let typ = unsafe { obj.downcast::<PyType>().unwrap_unchecked() };
("type", typ.name().to_owned())
} else {
("instance of", obj.class().name().to_owned())
};
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix typo and avoid unsafe downcast by storing the type.

Line 261 has a typo: "This is will always be successful" should be "This will always be successful".

More importantly, the unsafe downcast at line 263 can be avoided. The current code discards the downcasted PyType in the Ok(_) branch at line 244, then later performs an unsafe downcast to retrieve it. Instead, store the downcasted type when is_type is true and reuse it, eliminating the need for unsafe code.

Apply this diff to fix the typo and avoid unsafe:

-    let is_type = match obj.clone().downcast::<PyType>() {
+    let obj_type = match obj.clone().downcast::<PyType>() {
         Ok(cls) if cls.fast_issubclass(&ty) => return Ok(cls),
-        Ok(_) => true,
-        Err(_) => false,
+        Ok(cls) => Some(cls),
+        Err(_) => None,
     };
+    let is_type = obj_type.is_some();
 
     if obj.fast_isinstance(&ty) {
         return Ok(obj.class().to_owned());
     }
 
     let class_attr = obj.get_attr("__class__", vm)?;
     if let Ok(cls) = class_attr.downcast::<PyType>()
         && !cls.is(&ty)
         && cls.fast_issubclass(&ty)
     {
         return Ok(cls);
     }
 
-    let (type_or_instance, obj_str) = if is_type {
-        // SAFETY: This is will always be successful as we already checked at the begining of
-        // the function.
-        let typ = unsafe { obj.downcast::<PyType>().unwrap_unchecked() };
-        ("type", typ.name().to_owned())
+    let (type_or_instance, obj_str) = if let Some(typ) = obj_type {
+        ("type", typ.name().to_owned())
     } else {
         ("instance of", obj.class().name().to_owned())
     };
🤖 Prompt for AI Agents
crates/vm/src/builtins/super.rs around lines 242-267: the code currently throws
away the successful downcasted PyType in the match and later does an unsafe
downcast and has a typo in the comment; change the match to capture and store
the downcasted PyType (e.g. Option<PyTypeRef> or similar) instead of a boolean
flag, use that stored PyType when building type_or_instance (avoiding unsafe
downcast), update the comment to "This will always be successful", and remove
the unsafe unwrap_unchecked usage so the stored safe reference is reused.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

👍

@youknowone youknowone merged commit b84f7c1 into RustPython:main Nov 30, 2025
13 checks passed
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.