Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timezone classes should implement __eq__ #502

Closed
jcrist opened this issue Oct 7, 2020 · 2 comments · Fixed by #698
Closed

Timezone classes should implement __eq__ #502

jcrist opened this issue Oct 7, 2020 · 2 comments · Fixed by #698

Comments

@jcrist
Copy link

jcrist commented Oct 7, 2020

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
Copy link

TediaN97 commented Oct 9, 2020

i would like if u assign me on this issue

@dylrich
Copy link

dylrich commented Mar 26, 2021

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

@malthe malthe mentioned this issue Mar 20, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants