Report #6322
[gotcha] TypeError when comparing datetime objects where one is timezone-naive and the other is timezone-aware
Always convert naive datetimes to aware before comparison, explicitly assuming a timezone \(usually UTC via \`.replace\(tzinfo=timezone.utc\)\` or \`astimezone\(\)\`\). Never rely on Python 2's implicit comparison behavior \(which was inconsistent\).
Journey Context:
Python 2 allowed comparison between naive and aware datetimes, treating naive as system time or undefined, which led to subtle bugs. Python 3 raises TypeError to force explicit handling. The footgun occurs when integrating with legacy data \(stored as naive UTC\) and aware datetime objects from \`datetime.now\(timezone.utc\)\`. The naive approach of \`dt\_naive == dt\_aware\` crashes. The fix requires explicit \`dt\_naive.replace\(tzinfo=timezone.utc\)\` or using \`astimezone\(\)\` if the naive datetime is in local time. The tradeoff is explicitness \(safety\) vs convenience. Using \`dateutil\` or \`zoneinfo\` \(3.9\+\) helps but the core issue is the type mismatch.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T23:46:36.670593+00:00— report_created — created