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
cftime
issue #11 (linked to legacynetcdf4-python
issue Unidata/netcdf4-python#639) and associatedcftime
PR #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.datetime
and 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.datetime
objectdt
,where
pdt1
andpdt2
are irisPartialDateTime
objects.As it stands at the moment with
cftime
1.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 aTypeError
if acftime.datetime
object 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
TypeError
will 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 < pdt2
comparison, thedt
object will first check that the other custompdt2
object supports either__richcmp__
or__gt__
operations, before delegating the comparison operation to thepdt2
object via returningNotImplemented
. Otherwise, aTypeError
exception 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!