Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,13 @@ def add_dim_coord(self, dim_coord, data_dim):

def _add_unique_dim_coord(self, dim_coord, data_dim):
if isinstance(dim_coord, iris.coords.AuxCoord):
raise ValueError('The dim_coord may not be an AuxCoord instance.')
try:
dim_coord = iris.coords.DimCoord.from_coord(dim_coord)
warnings.warn('converting AuxCoord to DimCoord',
stacklevel=2)
except ValueError as e:
raise ValueError('Could not convert coord to DimCoord: '
+ str(e))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What extra information is contained in str(e) (not necessarily a criticism, I'm at a windows machine so I can't check myself)?

If it doesn't add much useful I'd say ditch it and go for a more descriptive static message. On the flip side, if e already contains a descriptive message that is good to use as is then we don't even necessarily have to re-raise it...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ValueError: Could not convert coord to DimCoord: The points array must be strictly monotonic.
Probably worth keeping, do you think? Could do with being changed from a colon after DimCoord thought

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, then I guess what I want to know is will it ever be anything other than that? If not then you could just write that... If it can be one of any number of things then maybe this is OK.


# Convert data_dim to a single integer
if isinstance(data_dim, collections.Container):
Expand Down
13 changes: 10 additions & 3 deletions lib/iris/tests/test_cube.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2013, Met Office
# (C) British Crown Copyright 2010 - 2014, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -81,11 +81,18 @@ def test_no_dim(self):
self.cube.add_dim_coord,
iris.coords.DimCoord(np.arange(2), "latitude"))

def test_adding_aux_coord(self):
coord = iris.coords.AuxCoord(np.arange(2), "latitude")
def test_adding_nonmonotonic_aux_coord(self):
coord = iris.coords.AuxCoord([0, 1, 1, 2], "latitude")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add a new test that exercises the new functionality. Check that a strictly monotonic AuxCoord can be accepted and also check the warning is raised perhaps.

with self.assertRaises(ValueError):
self.cube.add_dim_coord(coord, 0)

def test_adding_aux_coord(self):
try:
coord = iris.coords.AuxCoord(np.arange(2), "latitude")
self.cube.add_dim_coord(coord, 0)
except ValueError as e:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't look right. You don't need the try/except block, if the code you are testing raises an exception then the test will fail anyway.

It'd be nice to check that a warning is issued too. I'm not sure what the current best practice for doing this in Iris is (it can be non-trivial). I think @pelson did something like this recently... I may be wrong though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's how I originally had it but I thought that would be bad practise - i.e. an error is different from a fail. If that's considered acceptable in iris testing then I'll change it back.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should also be checking that the coordinate was added correctly, sorry I forgot to say that before, but it is very important! I didn't explain myself very well, sorry for confusion.

We shouldn't really be testing for errors like this, we should be testing the result of the code. When the code as you called it is functioning correctly no error will be raised, which means you don't need to allow for that in the test. The test should aim to capture changes in behaviour (i.e. the resulting coordinate) rather than an error, so an error shouldn't result in a test fail. If the code somehow gets broken so that this usage becomes an error then we will still be notified, but the test suite will report an error, rather than a fail, and this is OK.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We shouldn't really be testing for errors like this, we should be testing the result of the code.

OK - I think I see what you mean. New test coming up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK - I think I see what you mean

Awesome, because I don't think I explained it well that time either. I should have said before, you already have an explicit test for the error condition, so for any other tests you should assume this condition is not met and your bases are still covered.

self.fail(str(e))


class TestEquality(tests.IrisTest):
def test_not_implmemented(self):
Expand Down