Skip to content

Python: accept nanosecond timestamps in generated parse_rfc3339 - #298

Open
sebxt wants to merge 1 commit into
1Password:mainfrom
sebxt:fix/python-datetime-nanosecond-parse
Open

sebxt wants to merge 1 commit into
1Password:mainfrom
sebxt:fix/python-datetime-nanosecond-parse

Conversation

@sebxt

@sebxt sebxt commented Oct 19, 2026

Copy link
Copy Markdown

The generated parse_rfc3339 validator passes the string straight to datetime.strptime(..., "%Y-%m-%dT%H:%M:%S.%fZ"). %f only matches up to 6 digits, while the time crate's RFC 3339 serializer emits up to 9, so a value like 2024-05-01T12:34:56.123456789Z coming from a Rust service fails Pydantic validation.

This change truncates the fractional part to 6 digits before calling strptime. datetime only has microsecond precision, so nothing representable is lost. Strings without a fraction, or with 6 digits or fewer, are unchanged.

Changes:

  • core/src/language/python.rs: trim the fraction in the emitted parse_rfc3339.
  • Updated the test_serde_iso8601 and test_custom_serialize_deserialize_functions Python snapshots.

Testing:

  • cargo test -p typeshare-core --test snapshot_tests passes (the two Python snapshots fail without the generator change).
  • Loaded the generated test_serde_iso8601/output.py with pydantic 2 and validated {"time": "2024-05-01T12:34:56.123456789Z"}: before, ValidationError; after, 2024-05-01 12:34:56.123456. The 3 digit and no fraction cases behave as before.

Fixes #297

Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>

This branch has not been deployed

No deployments
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.

Python: generated parse_rfc3339 rejects timestamps with more than 6 fractional digits

1 participant