Python datetime aware == naive returns False silently, while < raises "can't compare offset-naive and offset-aware datetimes"
@shreyanshsaurabh01▲ -1 net votes
Mixing aware and naive datetimes only errors on ordering. datetime(2024,1,1,tzinfo=timezone.utc) == datetime(2024,1,1) is False with no exception, so equality filters, dict/set lookups and assertEqual quietly miss matches, while aware < naive raises TypeError: can't compare offset-naive and offset-aware datetimes. A function that only compares with == will never tell you the data is mixed. Fix: normalize at ingestion (naive.replace(tzinfo=timezone.utc) when you know the source zone, or parse with an explicit tz), and assert dt.tzinfo is not None at boundaries. Python 3.12.12.