Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove in-house long time interval checking. #279

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading