| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command. You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file. Use the checkbox below for a quick retry:
WalkthroughThe iter_wrapper function in slot.rs was refactored to explicitly look up the __iter__ special method on objects rather than directly invoking it, with added error handling to distinguish between missing methods and methods explicitly set to None, raising appropriate AttributeError or TypeError exceptions. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem🚥 Pre-merge checks | ✅ 2 | ❌ 1 ❌ Failed checks (1 warning)
✏️ 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. ❤️ ShareComment @coderabbitai help to get the list of available commands and usage tips. |
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agentsIn `@crates/vm/src/types/slot.rs`:
- Around line 548-551: Replace the AttributeError raised when __iter__ is
missing with a TypeError: in the block that calls vm.get_special_method(&zelf,
identifier!(vm, __iter__)), change the ok_or_else(...) from
vm.new_attribute_error(...) to vm.new_type_error(...) and construct the message
as "'<ClassName>' object is not iterable" using the object's class name (e.g.,
format!("'{}' object is not iterable", zelf.class().name())). Keep the existing
method_is_none() TypeError behavior unchanged.
crates/vm/src/types/slot.rs (2)545-551: Decorative comment separators violate coding guidelines.
Lines 545–547 and 553–556 use blank // lines as section separators. As per coding guidelines, "Do not add decorative section separators (e.g. // -----------, // ===, /* *** */). Use /// doc-comments or short // comments only when they add value."
Proposed fix: collapse to single-line comments- // - // look-up '__iter__' method - // + // Look up __iter__ method let method_ident = identifier!(vm, __iter__); let method = vm .get_special_method(&zelf, method_ident)? .ok_or_else(|| vm.new_attribute_error(method_ident.as_str().to_owned()))?; - // - // setting '__iter__' method to 'None' value is used - // to mark non-iterable objects, raise TypeError - // + // __iter__ = None marks non-iterable objects if method_is_none(&method, vm) {
537-562: Consider consistency with __hash__ = None handling.
The __hash__ = None case (lines 758–783) is handled at slot-update time by storing hash_not_implemented as the slot function. The __iter__ = None case is handled at call time inside the wrapper. Both approaches work, but the inconsistency may confuse future maintainers. If there's no strong reason to differ, consider aligning the pattern (e.g., storing a dedicated iter_not_implemented function during slot update, similar to hash_not_implemented).
That said, CPython's slot_tp_iter also checks at call time, so the current approach is arguably more faithful to CPython.
Sorry, something went wrong.
| let method_ident = identifier!(vm, __iter__); | ||
| let method = vm | ||
| .get_special_method(&zelf, method_ident)? | ||
| .ok_or_else(|| vm.new_attribute_error(method_ident.as_str().to_owned()))?; |
There was a problem hiding this comment.
⚠️ Potential issue | 🟠 Major
🧩 Analysis chain🏁 Script executed:
#!/bin/bash
# Verify what error CPython raises when __iter__ is dynamically deleted after being set
# Also check how get_special_method works and what it returns for None vs missing
echo "=== Check get_special_method implementation ==="
ast-grep --pattern 'fn get_special_method($$$) {
$$$
}'
echo ""
echo "=== Look for get_special_method definition ==="
rg -n 'fn get_special_method' crates/vm/src/ -A 20 | head -50Repository: RustPython/RustPython
Length of output: 1467
🏁 Script executed:
#!/bin/bash
echo "=== Check iter_wrapper and surrounding context in slot.rs ==="
rg -n 'iter_wrapper\|__iter__' crates/vm/src/types/slot.rs -B 5 -A 10Repository: RustPython/RustPython
Length of output: 126
🏁 Script executed:
#!/bin/bash
echo "=== Check how other slot wrappers handle missing methods ==="
rg -n 'new_attribute_error|new_type_error' crates/vm/src/types/slot.rs -B 3 -A 1 | head -100Repository: RustPython/RustPython
Length of output: 4724
🏁 Script executed:
#!/bin/bash
echo "=== Check if there are references to 'not iterable' error messages ==="
rg -n 'not iterable' crates/vm/src/ -B 2 -A 2Repository: RustPython/RustPython
Length of output: 1234
🏁 Script executed:
#!/bin/bash
echo "=== Check get_special_method behavior in the codebase ==="
rg -n 'get_special_method.*ok_or' crates/vm/src/ -B 3 -A 3 | head -60Repository: RustPython/RustPython
Length of output: 122
Change error type from AttributeError to TypeError when __iter__ is missing.
The code at line 551 currently raises AttributeError when get_special_method returns None (i.e., when __iter__ is not found). This is inconsistent with CPython, which raises TypeError: '<ClassName>' object is not iterable. This error type also aligns with how other slot wrappers in the codebase handle missing methods—len_wrapper, repr_wrapper, str_wrapper, and others all use new_type_error() for similar cases.
The method_is_none() check at line 557 handles the case where __iter__ exists but returns None, which should also raise the same TypeError (already correct).
Proposed fix let method_ident = identifier!(vm, __iter__);
let method = vm
.get_special_method(&zelf, method_ident)?
- .ok_or_else(|| vm.new_attribute_error(method_ident.as_str().to_owned()))?;
+ .ok_or_else(|| vm.new_type_error(format!("'{}' object is not iterable", zelf.class().name())))?;‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| let method_ident = identifier!(vm, __iter__); | |
| let method = vm | |
| .get_special_method(&zelf, method_ident)? | |
| .ok_or_else(|| vm.new_attribute_error(method_ident.as_str().to_owned()))?; | |
| let method_ident = identifier!(vm, __iter__); | |
| let method = vm | |
| .get_special_method(&zelf, method_ident)? | |
| .ok_or_else(|| vm.new_type_error(format!("'{}' object is not iterable", zelf.class().name())))?; |
In `@crates/vm/src/types/slot.rs` around lines 548 - 551, Replace the
AttributeError raised when __iter__ is missing with a TypeError: in the block
that calls vm.get_special_method(&zelf, identifier!(vm, __iter__)), change the
ok_or_else(...) from vm.new_attribute_error(...) to vm.new_type_error(...) and
construct the message as "'<ClassName>' object is not iterable" using the
object's class name (e.g., format!("'{}' object is not iterable",
zelf.class().name())). Keep the existing method_is_none() TypeError behavior
unchanged.
Sorry, something went wrong.
|
Thanks for the PR! |
Sorry, something went wrong.
Thanks for investigating! |
Sorry, something went wrong.
|
@elmjag please merge main, it should pass now |
Sorry, something went wrong.
I'll look into using this approach for __iter__ = None case. Sounds like a better way to implement it, marking this PR as draft for now. |
Sorry, something went wrong.
Setting special `__iter__` method to `None` is used to mark objects as non-iterable. https://docs.python.org/3/reference/datamodel.html#special-method-names Check if `__iter__` is set to `None` and raise TypeError exception.
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/typing.py dependencies:
dependent tests: (12 tests)
Legend:
|
Sorry, something went wrong.
|
@elmjag can you please resolve the conflicts? I think it can be merged |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Setting special __iter__ method to None is used to mark objects as non-iterable.
https://docs.python.org/3/reference/datamodel.html#special-method-names
Check if __iter__ is set to None and raise TypeError exception.
Summary by CodeRabbit