← Back to Pull requests

Vendor CPython's textwrap #1301

Closed
unknown closed this pull request without merging on Nov 26, 2026 09:44 UTC
View on GitHub
unknown unknown commented on Nov 10, 2026 13:46 UTC
Author

Stacked on #1298: that PR's branch lives in a fork, so the base here is \main\ and the diff also shows its commits. Only the last two commits are new:

  • \Add str.translate, str.maketrans and re.VERBOSE\

  • \Vendor CPython's textwrap\
  • What

    \import textwrap\ runs CPython 3.14's own \Lib/textwrap.py\.

  • \str.translate\ / \str.maketrans\ (new \types/str_translate.rs\), matching CPython's behaviour and error messages. The table is looked up with generic subscription, and the result is built with \StringBuilder\.

  • \re.VERBOSE\ / \re.X\ exposed, and included in the pattern repr.

  • Patch \0003-textwrap-no-del\: Monty does not parse \del\ yet (#1032), and cannot read a builtin method without calling it (\patsearch = pattern.search\).

  • Registered in the type checker, JS sandbox module list and typeshed; docs and \limitations/\ updated.
  • Divergences (documented)

  • \TextWrapper.word_punct\, \letter\, \nowhitespace\ and \whitespace\ stay as class attributes.

  • A \translate\ mapping to a surrogate raises \ValueError\, since Monty strings cannot hold lone surrogates.

  • \bytes.translate\ / \bytes.maketrans\ are not implemented.
  • Tests

    New \str__translate\ and \textwrap__module\ cases and \re.VERBOSE\ asserts in \re__basic\, run on Monty and CPython. \cargo nextest -p monty -p monty-type-checking -p monty-types\, \make lint-rs\, \make check-stdlib\, \make check-typeshed\ and \branch_diff.py --check\ pass.

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

    Summary by cubic


    Vendors CPython's own pure-Python textwrap.py and its upstream test suite, so import textwrap works without a Rust port, and adds the str.translate/str.maketrans methods and re.VERBOSE/re.X that they need. The module is embedded in the binary and runs like a file module: compiled per session, cached by name, with no OS call.

    Infrastructure

  • crates/monty/stdlib/vendor.toml pins CPython v3.14.10; make vendor-stdlib downloads the files and LICENSE, applies patches/*.patch, and generates the embedded source table.

  • make check-stdlib runs in CI and fails if vendored files drift from the pinned release.

  • make stdlib-candidates reports which stdlib modules the current binary can load and the first blocker of each.

  • The 68 upstream test_textwrap.py tests run on Monty and CPython; the shared unittest patch drops with_traceback(), which Monty exceptions lack, and each class lists its test methods since Monty has no dir().

  • The vendored CPython test patches are skipped in codespell, since their context lines quote upstream test code.
  • Divergences

  • One patch works around del not parsing (#1032) — the class-body del is dropped and del chunks[-1] becomes .pop() — and bound builtin methods not being readable without a call; dedent's error names the type via __name__ since Monty types have no __qualname__.

  • str.translate builds its output through a single memory-checked path, so both mapped values and unmapped misses stay within the resource limits, and validates its arguments before copying the receiver, so an invalid call on a string too large to copy raises TypeError rather than MemoryError.

  • A translate mapping to a surrogate code point raises ValueError, since Monty strings cannot hold lone surrogates; bytes.translate/bytes.maketrans are not implemented.
  • <sup>Written for commit 9889c77a05b515d40349225caecb3a3acda3a4ad. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1301?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: 507 lines (+380 / -127) across 6 files Reviewer: rewitt94 Review duration: 22798m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 507 lines diff across 6 files
    0.48
    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.373
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1301"
    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.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    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.8% · Safe to merge
    18.8%
    Repository