pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

Timezone classes should implement `__eq__`

Open jcrist opened this issue 4 years ago • 2 comments

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.

jcrist avatar Oct 07 '20 22:10 jcrist

i would like if u assign me on this issue

TediaN97 avatar Oct 09 '20 14:10 TediaN97

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

dylrich avatar Mar 26 '21 05:03 dylrich