Fix deprecated datetime.utcnow() in test fixture#307
Conversation
…est.py Agent-Logs-Url: https://github.com/wolph/python-progressbar/sessions/2475a328-cf62-4d16-91e5-13ac2078e52d Co-authored-by: wolph <270571+wolph@users.noreply.github.com>
datetime.utcnow() in test fixture
|
/gemini review |
There was a problem hiding this comment.
Pull request overview
Updates the test-time timezone offset calculation used by freezegun to avoid deprecated datetime.utcnow() usage on newer Python versions.
Changes:
- Replace
datetime.utcnow()-based offset computation withdatetime.now(timezone.utc).astimezone().utcoffset(). - Preserve correct handling for negative and fractional-hour timezone offsets by using
timedelta.total_seconds().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| offset_hours = int(offset_seconds / 3600) | ||
| # Compute the local UTC offset so freezegun uses the same timezone as | ||
| # the local system. Using datetime.now(timezone.utc).astimezone() avoids | ||
| # the deprecated datetime.utcnow() which was removed in Python 3.12+. |
There was a problem hiding this comment.
The comment says datetime.utcnow() “was removed in Python 3.12+”, but it was deprecated in 3.12 (and still exists). Suggest rewording to avoid misinformation (e.g., “deprecated since Python 3.12; use timezone-aware datetimes instead”).
| # the deprecated datetime.utcnow() which was removed in Python 3.12+. | |
| # the deprecated datetime.utcnow() (deprecated since Python 3.12) and | |
| # uses a timezone-aware datetime instead. |
There was a problem hiding this comment.
Code Review
This pull request updates the sleep_faster fixture in tests/conftest.py to replace the deprecated datetime.utcnow() with a Python 3.12-compatible approach for calculating local UTC offsets. Feedback suggests simplifying the logic by passing the timedelta object directly to freezegun.freeze_time rather than manually converting it to hours.
| local_offset = datetime.now(timezone.utc).astimezone().utcoffset() | ||
| offset_hours = local_offset.total_seconds() / 3600 | ||
|
|
||
| freeze_time = freezegun.freeze_time(tz_offset=offset_hours) |
There was a problem hiding this comment.
The freezegun.freeze_time function accepts a timedelta object directly for the tz_offset parameter. You can simplify this logic by passing local_offset directly, which avoids the manual conversion to hours and handles fractional offsets more cleanly without precision loss.
| local_offset = datetime.now(timezone.utc).astimezone().utcoffset() | |
| offset_hours = local_offset.total_seconds() / 3600 | |
| freeze_time = freezegun.freeze_time(tz_offset=offset_hours) | |
| local_offset = datetime.now(timezone.utc).astimezone().utcoffset() | |
| freeze_time = freezegun.freeze_time(tz_offset=local_offset) |
|
@copilot Did you benchmark the performance before/after? Are there any other places in the code that use |
Agent-Logs-Url: https://github.com/wolph/python-progressbar/sessions/fdfa1d3f-c76d-4b45-a83f-e4d4446e7db3 Co-authored-by: wolph <270571+wolph@users.noreply.github.com>
No benchmarking needed here — the offset is computed once per test fixture setup (not in a hot path), so there's no meaningful performance difference. I also searched the entire codebase for |
Python 3.12+ deprecated
datetime.utcnow(), and Python 3.13 surfaces additional datetime-relatedDeprecationWarnings around locale-sensitive date parsing. The test fixture inconftest.pywas using the deprecated API to compute the local UTC offset forfreezegun.Changes
tests/conftest.py: Replacedatetime.utcnow()withdatetime.now(timezone.utc).astimezone().utcoffset()to compute the local timezone offsetThe new approach also fixes two latent bugs in the old calculation:
.secondson a negativetimedeltareturns the wrong value for UTC− timezones (e.g. UTC−5 would yield68400instead of−18000)freezegun'stz_offsetcorrectly handles fractional-hour offsets (e.g. UTC+5:30), removing the need for the+ 10buffer hackWarning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
docs.python.org/home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js(dns block)If you need me to access, download, or install something from one of these locations, you can either: