Add range start/stop/step attributes and count/index methods

Merged
LE
4d ago
+174-2

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, step are read-only attributes; assigning raises AttributeError: readonly attribute.
  • count(x) / index(x) use O(1) arithmetic for int and bool (computed in i128, so ranges near the i64 bounds 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') raises sequence.index(x): x not in sequence). The fallback loop polls the time limit.
  • The methods are registered through #[pyattrs] like tuple.count / tuple.index, so range.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.py fails 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-return and --features memory-model-checks.

  • cargo clippy -p monty --all-targets -- -D warnings, cargo fmt --check, scripts/check_imports.py and cargo test -p monty --lib pass.

  • I also compared about 20 extra edge cases against CPython 3.14 (empty ranges, False, -0.0, complex(3, 0), reversed slices, full i64 span), 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, and step attributes; assignment raises AttributeError: readonly attribute.
  • Adds count() and index() matching CPython: O(1) arithmetic lookup for int/bool (computed in i128 to avoid overflow for ranges near the i64 bounds), 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] like tuple.count/tuple.index, so range.count(r, 4) also works, and argument-count and keyword-argument error messages match CPython.