8000 Fix dict.keys behavior by youknowone · Pull Request #6476 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@youknowone
Copy link
Member
@youknowone youknowone commented Dec 24, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved dictionary merging behavior when processing object attributes, with enhanced error handling for edge cases involving missing keys methods.

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

@coderabbitai
Copy link
Contributor
coderabbitai bot commented Dec 24, 2025
📝 Walkthrough

Walkthrough

The merge_object function in the dict module was refactored to resolve the keys method using Python attribute resolution semantics via get_attr rather than direct method lookup. Error handling now distinguishes between AttributeError (triggering fallback to legacy iteration) and other errors (propagated immediately).

Changes

Cohort / File(s) Summary
Dict merge object refactoring
crates/vm/src/builtins/dict.rs
Modified merge_object to use get_attr for keys method resolution instead of vm.get_method, adding explicit AttributeError fallback handling that defers to legacy tuple-based iteration when keys method is unavailable. Other errors propagate immediately.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A rabbit hops through attribute trees, 🐰
Finding keys where once were mysteries,
Fallback paths when nothing's found,
Error handling, oh so sound!
Refactored swift, semantics clean. ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: fixing dict.keys behavior, which aligns with the refactored attribute resolution logic in the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@youknowone youknowone marked this pull request as ready for review December 24, 2025 04:15
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: 0

🧹 Nitpick comments (1)
crates/vm/src/builtins/dict.rs (1)

66-107: Consider testing with proxy objects and edge cases.

While the implementation is correct, the behavioral change should be thoroughly tested:

  1. Proxy objects: Verify that proxy objects with overridden __getattribute__ now correctly invoke the keys() method
  2. Non-callable keys attribute: Objects with a non-callable keys attribute will now raise TypeError instead of falling back to iteration (this is more correct Python behavior)
  3. Error propagation: Verify that errors raised during keys() iteration are properly propagated

These test scenarios will help ensure the fix works as intended for the reported issue.

📜 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 00205aa and 45d2c42.

📒 Files selected for processing (1)
  • crates/vm/src/builtins/dict.rs
🧰 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/dict.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). (10)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Check Rust code with clippy
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check the WASM package and demo
🔇 Additional comments (2)
crates/vm/src/builtins/dict.rs (2)

73-86: LGTM! Proper use of get_attr for Python attribute resolution semantics.

The refactor correctly uses get_attr to invoke __getattribute__, which is essential for proxy objects that override attribute access. The error handling is well-structured:

  • Ok(keys_method): Successfully calls the method and iterates over keys
  • Err(AttributeError): Falls back to legacy iteration (no keys method)
  • Err(other): Properly propagates errors (e.g., TypeError if keys is not callable, errors during iteration)

This aligns with the PR objective to fix dict.keys behavior for proxy objects.


87-105: LGTM! Fallback logic correctly handles objects without keys method.

The legacy iteration path properly handles objects that don't provide a keys() method by:

  • Iterating over the object directly
  • Validating each element is a 2-tuple (key, value)
  • Inserting each pair into the dictionary

The validation ensures exactly two elements per tuple, raising a clear error message otherwise.

@youknowone youknowone merged commit c763d67 into RustPython:main Dec 24, 2025
13 checks passed
@youknowone youknowone deleted the dict-keys branch December 24, 2025 04:50
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.

1 participant

0