Skip to content
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

Disallow datetime ranges with mixed tzinfo #194

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Peter554
Copy link
Contributor

@Peter554 Peter554 commented Jan 29, 2025

Context: #192


Update: I've tried this in Kraken but the number of errors was rather daunting. I think it's unlikely we find time to prioritise this change right now, so I'm proposing #195 as an alternative/first-step to unblock things.

@Peter554 Peter554 self-assigned this Jan 29, 2025
@Peter554 Peter554 force-pushed the disallow-datetime-ranges-with-mixed-tzinfo branch from b14a4b5 to 21ef3e2 Compare January 29, 2025 08:22
@Peter554 Peter554 force-pushed the disallow-datetime-ranges-with-mixed-tzinfo branch from 21ef3e2 to 2b982f0 Compare January 29, 2025 08:51
Comment on lines -1247 to -1252
dt_range = ranges.FiniteDatetimeRange(
datetime.datetime(2020, 1, 1, tzinfo=zoneinfo.ZoneInfo("Asia/Dubai")),
datetime.datetime(
2020, 1, 10, tzinfo=zoneinfo.ZoneInfo("Australia/Sydney")
),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing this test case since it's no longer possible to construct such a range!

@@ -867,12 +867,21 @@ def intersection(
# We're deliberately overriding the base class here for better performance.
# We can simplify the implementation since we know we're dealing with finite
# ranges with INCLUSIVE_EXCLUSIVE bounds.
if self.tzinfo != other.tzinfo:
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 if isinstance(other, FiniteDatetimeRange): branch gets its own logic to aid performance. other.tzinfo is simply looking up a field, where as calling get_tzinfo(other) has to execute that entire function.

@Peter554 Peter554 force-pushed the disallow-datetime-ranges-with-mixed-tzinfo branch from 2b982f0 to 1fab13d Compare January 29, 2025 09:04
@Peter554 Peter554 force-pushed the disallow-datetime-ranges-with-mixed-tzinfo branch from 1fab13d to 949ead8 Compare January 29, 2025 09:05
@@ -1019,6 +1019,15 @@ def test_finite_range(self):


class TestFiniteDatetimeRange:
def test_cannot_construct_with_inconsistent_tzinfo(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to have similar logic for DatetimeRange and HalfFiniteDatetimeRange, but those two are currently not independent classes but just type aliases. So there is nowhere to insert the logic 🤔 Is there any reason not to make DatetimeRange and HalfFiniteDatetimeRange actual classes (if we decide to do this I might save that for a separate PR, or do it as a preparation PR)?

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