Skip to content

Timezone classes should implement __eq__ #502

@jcrist

Description

@jcrist

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions