Skip to content

Commit f58f050

Browse files
Fix attribute array comparison (#6181)
* Initial fix ... * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Cleaned up old comments. Extended unit test to cover inequality * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed dev comments * Elevated attribute comparison values to np.arrays * Added WhatsNew entry --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b9a950a commit f58f050

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ This document explains the changes made to Iris for this release
6161
attribute. Also made cell_method string parsing more lenient w.r.t.
6262
whitespace. (:pull:`6083`)
6363

64+
#. `@ukmo-ccbunney`_ fixed comparison of cubes with array type attributes;
65+
fixes :issue:`6027` (:pull:`6181`)
66+
6467
💣 Incompatible Changes
6568
=======================
6669

lib/iris/_merge.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ def _defn_msgs(self, other_defn):
388388
diff_attrs = [
389389
repr(key[1])
390390
for key in attrs_1
391-
if np.all(attrs_1[key] != attrs_2[key])
391+
if not np.array_equal(
392+
np.array(attrs_1[key], ndmin=1), np.array(attrs_2[key], ndmin=1)
393+
)
392394
]
393395
diff_attrs = ", ".join(sorted(diff_attrs))
394396
msgs.append(

lib/iris/common/mixin.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Any
1212

1313
import cf_units
14+
import numpy as np
1415

1516
import iris.std_names
1617

@@ -104,11 +105,9 @@ def __eq__(self, other):
104105
match = set(self.keys()) == set(other.keys())
105106
if match:
106107
for key, value in self.items():
107-
match = value == other[key]
108-
try:
109-
match = bool(match)
110-
except ValueError:
111-
match = match.all()
108+
match = np.array_equal(
109+
np.array(value, ndmin=1), np.array(other[key], ndmin=1)
110+
)
112111
if not match:
113112
break
114113
return match

lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@ def test___eq___numpy(self):
4242
right = LimitedAttributeDict(**values)
4343
self.assertEqual(left, right)
4444
self.assertEqual(left, values)
45+
4546
values = dict(one=np.arange(1), two=np.arange(1), three=np.arange(1))
4647
left = LimitedAttributeDict(dict(one=0, two=0, three=0))
4748
right = LimitedAttributeDict(**values)
4849
self.assertEqual(left, right)
4950
self.assertEqual(left, values)
5051

52+
# Test inequality:
53+
values = dict(one=np.arange(1), two=np.arange(2), three=np.arange(3))
54+
left = LimitedAttributeDict(**values)
55+
right = LimitedAttributeDict(
56+
one=np.arange(3), two=np.arange(2), three=np.arange(1)
57+
)
58+
self.assertNotEqual(left, right)
59+
self.assertNotEqual(values, right)
60+
5161
def test___setitem__(self):
5262
for key in self.forbidden_keys:
5363
item = LimitedAttributeDict()

0 commit comments

Comments
 (0)