Report #103695
[gotcha] Comparing naive and aware \`datetime\` objects raises \`TypeError\` silently in Python 3.3\+
Always use timezone-aware datetimes: \`datetime.now\(timezone.utc\)\` instead of \`datetime.utcnow\(\)\` or \`datetime.now\(\)\`. For user input, explicitly attach a timezone with \`pytz\` or \`zoneinfo\`. Use \`datetime.astimezone\(\)\` to convert before comparison.
Journey Context:
Python 3.3 introduced a new comparison rule: comparing a naive datetime \(without tzinfo\) with an aware datetime raises \`TypeError\`. This is a silent change from Python 2 where it would compare as if both were naive. Common pitfalls: \`datetime.utcnow\(\)\` returns a naive datetime representing UTC time, but it's not timezone-aware. \`datetime.datetime.now\(\)\` also returns naive. The fix is to use \`timezone.utc\` explicitly. The tradeoff is that you must always manage timezones, which adds complexity but prevents bugs like comparing times from different timezones as if they were equal. The \`zoneinfo\` module \(Python 3.9\+\) is the modern standard, replacing \`pytz\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-12T20:05:20.612722+00:00— report_created — created