-
Notifications
You must be signed in to change notification settings - Fork 36
feat: Add ability to use datetime objects or time-shift strings in TimestampRange #2432
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
base: master
Are you sure you want to change the base?
Conversation
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2432 +/- ##
==========================================
+ Coverage 91.27% 91.29% +0.02%
==========================================
Files 192 192
Lines 26190 26192 +2
==========================================
+ Hits 23905 23913 +8
+ Misses 2285 2279 -6
🚀 New features to boost your workflow:
|
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a useful enhancement by allowing datetime objects to be used in TimestampRange. The implementation is sound and includes corresponding tests. My review focuses on improving code style and consistency by addressing import ordering and ensuring the consistent use of timezone-aware datetime objects in tests, in line with the repository's style guide.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a convenient feature by allowing datetime objects to be used in TimestampRange. The implementation is a good start, but it contains a critical issue related to handling timezone-naive datetime objects, which could lead to incorrect timestamps and subtle bugs. I have provided a comment with a suggested fix to enforce the use of timezone-aware datetime objects, ensuring data correctness.
| self.max = int(max.timestamp() * 1000) if isinstance(max, datetime) else max | ||
| self.min = int(min.timestamp() * 1000) if isinstance(min, datetime) else min |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for converting datetime objects to timestamps is vulnerable to timezone-related bugs. The datetime.timestamp() method uses the system's local timezone for naive datetime objects (those without tzinfo). This can result in incorrect millisecond timestamps if the code is run on a machine not set to UTC, as the Cognite API expects UTC timestamps.
To prevent incorrect data from being sent to the API, you should enforce that any provided datetime object is timezone-aware. Raising a ValueError for naive datetime objects is the safest approach.
I also recommend updating the docstrings for min and max to clarify that datetime objects must be timezone-aware.
| self.max = int(max.timestamp() * 1000) if isinstance(max, datetime) else max | |
| self.min = int(min.timestamp() * 1000) if isinstance(min, datetime) else min | |
| if isinstance(max, datetime): | |
| if max.tzinfo is None: | |
| raise ValueError("datetime object for 'max' must be timezone-aware.") | |
| self.max = int(max.timestamp() * 1000) | |
| else: | |
| self.max = max | |
| if isinstance(min, datetime): | |
| if min.tzinfo is None: | |
| raise ValueError("datetime object for 'min' must be timezone-aware.") | |
| self.min = int(min.timestamp() * 1000) | |
| else: | |
| self.min = min |
References
- The style guide emphasizes specific exceptions and graceful error handling. Passing a naive datetime can lead to incorrect behavior, so raising a
ValueErrorfor this invalid input is appropriate to ensure data integrity. (link)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cognitedata/python-sdk-maintainers what do you think? Personally I think the SDK should be consistent, either enforcing timezone awareness or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MortGron you can check in utils/_time.py for existing conversion functions. I vote to be strict on timezone, or at least throw a warning when naive. This PR won't be accepted until v8 is out btw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion about the utils module! What do you think about letting the utils functions in the _time.py handle timezone enforcement or warnings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Knock yourself out 👌
Description
Let
TimestampRangeobjects acceptdatetimeobjects as arguments. The motivation fot this is that it is a very common use pattern that users convertdatetimeobjects to timestamps before makingTimestampRangeobjects.Checklist:
If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.