← Back to Pull requests

Allow `list`, `tuple`, `...` constructors to suspend #1479

Open
unknown opened this pull request with 393 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 29, 2026 20:57 UTC
Author

Collection constructors currently raise NotImplementedError when a custom iterator, generator, or unread file needs a host call while it is being consumed. For example, list(open(path)) and shlex.split(open(path)) fail on an unread file.

Allow list, tuple, set, frozenset, dict, bytes, deque, Counter, and defaultdict constructors to suspend while iterating. Keep synchronous paths for known safe inputs; the other paths use frozen frames that survive session dumps. Set insertion, dict pair validation, byte conversion, and counting happen as items arrive so errors stop the source before it advances again. Synchronous callbacks within these operations retain their existing limitations.

Tuple and frozenset construction use temporary mutable collections; bytes construction uses a temporary byte buffer.

Tests:

  • cargo test -p monty --features memory-model-checks

  • Monty fixture suite with memory-model-checks (831 passed)

  • Collection suspension cases against CPython, including incremental errors and dump/restore

  • cargo clippy -p monty --all-targets --features memory-model-checks -- -D warnings
  • Related: #1416, #1475.

    <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    list(), tuple(), set(), frozenset(), dict(), bytes(), collections.deque(), collections.Counter(), and collections.defaultdict() constructors now suspend while consuming iterables that need host calls, so patterns like list(open(path)) work on unread files instead of raising NotImplementedError.

  • Fast synchronous paths run first — native iterables flagged sync_iterable, dict sources, and exact-type passthroughs (tuples, frozensets, bytes) never enter a Python frame. map and filter callbacks can now suspend when driven by a collection constructor, for, or functools.reduce(); sorted(), sum, and itertools adaptors still cannot.

  • Suspension uses frozen frames that survive session dumps; insertion, pair validation, byte conversion, and counting run item-by-item so errors stop the source before it advances again. tuple(), frozenset(), and bytes() build through temporary mutable collections on the suspendable path. New tests cover incremental errors and dump/restore for every constructor.

  • Adds a test-hooks-gated list._sync() so tests can keep exercising synchronous collection now that list() suspends; CI enables the test-hooks feature, and the pyattrs macro accepts #[cfg]-gated attributes.
  • <sup>Written for commit a06516dadd5439e728578de1f9ce3e2885246ed6. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1479?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: 393 lines (+294 / -99) across 4 files Reviewer: jaheba Review duration: 1285m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.43) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 393 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.358
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1479"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 43.1% (MEDIUM) · Higher = larger blast radius
    43.1%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 23.0% · Safe to merge
    23.0%
    Repository