← Back to Pull requests

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

Closed
unknown closed this pull request without merging on Dec 01, 2026 10:55 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 21:01 UTC
Author

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.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    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.
  • <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. -->

    Diff: 490 lines (+367 / -123) across 6 files Reviewer: davidhewitt Review duration: 5153m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.4% (MEDIUM) · Higher = larger blast radius
    47.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.7% · Safe to merge
    18.7%
    Repository