Hi there! 👋
I noticed that the codebase uses datetime.utcnow() or datetime.utcfromtimestamp(). These are deprecated and won't work with Python 3.12. They also handle naïve datetimes, which can lead to bugs. Could we switch to timezone-aware alternatives?
CodeQL Alerts
Here are the specific instances CodeQL flagged:
-
Explanation
Issue:
datetime.utcnow() and datetime.utcfromtimestamp() return naïve datetimes (without timezone info).
- In Python 3, naïve datetimes are interpreted as system-local times, causing inconsistencies.
- These methods are deprecated and no longer work in Python 3.12.
Example Problem:
from datetime import datetime
ts = 1571595618.0
x = datetime.utcfromtimestamp(ts)
x_ts = x.timestamp()
assert ts == x_ts, f"{ts} != {x_ts}" # Can fail in non-UTC locales
Recommended Solution:
Switch to time zone-aware methods:
from datetime import datetime, timezone
# Replace utcnow()
dt_now = datetime.now(tz=timezone.utc)
# Replace utcfromtimestamp()
ts = 1571595618.0
x = datetime.fromtimestamp(ts, tz=timezone.utc)
x_ts = x.timestamp()
assert ts == x_ts, f"{ts} != {x_ts}" # This succeeds
Action Required:
- Replace all instances of
datetime.utcnow() with datetime.now(tz=timezone.utc).
- Replace all instances of
datetime.utcfromtimestamp() with datetime.fromtimestamp(ts, tz=timezone.utc).
References:
For more details, see:
Thank you so much for your time and effort in maintaining this project! 🌟
Best,
Shrey
Hi there! 👋
I noticed that the codebase uses
datetime.utcnow()ordatetime.utcfromtimestamp(). These are deprecated and won't work with Python 3.12. They also handle naïve datetimes, which can lead to bugs. Could we switch to timezone-aware alternatives?CodeQL Alerts
Here are the specific instances CodeQL flagged:
feathub/python/feathub/registries/mysql_registry.py
Line 173 in 15d6382
Explanation
Issue:
datetime.utcnow()anddatetime.utcfromtimestamp()return naïve datetimes (without timezone info).Example Problem:
Recommended Solution:
Switch to time zone-aware methods:
Action Required:
datetime.utcnow()withdatetime.now(tz=timezone.utc).datetime.utcfromtimestamp()withdatetime.fromtimestamp(ts, tz=timezone.utc).References:
For more details, see:
Thank you so much for your time and effort in maintaining this project! 🌟
Best,
Shrey