refactor: replace usage of pytz with zoneinfo#475
Open
asmfstatoil wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors Tagreader’s timezone handling to remove the pytz dependency and use Python’s built-in zoneinfo instead, updating runtime code, tests, and documentation accordingly.
Changes:
- Replaced
pytztimezone usage withzoneinfo.ZoneInfoanddatetime.timezone.utcin core code paths. - Updated tests to construct/compare timezone-aware datetimes using
ZoneInfo, and refreshed docs/examples to use IANA zone names (e.g.,America/Sao_Paulo). - Minor cleanup (tuple unpacking style, timedelta slicing simplification).
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
tagreader/utils.py |
Switches default timezone handling to ZoneInfo and updates naive-datetime tz attachment logic. |
tagreader/clients.py |
Updates client timezone parsing/validation to use ZoneInfo and removes pytz usage. |
tagreader/web_handlers.py |
Replaces pytz.UTC conversions with datetime.timezone.utc. |
tagreader/cache.py |
Replaces pytz.utc conversion with datetime.timezone.utc. |
tests/test_utils.py |
Updates datetime-with-timezone assertions to use ZoneInfo. |
tests/test_clients.py |
Updates timezone expectations to ZoneInfo. |
tests/test_PIHandlerREST.py |
Minor tuple unpacking cleanup in tests. |
tests/test_bucketcache.py |
Simplifies timestamp slicing using TS directly. |
tests/test_AspenHandlerREST_connect.py |
Adjusts test time constants. |
documentation/docs/about/usage/time-zone.md |
Updates timezone guidance from pytz to zoneinfo/tzdata. |
documentation/docs/about/usage/basic-usage.md |
Updates example timezone string to America/Sao_Paulo. |
docs/manual.md |
Updates manual to reflect zoneinfo usage and updated examples. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
51
to
55
| def ensure_datetime_with_tz( | ||
| date_stamp: Union[datetime, str, pd.Timestamp], | ||
| tz: tzinfo = pytz.timezone("Europe/Oslo"), | ||
| tz: tzinfo = ZoneInfo("Europe/Oslo"), | ||
| ) -> datetime: | ||
| date_stamp = convert_to_pydatetime(date_stamp) |
| datasource: str, | ||
| imstype: Optional[Union[str, IMSType]] = None, | ||
| tz: Union[tzinfo, str] = pytz.timezone("Europe/Oslo"), | ||
| tz: Union[tzinfo, str] = ZoneInfo("Europe/Oslo"), |
Comment on lines
+214
to
+218
| self.tz = ZoneInfo(tz) | ||
| except ZoneInfoNotFoundError: | ||
| raise ValueError( | ||
| f"Invalid timezone string. Given type was {type(tz)}" | ||
| ) from None |
Comment on lines
26
to
+28
| assert ensure_datetime_with_tz( | ||
| "02.01.03 00:00:00", pytz.timezone("Brazil/East") | ||
| ) == timezone("Brazil/East").localize(datetime.datetime(2003, 1, 2, 0, 0, 0)) | ||
| "02.01.03 00:00:00", ZoneInfo("America/Sao_Paulo") | ||
| ) == datetime.datetime(2003, 1, 2, 0, 0, 0, tzinfo=ZoneInfo("America/Sao_Paulo")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.