sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.21k stars 386 forks source link

Timezone classes should implement `__eq__` #502

Open jcrist opened 3 years ago

jcrist commented 3 years ago

Identical timezones don't compare equal, since equality is currently implemented via instance comparison.

In [28]: from pendulum.tz.timezone import Timezone

In [29]: t1 = Timezone("UTC")

In [30]: t2 = Timezone("UTC")

In [31]: t1 == t2
Out[31]: False

This is most likely to come up when pickling pendulum instances - when unpickled the timezone instances will no longer match:

In [44]: import pendulum

In [45]: import pickle

In [46]: t = pendulum.now()

In [47]: t2 = pickle.loads(pickle.dumps(t))

In [48]: t.tzinfo
Out[48]: Timezone('America/Chicago')

In [49]: t2.tzinfo
Out[49]: Timezone('America/Chicago')

In [50]: t.tzinfo == t2.tzinfo.  # I'd expect this to be true
Out[50]: False

This came up when using pendulum objects with pandas and dask. Some pandas methods validate the datetime objects input by checking that all provided timezones are identical. After deserializing the pendulum objects, the timezones no longer compared identical, even though they were equivalent.


It looks like there was an intent to cache instances with the same name here:

https://github.com/sdispater/pendulum/blob/daa4b936daf3f4dfa7d211aa0ac1e9d82d5401d4/pendulum/tz/__init__.py#L34-L35

Since the unpickling code doesn't go through this path, the cache isn't used on load. Either the caching should be moved into the classes themselves, or __eq__ should be implemented so multiple equivalent instances still compare equal.

TediaN97 commented 3 years ago

i would like if u assign me on this issue

dylrich commented 3 years ago

This has been problematic for us in some of our tests which dynamically load modules - would love a fix!