diff --git a/cftime/_cftime.pyx b/cftime/_cftime.pyx index c5bedd44..9886b1a1 100644 --- a/cftime/_cftime.pyx +++ b/cftime/_cftime.pyx @@ -8,6 +8,7 @@ import numpy as np import math import numpy import re +import sys import time from datetime import datetime as real_datetime from datetime import timedelta, MINYEAR @@ -1615,7 +1616,17 @@ Gregorial calendar. raise TypeError("cannot compare {0!r} and {1!r} (different calendars)".format(self, other)) return PyObject_RichCompare(dt.to_tuple(), to_tuple(other), op) else: - raise TypeError("cannot compare {0!r} and {1!r}".format(self, other)) + # With Python3 we can use "return NotImplemented". If the other + # object does not have rich comparison instructions for cftime + # then a TypeError is automatically raised. With Python2 in this + # scenario the default behaviour is to compare the object ids + # which will always have a result. Therefore there is no way to + # differentiate between objects that do or do not have legitimate + # comparisons, and so we cannot remove the TypeError below. + if sys.version_info[0] < 3: + raise TypeError("cannot compare {0!r} and {1!r}".format(self, other)) + else: + return NotImplemented cdef _getstate(self): return (self.year, self.month, self.day, self.hour,