Skip to content

Add custom Duration and Timestamp classes with nanosecond support #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

hectorcast-db
Copy link
Contributor

@hectorcast-db hectorcast-db commented May 21, 2025

What changes are proposed in this pull request?

Add custom Duration and Timestamp classes with nanosecond support

How is this tested?

Added tests

NO_CHANGELOG=true

Comment on lines +28 to +42
def __init__(self, seconds: int = 0, nanoseconds: int = 0) -> None:
"""Initialize a Duration with seconds and nanoseconds.

Args:
seconds: Number of seconds
nanoseconds: Number of nanoseconds (0-999999999)

Raises:
TypeError: If seconds or nanoseconds are not integers
ValueError: If nanoseconds is not between 0 and 999999999
"""
if not isinstance(seconds, int):
raise TypeError("seconds must be an integer")
if not isinstance(nanoseconds, int):
raise TypeError("nanoseconds must be an integer")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this type check when we explicitly stated that in the function arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type in the arguments are only "hints" for the linter, but python does not enforce them.

self.nanoseconds = nanoseconds

@classmethod
def from_timedelta(cls, td: timedelta) -> "Duration":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is returning "Duration" with quotes. Is this expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. In Python you cannot use a type before introduced. This is a workaround introduced at some point:

https://peps.python.org/pep-0484/#forward-references

Copy link

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/sdk-py

Inputs:

  • PR number: 975
  • Commit SHA: 5d4c93f366fbfe57daaf85dfdab441cea76bbdb2

Checks will be approved automatically on success.

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.

2 participants