Skip to content

Commit d1f3e45

Browse files
authored
Cell comparison: remove ancient netcdftime/cftime workarounds (#4729)
* remove ancient workarounds * whatsnew
1 parent bae9953 commit d1f3e45

File tree

3 files changed

+7
-55
lines changed

3 files changed

+7
-55
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ This document explains the changes made to Iris for this release
6060
variables and cell measures that map to a cube dimension of length 1 are now
6161
included in the respective vector sections. (:pull:`4945`)
6262

63+
#. `@rcomer`_ removed some old redundant code that prevented determining the
64+
order of time cells. (:issue:`4697`, :pull:`4729`)
65+
6366

6467
💣 Incompatible Changes
6568
=======================

lib/iris/coords.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import warnings
1919
import zlib
2020

21-
import cftime
2221
import dask.array as da
2322
import numpy as np
2423
import numpy.ma as ma
@@ -1411,16 +1410,6 @@ def __common_cmp__(self, other, operator_method):
14111410
):
14121411
raise ValueError("Unexpected operator_method")
14131412

1414-
# Prevent silent errors resulting from missing cftime
1415-
# behaviour.
1416-
if isinstance(other, cftime.datetime) or (
1417-
isinstance(self.point, cftime.datetime)
1418-
and not isinstance(other, iris.time.PartialDateTime)
1419-
):
1420-
raise TypeError(
1421-
"Cannot determine the order of " "cftime.datetime objects"
1422-
)
1423-
14241413
if isinstance(other, Cell):
14251414
# Cell vs Cell comparison for providing a strict sort order
14261415
if self.bound is None:
@@ -1485,19 +1474,7 @@ def __common_cmp__(self, other, operator_method):
14851474
else:
14861475
me = max(self.bound)
14871476

1488-
# Work around to handle cftime.datetime comparison, which
1489-
# doesn't return NotImplemented on failure in some versions of the
1490-
# library
1491-
try:
1492-
result = operator_method(me, other)
1493-
except TypeError:
1494-
rop = {
1495-
operator.lt: operator.gt,
1496-
operator.gt: operator.lt,
1497-
operator.le: operator.ge,
1498-
operator.ge: operator.le,
1499-
}[operator_method]
1500-
result = rop(other, me)
1477+
result = operator_method(me, other)
15011478

15021479
return result
15031480

lib/iris/tests/unit/coords/test_Cell.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,6 @@ def assert_raises_on_comparison(self, cell, other, exception_type, regexp):
3030
with self.assertRaisesRegex(exception_type, regexp):
3131
cell >= other
3232

33-
def test_cftime_cell(self):
34-
# Check that cell comparison when the cell contains
35-
# cftime.datetime objects raises an exception otherwise
36-
# this will fall back to id comparison producing unreliable
37-
# results.
38-
cell = Cell(cftime.datetime(2010, 3, 21))
39-
dt = mock.Mock(timetuple=mock.Mock())
40-
self.assert_raises_on_comparison(
41-
cell, dt, TypeError, "determine the order of cftime"
42-
)
43-
self.assert_raises_on_comparison(
44-
cell, 23, TypeError, "determine the order of cftime"
45-
)
46-
self.assert_raises_on_comparison(
47-
cell, "hello", TypeError, "Unexpected type.*str"
48-
)
49-
50-
def test_cftime_other(self):
51-
# Check that cell comparison to a cftime.datetime object
52-
# raises an exception otherwise this will fall back to id comparison
53-
# producing unreliable results.
54-
dt = cftime.datetime(2010, 3, 21)
55-
cell = Cell(mock.Mock(timetuple=mock.Mock()))
56-
self.assert_raises_on_comparison(
57-
cell, dt, TypeError, "determine the order of cftime"
58-
)
59-
6033
def test_PartialDateTime_bounded_cell(self):
6134
# Check that bounded comparisions to a PartialDateTime
6235
# raise an exception. These are not supported as they
@@ -85,10 +58,9 @@ def test_PartialDateTime_unbounded_cell(self):
8558
def test_datetime_unbounded_cell(self):
8659
# Check that cell comparison works with datetimes.
8760
dt = datetime.datetime(2000, 6, 15)
88-
cell = Cell(datetime.datetime(2000, 1, 1))
89-
# Note the absence of the inverse of these
90-
# e.g. self.assertGreater(dt, cell).
91-
# See http://bugs.python.org/issue8005
61+
cell = Cell(cftime.datetime(2000, 1, 1))
62+
self.assertGreater(dt, cell)
63+
self.assertGreaterEqual(dt, cell)
9264
self.assertLess(cell, dt)
9365
self.assertLessEqual(cell, dt)
9466

0 commit comments

Comments
 (0)