← Back to Pull requests

Class attribute tables #989

Closed
unknown closed this pull request without merging on Oct 20, 2026 15:36 UTC
View on GitHub
unknown unknown commented on Oct 07, 2026 13:16 UTC
Author

Depends on #1050; this PR targets dh/either-str and builds on its PyStr view.

Define Python-visible builtin attributes alongside their Rust implementations using #[pyattrs].
Receiver names the actual native view passed to a method.
String methods are defined directly on PyStr, covering both interned and heap strings:

use monty_macros::pyattrs;

#[pyattrs]
impl<'h> PyAttrs<'h> for PyStr<'h> {
    type Receiver = PyStr<'h>;

    #[pymethod]
    fn upper(text: &PyStr<'h>, args: ArgValues, vm: &mut VM<'h>) -> RunResult<Value> {
        args.check_zero_args("str.upper", vm.heap)?;
        Ok(allocate_string(uppercase(text.as_str(vm)), vm.heap))
    }
}

Attribute definitions and receivers

The macro emits an inherent implementation containing the functions, a PyAttrs implementation with
const ATTRS: &'static [(StaticStrings, AttrDef)], and a const lookup_attr match returning entries from that table.
Rust names determine attribute names: upper maps to StaticStrings::Upper, and total_seconds to TotalSeconds.

  • #[pymethod] registers a method and generates its adapter from &Value to the native receiver.

  • #[pyclassmethod] registers a class-method callable.

  • #[pyvalue] registers a value factory.
  • Receiver can be a HeapRead<'h, T>, HeapObjectRead<'h, T>, PyStr<'h>, or an immediate f64, i64, or bool.
    Heap methods select & or &mut in their explicit first parameter; string methods take &PyStr.
    Adapters remain callable for any VM lifetime, independently of the lifetime used to select the attribute table.
    Other builtin methods use zero-sized namespaces such as PyListImpl, which are never instantiated or stored in the heap.

    Dispatch and scope

    The centralized attrs(owner: Type) selects each type's trait constant.
    py_call_attr and class-method dispatch use static lookups, with enumerable, const-compatible tables.
    There is no runtime registry or dynamic discovery.

    This covers 102 attributes across str, list, tuple, complex, date, time, timedelta, timezone, regex patterns, and regex matches.
    All 44 existing string methods use the generated table.
    Single-use string implementations are inlined into their registered methods; shared startswith/endswith validation remains
    an unregistered helper on PyStr.

    Existing argument handling, error propagation, heap borrowing, and reference management are retained.
    List sorting keeps its reentrancy handling and modification detection.
    Native object representations, bytecode, serialization, and the dump version are unchanged by this PR.
    Standalone callable attributes are separate follow-up work.

    Validation

  • 944 Rust tests pass with memory-model checks; two doctests are ignored.

  • Tests cover table contents, lookup fallbacks, native receiver adapters, argument cleanup, and value errors.

  • String tests cover interned and computed receivers, Unicode, joins, formatting, and rejected arguments.

  • Macro tests cover lifetime-aware implementations, shared/mutable heap adapters, invalid receivers and signatures,

  • and duplicate trait implementations.
  • All 15 Monty string fixtures pass. Three unchanged CPython counterparts fail in assertions on this host.

  • Clippy passes with warnings denied; formatting and import checks pass.
  • Diff: 410 lines (+307 / -103) across 5 files Reviewer: jaheba Review duration: 18860m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 410 lines diff across 5 files
    0.45
    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.360
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #989"
    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: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    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: 17.7% · Safe to merge
    17.7%
    Repository