Add range start/stop/step attributes and count/index methods #1463
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 490 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.371
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1463"
general
weight: 0.35
davidhewitt
Summary
rangeobjects had nostart,stoporstepattributes and nocount()/index()methods, although the type-checker stubs advertise all five, so code liker.startorr.index(x)passed--type-checkand then raisedAttributeErrorat runtime. This is therangepart 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.#[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.countwithout calling it still fails, the same as for every other builtin method (see "Builtin methods are call-only" inlimitations/builtins.md). I left that alone here.Refs #1319
Tests
Added a
range attributes and count/indexsection tocrates/monty/test_cases/range__ops.py. It covers the attributes, read-only assignment, int/bool/float/str/big-intcountandindex, negative steps, values near2**63, unbound calls and the exact error messages.range__ops.pyfails on Monty (AttributeError: 'range' object has no attribute 'start') and passes on CPython 3.14.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.False,-0.0,complex(3, 0), reversed slices, fulli64span), and all matched.<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Fills in the
rangeattributes and methods the type checker advertises, so code liker.startorr.index(x)no longer passes type-checking and then raisesAttributeErrorat runtime. This is therangepart of group 2 for #1319.start,stop, andstepattributes; assignment raisesAttributeError: readonly attribute.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.#[pyattrs]liketuple.count/tuple.index, sorange.count(r, 4)also works, and argument-count and keyword-argument error messages match CPython.<sup>Written for commit fa5a7f066d82ba7ffee9c3fc61ae98fcdb8bc0ee. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/1463?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->