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

fix UnitOperationError exception when the exponent is unyt_array or u… #131

Merged
merged 1 commit into from
Jan 27, 2020
Merged

fix UnitOperationError exception when the exponent is unyt_array or u… #131

merged 1 commit into from
Jan 27, 2020

Conversation

l-johnston
Copy link
Contributor

…nyt_quantity type

Raising a value to a unyt_array type causes an AttributeError in the str() of UnitOperationError since unit2 is a unyt_array and not a Unit.

>>> from unyt import m
>>> 10**(1*m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/d/Python/unyt/unyt/unyt/array.py", line 1669, in __array_ufunc__
    raise UnitOperationError(ufunc, u0, u1)
unyt.exceptions.UnitOperationError: <exception str() failed>

Comment on lines +501 to +506
try:
np.power(cm_arr, unyt_quantity(3, "g"))
except UnitOperationError as err:
assert isinstance(err.unit1, Unit)
assert isinstance(err.unit2, Unit)

Copy link
Member

Choose a reason for hiding this comment

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

Good catch on the bug!

Assuming my reading of the pytest docs is correct, I'm pretty sure this can be phrased like this:

with pytest.raises(UnitOperationError) as excinfo:
    assert type(excinfo.value.unit1) is Unit
    assert type(excinfo.value.unit2) is Unit

However I think the way you have it is actually simpler so I'm going to merge it as-is.

@ngoldbaum ngoldbaum merged commit 890fe89 into yt-project:master Jan 27, 2020
@l-johnston l-johnston deleted the fix_unitoperationerror_exception branch January 28, 2020 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants