diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 73cc419c5e..bbcc2266e4 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -57,6 +57,9 @@ This document explains the changes made to Iris for this release attribute. Also made cell_method string parsing more lenient w.r.t. whitespace. (:pull:`6083`) +#. `@ukmo-ccbunney`_ fixed comparison of cubes with array type attributes; + fixes :issue:`6027` (:pull:`6181`) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/_merge.py b/lib/iris/_merge.py index 2d8beb6f27..5e00d9b2f0 100644 --- a/lib/iris/_merge.py +++ b/lib/iris/_merge.py @@ -388,7 +388,9 @@ def _defn_msgs(self, other_defn): diff_attrs = [ repr(key[1]) for key in attrs_1 - if np.all(attrs_1[key] != attrs_2[key]) + if not np.array_equal( + np.array(attrs_1[key], ndmin=1), np.array(attrs_2[key], ndmin=1) + ) ] diff_attrs = ", ".join(sorted(diff_attrs)) msgs.append( diff --git a/lib/iris/common/mixin.py b/lib/iris/common/mixin.py index 8e89f0ccd0..87d58944c7 100644 --- a/lib/iris/common/mixin.py +++ b/lib/iris/common/mixin.py @@ -11,6 +11,7 @@ from typing import Any import cf_units +import numpy as np import iris.std_names @@ -104,11 +105,9 @@ def __eq__(self, other): match = set(self.keys()) == set(other.keys()) if match: for key, value in self.items(): - match = value == other[key] - try: - match = bool(match) - except ValueError: - match = match.all() + match = np.array_equal( + np.array(value, ndmin=1), np.array(other[key], ndmin=1) + ) if not match: break return match diff --git a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py index f538279bb1..dfb49f0f8d 100644 --- a/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py +++ b/lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py @@ -42,12 +42,22 @@ def test___eq___numpy(self): right = LimitedAttributeDict(**values) self.assertEqual(left, right) self.assertEqual(left, values) + values = dict(one=np.arange(1), two=np.arange(1), three=np.arange(1)) left = LimitedAttributeDict(dict(one=0, two=0, three=0)) right = LimitedAttributeDict(**values) self.assertEqual(left, right) self.assertEqual(left, values) + # Test inequality: + values = dict(one=np.arange(1), two=np.arange(2), three=np.arange(3)) + left = LimitedAttributeDict(**values) + right = LimitedAttributeDict( + one=np.arange(3), two=np.arange(2), three=np.arange(1) + ) + self.assertNotEqual(left, right) + self.assertNotEqual(values, right) + def test___setitem__(self): for key in self.forbidden_keys: item = LimitedAttributeDict()