From b8d8bfc39639b3a8029975cb456e5860750bb246 Mon Sep 17 00:00:00 2001 From: Martin Yeo <40734014+trexfeathers@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:53:13 +0100 Subject: [PATCH] Remove in-house long time interval checking. (#279) * 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 --------- Co-authored-by: Bill Little --- cf_units/__init__.py | 22 ++++++++++++--------- cf_units/tests/integration/test_date2num.py | 8 -------- cf_units/tests/unit/unit/test_Unit.py | 13 +++++++----- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 4f9aedef..33366729 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -16,6 +16,7 @@ import copy import math from contextlib import contextmanager +from warnings import warn import cftime import numpy as np @@ -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. @@ -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(): @@ -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 diff --git a/cf_units/tests/integration/test_date2num.py b/cf_units/tests/integration/test_date2num.py index a067e6bc..020f1421 100644 --- a/cf_units/tests/integration/test_date2num.py +++ b/cf_units/tests/integration/test_date2num.py @@ -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) diff --git a/cf_units/tests/unit/unit/test_Unit.py b/cf_units/tests/unit/unit/test_Unit.py index 170f41f5..2293cbae 100644 --- a/cf_units/tests/unit/unit/test_Unit.py +++ b/cf_units/tests/unit/unit/test_Unit.py @@ -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( @@ -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")