Skip to content

Preserve nanosecond precision for Timestamp#213

Open
ClausHolbechArista wants to merge 1 commit into
betterproto:mainfrom
ClausHolbechArista:add-nanoseconds
Open

Preserve nanosecond precision for Timestamp#213
ClausHolbechArista wants to merge 1 commit into
betterproto:mainfrom
ClausHolbechArista:add-nanoseconds

Conversation

@ClausHolbechArista
Copy link
Copy Markdown

@ClausHolbechArista ClausHolbechArista commented May 20, 2026

Summary

  • Add NanoDatetime to preserve sub-microsecond precision for google.protobuf.Timestamp
  • Route Timestamp conversion, JSON formatting, and RFC3339 parsing through NanoDatetime
  • Keep generated Timestamp wrapper types as datetime.datetime
  • Generate the NanoDatetime import only for known types that need it
  • Typehints are still datetime.datetime to keep this non-breaking.
    • The NanoDatetime subclass still satisfies typecheckers.
    • It could be considered to add some option to set the proper type hint later.

Testing

  • uv run pytest tests/test_nano_datetime.py tests/test_timestamp.py -q
  • uv run pytest tests/test_known_type_imports.py -q
  • uv run ruff check ...
  • uv run pyright ...
  • uv run poe generate
  • uv run poe get-local-compiled-tests

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
    • This change has an associated test.
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

Add NanoDatetime to carry sub-microsecond precision through
Timestamp conversions and JSON parsing/formatting while keeping
generated field types as datetime.datetime.

Generate the NanoDatetime import only for known types that need it,
and add runtime/compiler coverage for the new behavior.
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.

1 participant