- 
                Notifications
    You must be signed in to change notification settings 
- Fork 297
convert_units: raise exception when unit is unknown #2700
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
Conversation
e17e216    to
    35da692      
    Compare
  
    | Travis is telling me it can't find Cartopy. I'm pretty sure my changes didn't cause this! | 
| 
 Internet pixies... they've been cleared out now. 
 This is really an incompatible change rather than a bugfix, although this may be a matter of opinion. I can imagine someone writing code that relies on this behaviour though. | 
| Thanks @ajdawson, I've now moved the whatsnew to the incompatible changes section. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good start but I think there are a few bits that could be addressed to be improved.
| class Test_convert_units(tests.IrisTest): | ||
| def test_convert_unknown_units(self): | ||
| cube = iris.cube.Cube(1) | ||
| with self.assertRaises(UnitConversionError): | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can you use the assertRaisesRegexp method here, as elsewhere in the Iris tests codebase. See https://github.com/SciTools/iris/blob/master/lib/iris/tests/unit/cube/test_Cube.py#L1309-L1314 for an example of this. The benefit is that we can check that not only the right error is being raised, but the right error in the right place (i.e. the error messages match).
        
          
                docs/iris/src/whatsnew/2.0a0.rst
              
                Outdated
          
        
      | * The :meth:`~iris.cube.Cube.lazy_data` method no longer accepts any arguments. | ||
|  | ||
| * Attempting to use the :meth:`iris.cube.Cube.convert_units` method on a cube | ||
| where the existing unit is unknown will raise a :data:`UnitConversionError` | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The situation (a cube has units unknown) is quite buried in this sentence structure. Perhaps we could rewrite this to:
"Using :meth:iris.cube.Cube.convert_units on a cube with unknown units will result in a :data:dataConversionError being raised."
        
          
                lib/iris/cube.py
              
                Outdated
          
        
      | if self.units.is_unknown(): | ||
| raise iris.exceptions.UnitConversionError( | ||
| 'cannot convert from unknown units to {!s}, ' | ||
| 'set the "units" attribute instead'.format(unit)) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the second line of the error message may confuse rather than help, so perhaps we can drop it. I'd also suggest printing the repr of the unit's name in the error message; that is:
"cannot convert from 'unknown' units to {!r}.".format(unit.origin)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the second line of the error message may confuse rather than help
I disagree, a hint to the correct way to do this is highly valuable. The wording could be changed though. What did you think might confuse @dkillick?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it depends on why the error came up. If a user knew the unit was unknown and deliberately used the method to set it (as you suggested in your first comment @ajdawson) then telling them the right way to do it makes sense. If it came up because the data file was dodgy and the user didn't notice they weren't converting from anything (which has happened to me, and prompted posting the issue to begin with) probably all you need is to flag up that the cube unit is unknown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about something like 'Cannot convert from unknown units. The "cube.units" attribute may be set directly.' With no implication that what you are setting the unit to is the same as what you want to convert it to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK that is a fair point, the second line may be confusing under that circumstance. I can't think of a good catch-all alternative so I'm happy for you to remove as suggested by @dkillick, or if you want to try and re-word then please feel free.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree
Great 👍 I had exactly the same thought process but went for suggesting the easy solution, which was just to remove that section. What I think might confuse is the current wording which I don't think makes clear quite what needs to be done. I'm also not sure how common it is for other error messages in Iris to give a recommendation on how to fix the problem, which was my other, uncommented concern here.
That said I can certainly see the benefit of giving a suggestion in this case. But to be clear that might have to look like the following...
("cannot convert from 'unknown' units to {unit!r}. "
"Consider setting the cube's units instead, with `cube.units = cf_units.Unit({unit!r})`.".format(unit=unit.origin))And maybe at this point we return to the second line confusing rather than helping...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about something like 'Cannot convert from unknown units. The "cube.units" attribute may be set directly.'
@rcomer I like that - it is clear and descriptive without being complicated. If you could change the error message to that it would be great 👍
| Hi @rcomer - thanks for this proposed change, which looks to improve some odd silent behaviour in Iris. You've also chosen a good time to propose this incompatible change just in time for Iris v2.0! I've made some comments on some things that will need changing, which are all minor but I think necessary. I'd also like you to make the same set of changes again but for  | 
| Thanks for the feedback @dkillick. I'm actually on leave for the next two weeks (browsing GitHub from my sofa, as you do). So, assuming there's no major hurry, I'll address it properly when I get back. | 
| OK, I think I have covered everything now. The Travis errors appear to all be timeouts. Do I need to do anything about the CLA? The Met Office already owns all my work... | 
| 
 You don't need to sign the document but you do need to add your name to the  | 
|  | ||
| * The :meth:`~iris.cube.Cube.lazy_data` method no longer accepts any arguments. | ||
|  | ||
| * Using :meth:`~iris.cube.Cube.convert_units` on a cube with unknown units will | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"cube or coordinate" instead of writing the whole sentence out twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but then you wouldn't have the links to the documentation for the methods. I have no strong opinion either way.
d87c17e    to
    40c7e8a      
    Compare
  
    | I did a rebase to pick up #2757, but I think the internet pixies might be playing again:  | 
| 'The "coord.units" attribute may be set directly.') | ||
| with self.assertRaisesRegexp(UnitConversionError, emsg): | ||
| coord.convert_units('degrees') | ||
|  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are probably going to freak out about the PEP8 of your newlines here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have run the pep8 command on my local copy of that file and got no output.
| @dkillick - are you able to go through your review again and confirm the items that are outstanding please? I think there have been a few changes since your last comments, and they may now be addressed. Thanks 😄 | 
| @rcomer thanks for pressing on with this – it's a very beneficial addition 👍 | 
Fix for #1977. If the units on a cube are unknown, the
convert_unitsmethod should raise an exception. I consider the current behaviour of just setting the units to be a bug. If a user has not realised the metadata in the data file is incomplete, then the cube could end up with spurious data values.A couple of things I'm not sure of:
I couldn't figure out if the class definitions in
iris.exceptionswere in any particular order, so just put the new one at the end.Did I put the whatsnew in the right place?