-
Notifications
You must be signed in to change notification settings - Fork 12
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
Raise pint.DimensionalityError instead of ValueError #144
Comments
Looking at this further this is actually quite tricky. Currently the code will attempt to convert all variables in the xarray object, collect the errors, and then raise a However even if only one error was raised, or all the errors that were raised were of the same type, then currently it will still raise a I tried fixing that by changing def convert_units_dataset(obj, units):
converted = {}
failed = {}
for name, var in obj.variables.items():
unit = units.get(name)
try:
converted[name] = convert_units_variable(var, unit)
except (ValueError, pint.errors.PintTypeError) as e:
failed[name] = e
if failed:
exception_types = [type(e) for e in failed.values()]
if len(set(exception_types)) == 1:
# If all failures were of the same type then raise exception of that type
# Ensures simple cases of invalid unit conversion raise a pint.DimensionalityError
raise exception_types[0](failed)
else:
raise ValueError(failed)
return dataset_from_variables(converted, obj._coord_names, obj.attrs) but that doesn't work because the init signature of Fundamentally trying to combine multiple different errors into one is a bit weird, so what I suggest we do is raise the first error encountered, but with an longer message that can list any later errors found. That should hopefully be the best of both: specific error types for operations on one variable as you would expect, but the user can still see everything they messed up in one try. (And as |
I'm not particularly attached to having it raise Note that exceptions raised by the magnitude will not be caught, so those exceptions can shadow a previously failed |
Fundamentally I just feel quite strongly that errors caused by incorrect units should raise a pint unit-related error. It would be consistent with
Is it not fairly simple to just raise the first error encountered? Especially if we know/expect that error is only going to be one of
True, but "Make a list, raise the first, display any remaining" is logic that is basically the same even for a single error.
Its the actual exception type that I would like to be changed, not the message attached to the ValueError. Also I thought that message already did include the types anyway?
I'm not quite sure I understand what you mean by this. |
I just saw PEP-654, which seems like it would resolve this (and much better than a single exception class could, if I understand correctly): we'd define a Edit: maybe we don't even need the |
That looks cool! If that allows us to raise both types of error then it seems like it could be a good solution. |
There's also PEP 678 which allows attaching additional information to a exception. In our case, that would be the variable name and, in the case of I did investigate using that, but because they are scheduled for What do you think about postponing this feature until there's a bit more support? That would most likely be after the release of |
Totally fine with postponing this! |
It seems like adding notes to exceptions is now pretty well supported:
However it doesn't yet seem to be supported by Perhaps we should we try using them now? Or wait for (I realised after trying to use them in datatree xarray-contrib/datatree#264) |
yep, and there's even a backport package (we just can't use |
If you try to convert a
pint.Quantity
to an incompatible unit you get apint.DimensionalityError
, whereas if you try to convert a quantifiedDataArray
to an incompatible unit you get aValueError
. These should give the same type of error.The text was updated successfully, but these errors were encountered: