Skip to content

Commit

Permalink
Cell comparison: remove ancient netcdftime/cftime workarounds (#4729)
Browse files Browse the repository at this point in the history
* remove ancient workarounds

* whatsnew
  • Loading branch information
rcomer authored Oct 7, 2022
1 parent bae9953 commit d1f3e45
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 55 deletions.
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ This document explains the changes made to Iris for this release
variables and cell measures that map to a cube dimension of length 1 are now
included in the respective vector sections. (:pull:`4945`)

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


💣 Incompatible Changes
=======================
Expand Down
25 changes: 1 addition & 24 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import warnings
import zlib

import cftime
import dask.array as da
import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -1411,16 +1410,6 @@ def __common_cmp__(self, other, operator_method):
):
raise ValueError("Unexpected operator_method")

# Prevent silent errors resulting from missing cftime
# behaviour.
if isinstance(other, cftime.datetime) or (
isinstance(self.point, cftime.datetime)
and not isinstance(other, iris.time.PartialDateTime)
):
raise TypeError(
"Cannot determine the order of " "cftime.datetime objects"
)

if isinstance(other, Cell):
# Cell vs Cell comparison for providing a strict sort order
if self.bound is None:
Expand Down Expand Up @@ -1485,19 +1474,7 @@ def __common_cmp__(self, other, operator_method):
else:
me = max(self.bound)

# Work around to handle cftime.datetime comparison, which
# doesn't return NotImplemented on failure in some versions of the
# library
try:
result = operator_method(me, other)
except TypeError:
rop = {
operator.lt: operator.gt,
operator.gt: operator.lt,
operator.le: operator.ge,
operator.ge: operator.le,
}[operator_method]
result = rop(other, me)
result = operator_method(me, other)

return result

Expand Down
34 changes: 3 additions & 31 deletions lib/iris/tests/unit/coords/test_Cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,6 @@ def assert_raises_on_comparison(self, cell, other, exception_type, regexp):
with self.assertRaisesRegex(exception_type, regexp):
cell >= other

def test_cftime_cell(self):
# Check that cell comparison when the cell contains
# cftime.datetime objects raises an exception otherwise
# this will fall back to id comparison producing unreliable
# results.
cell = Cell(cftime.datetime(2010, 3, 21))
dt = mock.Mock(timetuple=mock.Mock())
self.assert_raises_on_comparison(
cell, dt, TypeError, "determine the order of cftime"
)
self.assert_raises_on_comparison(
cell, 23, TypeError, "determine the order of cftime"
)
self.assert_raises_on_comparison(
cell, "hello", TypeError, "Unexpected type.*str"
)

def test_cftime_other(self):
# Check that cell comparison to a cftime.datetime object
# raises an exception otherwise this will fall back to id comparison
# producing unreliable results.
dt = cftime.datetime(2010, 3, 21)
cell = Cell(mock.Mock(timetuple=mock.Mock()))
self.assert_raises_on_comparison(
cell, dt, TypeError, "determine the order of cftime"
)

def test_PartialDateTime_bounded_cell(self):
# Check that bounded comparisions to a PartialDateTime
# raise an exception. These are not supported as they
Expand Down Expand Up @@ -85,10 +58,9 @@ def test_PartialDateTime_unbounded_cell(self):
def test_datetime_unbounded_cell(self):
# Check that cell comparison works with datetimes.
dt = datetime.datetime(2000, 6, 15)
cell = Cell(datetime.datetime(2000, 1, 1))
# Note the absence of the inverse of these
# e.g. self.assertGreater(dt, cell).
# See http://bugs.python.org/issue8005
cell = Cell(cftime.datetime(2000, 1, 1))
self.assertGreater(dt, cell)
self.assertGreaterEqual(dt, cell)
self.assertLess(cell, dt)
self.assertLessEqual(cell, dt)

Expand Down

0 comments on commit d1f3e45

Please sign in to comment.