Skip to content

Commit

Permalink
Merge pull request #237 from Unidata/issue236
Browse files Browse the repository at this point in the history
Change ValueError to TypeError in datetime.__sub__ (closes issue #236)
  • Loading branch information
jswhit authored Mar 26, 2021
2 parents d8fb27f + 71da1ea commit 84ad19d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -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)
======================================
Expand Down
10 changes: 5 additions & 5 deletions src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 84ad19d

Please sign in to comment.