Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch failed html_repr for cube #3376

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,14 @@ def _repr_html_(self):
from iris.experimental.representation import CubeRepresentation

representer = CubeRepresentation(self)
return representer.repr_html()
# Catch exceptions in the html repr rather than passing them,
# and so fall back to the default cube repr.
try:
result = representer.repr_html()
except (IndexError, ValueError):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original preference was to catch any exceptions, but I see that's bad practice. Is this too broad a range of exceptions, too narrow, or just right?

result = None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn that something's gone wrong? It might be weird to not get an html repr of a cube unexpectedly (my preference is no - Iris is loud enough already).

finally:
return result

def __iter__(self):
raise TypeError("Cube is not iterable")
Expand Down
14 changes: 14 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2411,5 +2411,19 @@ def test_cell_method_correct_order(self):
self.assertTrue(cube1 == cube2)


@tests.skip_data
class Test__repr_html(tests.IrisTest):
def test_bad_repr(self):
# If an html repr fails we don't want it to pass the error.
cube = stock.simple_3d()
with mock.patch(
"iris.experimental.representation.CubeRepresentation.repr_html",
side_effect=ValueError("Bad substring"),
):
# Confirm the exception when we make the html repr is not returned
# by the cube method.
self.assertIsNone(cube._repr_html_())


if __name__ == "__main__":
tests.main()