fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)#2111
Conversation
…ne.utc) `datetime.utcnow()` has been deprecated since Python 3.12 because it returns a naive datetime that carries no timezone information, making it error-prone when mixed with timezone-aware datetimes. Replace all 8 occurrences across 7 files (5 source, 2 test) with the recommended `datetime.now(tz=timezone.utc)` which returns a proper timezone-aware datetime. Files changed: - elementary/utils/time.py - elementary/monitor/fetchers/alerts/schema/pending_alerts.py - elementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py - elementary/messages/messaging_integrations/teams_webhook.py - elementary/messages/messaging_integrations/slack_webhook.py - elementary/messages/messaging_integrations/file_system.py - tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.py - tests/mocks/fetchers/alerts_fetcher_mock.py
|
👋 @themavik |
📝 WalkthroughWalkthroughThis PR systematically replaces naive UTC timestamps with timezone-aware UTC timestamps across 8 files by importing Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
datetime.utcnow()has been deprecated since Python 3.12 because it returns a naive datetime object that carries no timezone information, making it error-prone when mixed with timezone-aware datetimes. The recommended replacement isdatetime.now(tz=timezone.utc)which returns a proper timezone-aware UTC datetime.Changes
Replaced all 8 occurrences of
datetime.utcnow()across 7 files:Source files (5)
elementary/utils/time.py—get_now_utc_str()elementary/monitor/fetchers/alerts/schema/pending_alerts.py— alert timestamp defaultselementary/monitor/data_monitoring/alerts/data_monitoring_alerts.py— suppression interval comparisonelementary/messages/messaging_integrations/teams_webhook.py— message send timestampelementary/messages/messaging_integrations/slack_webhook.py— message send timestampelementary/messages/messaging_integrations/file_system.py— filename generation and send timestampTest files (2)
tests/unit/monitor/fetchers/alerts/schemas/test_alert_data_schema.pytests/mocks/fetchers/alerts_fetcher_mock.pyBehavior
The returned datetime values are identical in absolute terms (both represent "now in UTC"), but
datetime.now(tz=timezone.utc)is timezone-aware and won't produce deprecation warnings in Python 3.12+.Assisted by Claude Opus 4.6 max thinking
Made with Cursor
Summary by CodeRabbit
Release Notes