Skip to content

Commit 8499fe0

Browse files
Improve error messages when comparing against objects vs strings (#4928)
* improve error messages * whitespace fix * edit/fix error messages * add whatsnew entry * Apply suggestions from code review Co-authored-by: Ruth Comer <[email protected]> * add test Co-authored-by: Ruth Comer <[email protected]>
1 parent b5178a9 commit 8499fe0

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ This document explains the changes made to Iris for this release
7979
#. `@rcomer`_ removed some old redundant code that prevented determining the
8080
order of time cells. (:issue:`4697`, :pull:`4729`)
8181

82+
#. `@stephenworsley`_ improved the accuracy of the error messages for
83+
:meth:`~iris.cube.Cube.coord` when failing to find coordinates in the case where
84+
a coordinate is given as the argument. Similarly, improved the error messages for
85+
:meth:`~iris.cube.Cube.cell_measure` and :meth:`~iris.cube.Cube.ancillary_variable`.
86+
(:issue:`4898`, :pull:`4928`)
87+
8288

8389
💣 Incompatible Changes
8490
=======================

lib/iris/cube.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,12 @@ def coord(
19901990
if name_or_coord is not None:
19911991
if not isinstance(name_or_coord, str):
19921992
_name = name_or_coord.name()
1993+
emsg = (
1994+
"Expected to find exactly 1 coordinate matching the given "
1995+
f"{_name!r} coordinate's metadata, but found none."
1996+
)
1997+
raise iris.exceptions.CoordinateNotFoundError(emsg)
1998+
19931999
bad_name = _name or standard_name or long_name or ""
19942000
emsg = (
19952001
f"Expected to find exactly 1 {bad_name!r} coordinate, "
@@ -2194,9 +2200,15 @@ def cell_measure(self, name_or_cell_measure=None):
21942200
bad_name = (
21952201
name_or_cell_measure and name_or_cell_measure.name()
21962202
) or ""
2203+
if name_or_cell_measure is not None:
2204+
emsg = (
2205+
"Expected to find exactly 1 cell measure matching the given "
2206+
f"{bad_name!r} cell measure's metadata, but found none."
2207+
)
2208+
raise iris.exceptions.CellMeasureNotFoundError(emsg)
21972209
msg = (
2198-
"Expected to find exactly 1 %s cell_measure, but found "
2199-
"none." % bad_name
2210+
f"Expected to find exactly 1 {bad_name!r} cell measure, "
2211+
"but found none."
22002212
)
22012213
raise iris.exceptions.CellMeasureNotFoundError(msg)
22022214

@@ -2281,9 +2293,16 @@ def ancillary_variable(self, name_or_ancillary_variable=None):
22812293
name_or_ancillary_variable
22822294
and name_or_ancillary_variable.name()
22832295
) or ""
2296+
if name_or_ancillary_variable is not None:
2297+
emsg = (
2298+
"Expected to find exactly 1 ancillary_variable matching the "
2299+
f"given {bad_name!r} ancillary_variable's metadata, but found "
2300+
"none."
2301+
)
2302+
raise iris.exceptions.AncillaryVariableNotFoundError(emsg)
22842303
msg = (
2285-
"Expected to find exactly 1 {!s} ancillary_variable, but "
2286-
"found none.".format(bad_name)
2304+
f"Expected to find exactly 1 {bad_name!r} ancillary_variable, "
2305+
"but found none."
22872306
)
22882307
raise iris.exceptions.AncillaryVariableNotFoundError(msg)
22892308

lib/iris/tests/unit/cube/test_Cube.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,25 @@ def test_fail_remove_ancilliary_variable_by_name(self):
25282528
self.cube.remove_ancillary_variable("notname")
25292529

25302530

2531+
class TestCoords(tests.IrisTest):
2532+
def setUp(self):
2533+
cube = Cube(np.arange(6).reshape(2, 3))
2534+
x_coord = DimCoord(points=np.array([2, 3, 4]), long_name="x")
2535+
cube.add_dim_coord(x_coord, 1)
2536+
self.x_coord = x_coord
2537+
self.cube = cube
2538+
2539+
def test_bad_coord(self):
2540+
bad_coord = self.x_coord.copy()
2541+
bad_coord.attributes = {"bad": "attribute"}
2542+
re = (
2543+
"Expected to find exactly 1 coordinate matching the given "
2544+
"'x' coordinate's metadata, but found none."
2545+
)
2546+
with self.assertRaisesRegex(CoordinateNotFoundError, re):
2547+
_ = self.cube.coord(bad_coord)
2548+
2549+
25312550
class Test__getitem_CellMeasure(tests.IrisTest):
25322551
def setUp(self):
25332552
cube = Cube(np.arange(6).reshape(2, 3))

0 commit comments

Comments
 (0)