← Back to Pull requests

Add date.toordinal(), date.fromordinal(), datetime.toordinal(), and datetime.fromordinal() #1271

Open
unknown opened this pull request with 363 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 07, 2026 19:45 UTC
Author

Implement proleptic Gregorian day ordinal methods on date and datetime matching CPython 3.14 semantics.

Changes


  • Add Fromordinal and Toordinal variants to StaticStrings.

  • Implement ord_to_year in date.rs to compute Gregorian years for ordinals above 3,652,059, matching CPython's ValueError: year must be in 1..9999, not {year} error format.

  • Implement date_from_ordinal and class_fromordinal on Date, parsing arguments via FromOrdinalArgs with style = parse_tuple to reject keywords (TypeError: fromordinal() takes no keyword arguments) and check arity (TypeError: fromordinal() takes exactly 1 argument (N given)).

  • Expose date.toordinal() and date.fromordinal(), accessible on both class and instance.

  • Implement datetime.toordinal() and datetime.fromordinal(), returning a naive datetime at midnight (00:00:00).

  • Wire class method dispatch in Type::call_attr for Date and DateTime.

  • Update docs/limitations/datetime.md to reflect toordinal and fromordinal() support.

  • Add test coverage in crates/monty/test_cases/datetime__core.py covering boundary ordinals (Day 1 0001-01-01, Day 3,652,059 9999-12-31), leap years, epoch dates, instance invocations, __index__ coercion, ValueError message parity, integer overflow checks (i32 bounds and BigInt), arity checks, and kwarg rejection.
  • Validation


  • cargo run -p monty-datatest -j 2 -- datetime__ (14/14 tests passed on both Monty and CPython).

  • cargo test -p monty -j 2 --test datetime_format (9/9 tests passed).

  • cargo clippy -p monty -j 2 --all-targets -- -D warnings (passed with zero warnings).

  • cargo +nightly fmt --all (passed).
  • <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Adds toordinal() and fromordinal() to date and datetime, matching CPython 3.14 semantics, now built on Jiff primitives after the chrono migration.

  • fromordinal(ordinal) constructs a date or a naive datetime at midnight; toordinal() returns the day count since 0001-01-01.

  • Error messages for out-of-range ordinals, kwarg rejection, wrong arity, and __index__ coercion match CPython.

  • Updates the datetime limitations doc and adds boundary, leap-year, and overflow test coverage.
  • <sup>Written for commit 7a37259904c479bab795cc7484d3570f0a4ac3c7. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/1271?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: 363 lines (+272 / -91) across 4 files Reviewer: davidhewitt Review duration: 21643m
    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 · 363 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.353
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1271"
    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: 42.7% (MEDIUM) · Higher = larger blast radius
    42.7%
    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: 22.7% · Safe to merge
    22.7%
    Repository