Skip to content

Commit

Permalink
Remove in-house long time interval checking. (#279)
Browse files Browse the repository at this point in the history
* Remove in-house long time interval checking.

* Restore is_long_time_interval() with a DeprecationWarning.

* Make test_deprecated a staticmethod.

* Fix to problem introduced in conflict resolution.

* Advance deprecation version.

Co-authored-by: Bill Little <bill.little@metoffice.gov.uk>

---------

Co-authored-by: Bill Little <bill.little@metoffice.gov.uk>
  • Loading branch information
trexfeathers and bjlittle committed Oct 12, 2023
1 parent 0b14dc1 commit b8d8bfc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
22 changes: 13 additions & 9 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import copy
import math
from contextlib import contextmanager
from warnings import warn

import cftime
import numpy as np
Expand Down Expand Up @@ -873,6 +874,12 @@ def is_long_time_interval(self):
discrepancy means we cannot run self.num2date() on a time unit with
a long time interval.
.. deprecated:: 3.3.0
Invalid long time intervals are now defended against within
cftime - do not use this routine, as cftime knows best what it can
and cannot support.
Returns:
Boolean.
Expand All @@ -887,6 +894,12 @@ def is_long_time_interval(self):
True
"""
deprecation = (
"This method is no longer needed due to cftime's improved "
"handling of long time intervals."
)
warn(deprecation, DeprecationWarning, stacklevel=2)

result = False
long_time_intervals = ["year", "month"]
if self.is_time_reference():
Expand Down Expand Up @@ -1830,15 +1843,6 @@ def cftime_unit(self):
if self.calendar is None:
raise ValueError("Unit has undefined calendar")

# `cftime` cannot parse long time intervals ("months" or "years").
if self.is_long_time_interval():
interval = self.origin.split(" ")[0]
emsg = (
'Time units with interval of "months", "years" '
'(or singular of these) cannot be processed, got "{!s}".'
)
raise ValueError(emsg.format(interval))

#
# ensure to strip out non-parsable 'UTC' postfix, which
# is generated by UDUNITS-2 formatted output
Expand Down
8 changes: 0 additions & 8 deletions cf_units/tests/integration/test_date2num.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,3 @@ def test_convert_microsecond(self):
res = date2num(date, self.unit, self.calendar)

assert exp == pytest.approx(res)

def test_long_time_interval(self):
# This test should fail with an error that we need to catch properly.
unit = "years since 1970-01-01"
date = datetime.datetime(1970, 1, 1, 0, 0, 5)
exp_emsg = 'interval of "months", "years" .* got "years".'
with pytest.raises(ValueError, match=exp_emsg):
date2num(date, unit, self.calendar)
13 changes: 8 additions & 5 deletions cf_units/tests/unit/unit/test_Unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ def test_no_change(self):
# the same object.
assert result is not u

def test_long_time_interval(self):
u = Unit("months since 1970-01-01", calendar="standard")
with pytest.raises(ValueError, match="cannot be processed"):
u.change_calendar("proleptic_gregorian")

def test_wrong_calendar(self):
u = Unit("days since 1900-01-01", calendar="360_day")
with pytest.raises(
Expand Down Expand Up @@ -273,6 +268,14 @@ def test_type_conversion(self):


class Test_is_long_time_interval:
@staticmethod
def test_deprecated():
unit = Unit("seconds since epoch")
with pytest.warns(
DeprecationWarning, match="This method is no longer needed"
):
_ = unit.is_long_time_interval()

def test_short_time_interval(self):
# A short time interval is a time interval from seconds to days.
unit = Unit("seconds since epoch")
Expand Down

0 comments on commit b8d8bfc

Please sign in to comment.