diff --git a/Changelog b/Changelog index 5a737acd..82fa7b8a 100644 --- a/Changelog +++ b/Changelog @@ -15,6 +15,7 @@ version 1.5.0 (not yet released) * remove legacy `utime` class, and legacy `JulianDayFromDate` and `DateFromJulianDay` functions (replaced by `cftime.datetime.toordinal` and `cftime.datetime.fromordinal`). PR #235. + * Change ValueError to TypeError in __sub__ (issue #236, PR #236). version 1.4.1 (release tag v1.4.1.rel) ====================================== diff --git a/src/cftime/_cftime.pyx b/src/cftime/_cftime.pyx index d7e0b9a6..5df026e5 100644 --- a/src/cftime/_cftime.pyx +++ b/src/cftime/_cftime.pyx @@ -1366,11 +1366,11 @@ The default format of the string produced by strftime is controlled by self.form if isinstance(other, datetime): # datetime - datetime if dt.calendar != other.calendar: - raise ValueError("cannot compute the time difference between dates with different calendars") + raise TypeError("cannot compute the time difference between dates with different calendars") if dt.calendar == "": - raise ValueError("cannot compute the time difference between dates that are not calendar-aware") + raise TypeError("cannot compute the time difference between dates that are not calendar-aware") if dt.has_year_zero != other.has_year_zero: - raise ValueError("cannot compute the time difference between dates with year zero conventions") + raise TypeError("cannot compute the time difference between dates with year zero conventions") ordinal_self = self.toordinal() # julian day ordinal_other = other.toordinal() days = ordinal_self - ordinal_other @@ -1387,7 +1387,7 @@ Cannot compute the time difference between dates with different calendars. One of the datetime objects may have been converted to a native python datetime instance. Try using only_use_cftime_datetimes=True when creating the datetime object.""" - raise ValueError(msg) + raise TypeError(msg) return dt._to_real_datetime() - other elif isinstance(other, timedelta): # datetime - timedelta @@ -1428,7 +1428,7 @@ Cannot compute the time difference between dates with different calendars. One of the datetime objects may have been converted to a native python datetime instance. Try using only_use_cftime_datetimes=True when creating the datetime object.""" - raise ValueError(msg) + raise TypeError(msg) return self - other._to_real_datetime() else: return NotImplemented diff --git a/test/test_cftime.py b/test/test_cftime.py index ec09e8f1..34714d56 100644 --- a/test/test_cftime.py +++ b/test/test_cftime.py @@ -1284,12 +1284,9 @@ def invalid_sub_5(): def invalid_sub_6(): self.date3_gregorian - self.date3_gregorian_yearzero - for func in [invalid_sub_1, invalid_sub_2]: + for func in [invalid_sub_1, invalid_sub_2, invalid_sub_3, invalid_sub_4, invalid_sub_5, invalid_sub_6]: self.assertRaises(TypeError, func) - for func in [invalid_sub_3, invalid_sub_4, invalid_sub_5, invalid_sub_6] : - self.assertRaises(ValueError, func) - def test_replace(self): self.assertEqual(self.date1_365_day.replace(year=4000).year, 4000) self.assertEqual(self.date1_365_day.replace(month=3).month, 3)