Add range start/stop/step attributes and count/index methods
Summary
range objects had no start, stop or step attributes and no count() / index() methods, although the type-checker stubs advertise all five, so code like r.start or r.index(x) passed --type-check and then raised AttributeError at runtime. This is the range part of group 2 in #1319.
This PR adds them with CPython's semantics:
start,stop,stepare read-only attributes; assigning raisesAttributeError: readonly attribute.count(x)/index(x)use O(1) arithmetic forintandbool(computed ini128, so ranges near thei64bounds don't overflow); big ints are never members. Any other value falls back to==against each element, like CPython (range(10).count(4.0) == 1,range(10).index('a')raisessequence.index(x): x not in sequence). The fallback loop polls the time limit.- The methods are registered through
#[pyattrs]liketuple.count/tuple.index, sorange.count(r, 4)also works. The error messages for wrong argument counts and keyword arguments match CPython.
Reading r.count without calling it still fails, the same as for every other builtin method (see "Builtin methods are call-only" in limitations/builtins.md). I left that alone here.
Refs #1319
Tests
Added a range attributes and count/index section to crates/monty/test_cases/range__ops.py. It covers the attributes, read-only assignment, int/bool/float/str/big-int count and index, negative steps, values near 2**63, unbound calls and the exact error messages.
-
Before the fix,
range__ops.pyfails on Monty (AttributeError: 'range' object has no attribute 'start') and passes on CPython 3.14. -
After the fix,
cargo run -p monty-datatest -- range__passes on Monty and CPython (12/12), including with--features ref-count-returnand--features memory-model-checks. -
cargo clippy -p monty --all-targets -- -D warnings,cargo fmt --check,scripts/check_imports.pyandcargo test -p monty --libpass. -
I also compared about 20 extra edge cases against CPython 3.14 (empty ranges,
False,-0.0,complex(3, 0), reversed slices, fulli64span), and all matched. -
I have read the contribution guide.
-
I understand and can explain the proposed changes, including any generated with AI assistance.
Summary by cubic
Fills in the range attributes and methods the type checker advertises, so code like r.start or r.index(x) no longer passes type-checking and then raises AttributeError at runtime. This is the range part of group 2 for #1319.
- Adds read-only
start,stop, andstepattributes; assignment raisesAttributeError: readonly attribute. - Adds
count()andindex()matching CPython: O(1) arithmetic lookup forint/bool(computed ini128to avoid overflow for ranges near thei64bounds), big ints are never members, and any other value falls back to==against each element with a time-limit poll that checks once before looping, so short ranges with nested__eq__callbacks are also bounded. - Registers the methods via
#[pyattrs]liketuple.count/tuple.index, sorange.count(r, 4)also works, and argument-count and keyword-argument error messages match CPython.