Pydantic v2 annotated types for python-dateutil
Project description
py-dateutil-pydantic
Pydantic v2 annotated types for python-dateutil. Validate dateutil objects from JSON-friendly inputs (strings, dicts) and serialize them back — with zero-cost pass-through when you already have a Python object.
Installation
pip install py-dateutil-pydantic
Quick Start
from pydantic import BaseModel
from py_dateutil_pydantic import (
DateutilDatetime,
DateutilTz,
RelativeDelta,
RRule,
Weekday,
)
class Schedule(BaseModel):
start: DateutilDatetime
recurrence: RRule
offset: RelativeDelta
day: Weekday
timezone: DateutilTz
# From JSON-friendly inputs
schedule = Schedule(
start="January 1, 2024 9:00 AM",
recurrence="DTSTART:20240101T090000\nRRULE:FREQ=WEEKLY;COUNT=4",
offset={"hours": 1, "minutes": 30},
day="MO",
timezone="America/New_York",
)
# Serialize back to JSON
print(schedule.model_dump(mode="json"))
# {
# "start": "2024-01-01T09:00:00",
# "recurrence": "DTSTART:...\nRRULE:FREQ=WEEKLY;COUNT=4",
# "offset": {"hours": 1, "minutes": 30},
# "day": "MO",
# "timezone": "America/New_York",
# }
Existing Python objects pass through without parsing overhead:
from datetime import datetime
from dateutil.relativedelta import relativedelta, weekday
from dateutil.rrule import rrule, WEEKLY
from dateutil import tz
schedule = Schedule(
start=datetime(2024, 1, 1, 9),
recurrence=rrule(WEEKLY, count=4, dtstart=datetime(2024, 1, 1, 9)),
offset=relativedelta(hours=1, minutes=30),
day=weekday(0),
timezone=tz.gettz("America/New_York"),
)
Types
Weekday
Wraps dateutil.relativedelta.weekday.
| Input | Example |
|---|---|
| String | "MO", "FR(+2)", "SA(-1)" |
| Integer | 0 (Monday) through 6 (Sunday) |
Serializes to string: "MO", "FR(+2)".
RelativeDelta
Wraps dateutil.relativedelta.relativedelta.
Accepts a dict with any combination of:
- Relative keys:
years,months,days,weeks,hours,minutes,seconds,microseconds - Absolute keys:
year,month,day,hour,minute,second,microsecond - Weekday:
weekday(string like"MO"or"FR(+2)")
Serializes to a dict of non-default attributes.
RRule
Wraps dateutil.rrule.rrule.
| Input | Example |
|---|---|
| RFC 5545 string | "DTSTART:20240101T000000\nRRULE:FREQ=WEEKLY;COUNT=3" |
Serializes to RFC 5545 string.
RRuleSet
Wraps dateutil.rrule.rruleset.
| Input | Example |
|---|---|
| RFC 5545 string | Multi-line string with RRULE, RDATE, EXRULE, EXDATE lines |
Serializes to RFC 5545 string.
DateutilTz
Wraps datetime.tzinfo (via dateutil.tz).
| Input | Example |
|---|---|
| IANA name | "America/New_York", "Europe/London" |
| UTC | "UTC" |
| Offset | "+05:30", "-05:00" |
| Local | "local" |
Serializes with type dispatch: tzutc -> "UTC", tzoffset -> "+HH:MM", tzlocal -> "local", tzfile -> IANA name.
TzUTC, TzOffset, TzLocal
Constrained variants of DateutilTz that only accept their specific timezone type.
TzUTC— only"UTC"TzOffset— offset string"+05:30"or dict{"name": "EST", "offset": -18000}TzLocal— only"local"
DateutilDatetime
Wraps datetime.datetime. Accepts any string that dateutil.parser.parse can handle:
"January 1, 2024"
"2024-01-01"
"01/15/2024 3:30 PM"
Serializes to ISO 8601 via .isoformat().
ISODatetime
Wraps datetime.datetime. Accepts only strict ISO 8601 strings (via dateutil.parser.isoparse):
"2024-01-01T12:00:00"
"2024-06-15T10:30:00+05:30"
Serializes to ISO 8601 via .isoformat().
JSON Schema
All types generate JSON schemas, so they work with OpenAPI and other tools that consume Pydantic model schemas:
print(Schedule.model_json_schema())
Development
# Install dependencies
uv sync
# Run tests
uv run pytest tests/ -v
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_dateutil_pydantic-0.1.0.tar.gz.
File metadata
- Download URL: py_dateutil_pydantic-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
929c8a5a5b5d91cab8b6f230fe39b01b601a2d8cb1818cd0c4e0294f5692f84d
|
|
| MD5 |
1cbd9062226c8df493dd347861e3fef6
|
|
| BLAKE2b-256 |
bf2e20fd2e9d103c50e10aac77e060b7383374849f99666b5974956596714433
|
File details
Details for the file py_dateutil_pydantic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_dateutil_pydantic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
597ce7b20883363b68824030765041f4f464ee66c24afca47ed4e844655ee7c4
|
|
| MD5 |
f951ae99bda03397dc63bee12e984870
|
|
| BLAKE2b-256 |
2bad3bc77d2cc348cb131b301cd981878f36457ea045314d325e042b56d62e38
|