Skip to content

feat: implement datetime.time (#331)#334

Draft
nurikk wants to merge 2 commits intopydantic:mainfrom
nurikk:feature/issue-331-datetime-time
Draft

feat: implement datetime.time (#331)#334
nurikk wants to merge 2 commits intopydantic:mainfrom
nurikk:feature/issue-331-datetime-time

Conversation

@nurikk
Copy link
Copy Markdown

@nurikk nurikk commented Apr 14, 2026

Summary

  • Fixes datetime.time is not implemented #331: from datetime import time no longer raises ImportError.
  • Adds Type::Time, crates/monty/src/types/time.rs, StaticStrings::Time / Fold, and wires time into the datetime module.
  • Phase-1 surface mirrors the existing datetime type's scope.

Design choices

  • tzinfo: accepts None or a TimeZone instance, matching the existing datetime type. Stores the HeapId so t.tzinfo is input_tz holds. A full tzinfo ABC is out of scope (consistent with datetime).
  • fold: keyword-only, validated to 0..=1, exposed on .fold, included in repr when non-zero (matches CPython 3.14). Monty does not interpret fold (no DST model) — round-trip only.
  • Out of scope (phase-1): replace(), strftime(), fromisoformat(), utcoffset(), tzname(), dst(), __format__, min/max/resolution, and MontyTime in the public MontyObject serde enum (falls back to repr at the PyO3/JS boundary).

Test plan

  • New crates/monty/test_cases/time__core.py covers construction, attribute access, isoformat(), repr/str, equality/ordering, hashability, truthiness, aware tzinfo semantics, fold, and CPython-compatible error messages — passes on Monty and CPython.
  • Full datatest suite: 930 passed.
  • cargo test -p monty --features ref-count-panic: 596 passed, 19 ignored.
  • cargo clippy --workspace --tests --all-features -- -D warnings clean.
  • cargo fmt --check, ruff check/ruff format --check clean.

nurikk added 2 commits April 14, 2026 17:46
Adds the `datetime.time` class so `from datetime import time` works and
matches CPython's phase-1 surface:

- `Type::Time` variant + display as `datetime.time`
- `types/time.rs` with narrow-integer storage, constructor validation
  (`hour`, `minute`, `second`, `microsecond`, `fold`), attribute access,
  `isoformat()`, CPython-compatible repr/str, hash, `__eq__`/ordering
- `StaticStrings::Time` (and `StaticStrings::Fold`)
- Module wiring in `modules/datetime.rs`
- Consolidated test file `crates/monty/test_cases/time__core.py`

tzinfo design choice: accept `None` or a `TimeZone` instance (same scope
as the existing `datetime.datetime` implementation), preserving the input
object identity so `t.tzinfo is input_tz` holds across attribute access.
A full `tzinfo` ABC is out of scope for phase 1 — matches `datetime.rs`.

fold is accepted as a keyword argument (0 or 1) and round-tripped through
the value so it appears in repr when non-zero and is readable via
`.fold`. CPython methods not in phase-1 scope (`replace`, `strftime`,
`fromisoformat`, `utcoffset`, `tzname`, `dst`, `__format__`) are not yet
implemented — they can be added in a follow-up.

On the public-API side, `HeapData::Time` falls back to a repr placeholder
in `MontyObject` for now; a dedicated `MontyTime` variant can follow once
host bindings need round-tripping.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 14, 2026

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 15 skipped benchmarks1


Comparing nurikk:feature/issue-331-datetime-time (cc77fc0) with main (dc44b92)

Open in CodSpeed

Footnotes

  1. 15 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 73.46369% with 95 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/monty/src/types/time.rs 73.37% 67 Missing and 23 partials ⚠️
crates/monty/src/heap_data.rs 66.66% 1 Missing and 2 partials ⚠️
crates/monty/src/object.rs 0.00% 1 Missing ⚠️
crates/monty/src/types/type.rs 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime.time is not implemented

1 participant