-
Notifications
You must be signed in to change notification settings - Fork 163
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
Address time zone localization issue #368
Address time zone localization issue #368
Conversation
|
||
|
||
def query_time_with_timezone(trino_connection, tz_str: str): | ||
@pytest.mark.parametrize( |
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.
Same logic as before but more aligned with how pytest
was intended to be used.
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.
Please make preparatory commits for optimisations not related to the change.
from zoneinfo import ZoneInfo # type: ignore | ||
|
||
except ModuleNotFoundError: | ||
from backports.zoneinfo import ZoneInfo # type: ignore |
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.
Previously there was no requirement in setup.py
for the backports.zoneinfo
package.
@@ -300,114 +306,114 @@ def query_time_with_timezone(trino_connection, tz_str: str): | |||
# min supported time(3) | |||
.add_field( | |||
sql=f"TIME '00:00:00 {tz_str}'", | |||
python=time(0, 0, 0).replace(tzinfo=tz)) | |||
python=time(0, 0, 0, tzinfo=tz)) |
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.
Clearer/cleaner to specify the time zone when creating the object.
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.
are there any other occurrences of this in production code as well?
d120739
to
013853c
Compare
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.
LGTM
|
||
|
||
def query_time_with_timezone(trino_connection, tz_str: str): | ||
@pytest.mark.parametrize( |
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.
Please make preparatory commits for optimisations not related to the change.
013853c
to
e184409
Compare
@mdesmet I've addressed your comments. |
@aalbu or @hashhar would either of you mind merging this given than @mdesmet has approved the change? Additionally what would be a rough estimate (days, weeks, or months) before this change would be released? The reason I'm asking is we likely need to give our Superset users—which leverages the Trino DB-API—a heads up of the issue and would ideally like to provide them with an rough ETA as to when it will be fixed. |
I'll take a look tomorrow. I'm leaving some quick editorial comments for now. Since this is a correctness issue I'd like to do a release as soon as we have this merged. |
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.
Commit "[preparatory] Use @pytest.mark.parameterize".
The project avoids prefixing commit messages, so something just like Parameterize time and timestamp with time zone tests
is good enough.
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.
commit "[preparatory] Specify tzinfo during datetime/time object creation" -> "Specify tzinfo during datetime/time creation in tests"
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.
commit " [fix] Address timezone localization issue ".
Can you add a short explanation in the commit message body about the issue? Alternatively I think it'd also be self-explanatory if you can add a test which showcases the issue and then in next commit add the fix and adjust the test.
@@ -78,7 +78,12 @@ | |||
"Topic :: Database :: Front-Ends", | |||
], | |||
python_requires='>=3.7', | |||
install_requires=["pytz", "requests", "tzlocal"], | |||
install_requires=[ | |||
"backports.zoneinfo;python_version<'3.9'", |
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.
missing zoneinfo require should be a separate commit, it's not related to the fix itself.
@@ -1041,7 +1037,7 @@ def new_instance(self, value: datetime, fraction: Decimal) -> TimestampWithTimeZ | |||
return TimestampWithTimeZone(value, fraction) | |||
|
|||
def normalize(self, value: datetime) -> datetime: | |||
if isinstance(self._whole_python_temporal_value.tzinfo, BaseTzInfo): | |||
if tz.datetime_ambiguous(value): |
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.
nice.
e184409
to
cbd4c6b
Compare
@hashhar I've addressed your comments. |
Consider the following example using the canonical way to add zones to datetime objects: >>> import pytz >>> import datetime >>> import zoneinfo >>> datetime.datetime(2023, 1, 1, tzinfo=pytz.timezone("America/Los_Angeles")).isoformat() '2023-01-01T00:00:00-07:53' >>> datetime.datetime(2023, 1, 1, tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles")).isoformat() '2023-01-01T00:00:00-08:00' pytz does eager timezone evaluation and uses the local-mean-time since the instant in time is not known. It requires an additional `localize` call to get the correct zone like so: >>> pytz.timezone("America/Los_Angeles").localize(datetime.datetime(2023, 1, 1)).isoformat() '2023-01-01T00:00:00-08:00' This increases chances of introducing bugs when writing idiomatic Python. The only reason to use pytz was because it allowed to control what happens with ambiguous datetimes but the standard library also allows provides control over that since 3.9 (and is available as backports.zoneinfo for older versions).
cbd4c6b
to
7ff12c7
Compare
Description
This PR addresses #366.
Per this article, there seemed to be merit in removing
pytz
in favor ofdateutil.tz
given that in Python 3.6 thedatetime
module remedied the ambiguous datetimes due to daylight saving time transition.Regrettably though one cannot obtain the IANA name from a
dateutil.tz
time zone—which is required when transpilingdatetime.time
/datetime.datetime
objects to Trino SQL when provided as operation parameters. Instead we use thezoneinfo
package (added to Python’s standard library in Python 3.9 and back ported) which does provide a mechanism of obtaining the specified IANA name from adatetime.datetime
/datetime.time
object, i.e.,The fix is rather simple as simply switching out the
pytz
package for thezoneinfo
package remedies the issue:Before
After
Non-technical explanation
Release notes
( ) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
(x) Release notes are required, with the following suggested text: