1

Why can't Python type checker (I tried Pyre) find this type error?

from datetime import datetime
import pytz

utc = pytz.utc
a = utc.localize(datetime.now())

print(a)
#2021-12-11 15:55:35.187927+00:00

print( datetime.now())
#2021-12-11 15:55:35.188028

if a > datetime.now():  # type error here. TypeError: can't compare offset-naive and offset-aware datetimes
    print("done")
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Jason O.
  • 3,168
  • 6
  • 33
  • 72
  • 2
    because its not one that can be detected statically - the type of a offset-naive datetime and the type of a tz-aware datetime is exactly the same. – SuperStormer Dec 12 '21 at 00:02
  • 1
    This is essentially a flaw of the `datetime` module; if it had different types for naive vs aware datetimes (which it absolutely could) this would be detectable statically. – Samwise Dec 12 '21 at 01:05

0 Answers0