Skip to content

Improve set_time error handling for large Python integers #9839

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

Merged
merged 2 commits into from
Apr 30, 2025

Conversation

oxkitsune
Copy link
Member

Related

What

Adds a clear error message when set_time receives an out-of-range timestamp.

@oxkitsune oxkitsune added 🐍 Python API Python logging API 😤 annoying Something in the UI / SDK is annoying to use exclude from changelog PRs with this won't show up in CHANGELOG.md 🪵 Log & send APIs Affects the user-facing API for all languages labels Apr 29, 2025
@abey79 abey79 added this to the 0.23.2 milestone Apr 29, 2025
f"set_time: `timestamp={timestamp!r}` is out of range; "
f"expected seconds since Unix epoch, datetime.datetime, or numpy.datetime64 "
f"(timeline='{timeline}')"
) from err
Copy link
Member

Choose a reason for hiding this comment

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

How large integers were you passing in, and why?

And shouldn't we protect against the same problem for sequence and duration timelines too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was passing a timestamp in milliseconds since unix epoch, e.g. '1_745_943_196_000'. But since we assume seconds, it would then be multiplied by 1e9 to convert to nanoseconds.

I don't think sequence and duration suffer from the same gotcha, so I'd argue it's not necessary.

Copy link
Member

@emilk emilk Apr 29, 2025

Choose a reason for hiding this comment

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

Sounds like we could improve the error message then with something along the lines of:

if 1_500_000_000_000 < timestamp < 2_000_000_000_000:
    raise "Expected seconds since unix epoch, but it looks like this is milliseconds"

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea I like this more as well. However 2_000_000_000_000 is only valid for about 8 years.

Let's check if timestamp is bigger than 1e11 which will always mean milliseconds instead of seconds 👍

)
except OverflowError as err:
raise ValueError(
f"set_time: `timestamp={timestamp!r}` is out of range; "
Copy link
Member

Choose a reason for hiding this comment

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

What is the !r?

Copy link
Member Author

Choose a reason for hiding this comment

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

It formats the value using repr(), instead of str().

import numpy as np

a = np.int64(123)
b = 123

print(f"{a!r} {a}")
print(f"{b!r} {b}")

would result in:

np.int64(123) 123
123 123

Figured that this would be desired to clearly show which type is being used, since we can take in numpy integers, python integers and floats iirc.

Copy link
Member

Choose a reason for hiding this comment

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

TIL!

@emilk emilk merged commit 6ae6799 into main Apr 30, 2025
36 checks passed
@emilk emilk deleted the gijs/set-time-warning branch April 30, 2025 07:31
abey79 pushed a commit that referenced this pull request May 1, 2025
### Related

* Closes #9822 

### What

Adds a clear error message when `set_time` receives an out-of-range
timestamp.
@abey79 abey79 added include in changelog and removed exclude from changelog PRs with this won't show up in CHANGELOG.md labels May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
😤 annoying Something in the UI / SDK is annoying to use include in changelog 🪵 Log & send APIs Affects the user-facing API for all languages 🐍 Python API Python logging API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

set_time Cannot handle large Python integers
3 participants