Fix cftime.datetime.__richcmp__ #81
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR re-addresses the now closed
cftimeissue #11 (linked to legacynetcdf4-pythonissue Unidata/netcdf4-python#639) and associatedcftimePR #53.The current implementation of
cftime.datetime.__richcmp__does not align with the old sought after behaviour alluded to in #11, and from my perspective that's a bit of a problem... as it's now not possible to perform (previously supported and operational) comparison operations betweencftime.datetimeand other custom datetime objects i.e. more specifically an iris PartialDateTime calendar agnostic objects.For example, it was possible to do the following comparison using a
netcdftime.datetimeobjectdt,where
pdt1andpdt2are irisPartialDateTimeobjects.As it stands at the moment with
cftime1.0.2.1, users are forced to do the equivalent comparison as follows, due to the implementation restrictions ofcftime.datetime.__richcmp__,as the current implementation of the
cftime.datetime.__richcmp__method will raise aTypeErrorif acftime.datetimeobject is a LHS operand participating in a rich comparison operation with another custom object (in Python2).Apologies for the long winded introduction 😰 (I figured that context for this PR would be helpful!)... but this PR bridges the gap back to the old behaviour, and maintains closer consistency with the behaviour between Python2 and Python3 for
cftime.datetime.In a nutshell, for Python3 it's business as usual for
cftime.datetime.__richcmp__. However for Python2, rich comparisons with other custom objects will be performed by delegating the comparison to the other object only if it advertises that it supports Cython__richcmp__or a suitable traditional rich comparison operator i.e.__lt__,__le__,__eq__,__ne__,__gt__, or__ge__, given the context of the current comparison.A
TypeErrorwill only be raised otherwise, thus ensuring that the other object__cmp__behaviour is circumvented, and more importantly, the default Python object ID comparison is avoided.For example, if we are doing a Python2
dt < pdt2comparison, thedtobject will first check that the other custompdt2object supports either__richcmp__or__gt__operations, before delegating the comparison operation to thepdt2object via returningNotImplemented. Otherwise, aTypeErrorexception is raised.Needless to say, the changes in this PR extends
cftime.datetime.__richcmp__support such that it again permits users to dopdt1<= dt < pdt2`, whoop!