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

Prevent crash when context is None #420

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
8 changes: 7 additions & 1 deletion pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,9 @@ def tolist(self):
__array_priority__ = 17

def __array_prepare__(self, obj, context=None):
# If this uf is handled by Pint, write it down in the handling dictionary.
# If we have no context, assume this is unhandled
if context is None:
return obj

# name of the ufunc, argument of the ufunc, domain of the ufunc
# In ufuncs with multiple outputs, domain indicates which output
Expand All @@ -1244,6 +1246,10 @@ def __array_prepare__(self, obj, context=None):
return obj

def __array_wrap__(self, obj, context=None):
# If we have no context, assume this is unhandled
if context is None:
return self.magnitude.__array_wrap__(obj, context)

uf, objs, huh = context

# if this ufunc is not handled by Pint, pass it to the magnitude.
Expand Down
23 changes: 19 additions & 4 deletions pint/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,8 @@ def test_right_shift(self):
'same')


class TestNDArrayQunatityMath(QuantityTestCase):

@helpers.requires_numpy()
@helpers.requires_numpy()
class TestNDArrayQuantityMath(QuantityTestCase):
def test_exponentiation_array_exp(self):
arr = np.array(range(3), dtype=np.float)
q = self.Q_(arr, None)
Expand All @@ -441,7 +440,6 @@ def test_exponentiation_array_exp(self):
self.assertRaises(DimensionalityError, op_, q_cp, q2_cp)

@unittest.expectedFailure
@helpers.requires_numpy()
def test_exponentiation_array_exp_2(self):
arr = np.array(range(3), dtype=np.float)
#q = self.Q_(copy.copy(arr), None)
Expand All @@ -456,3 +454,20 @@ def test_exponentiation_array_exp_2(self):
arr_cp = copy.copy(arr)
q_cp = copy.copy(q)
self.assertRaises(DimensionalityError, op.ipow, arr_cp, q_cp)

def test_maximum(self):
a = np.arange(4) * self.ureg.m
b = 2 * np.ones(4) * self.ureg.m
# this line would previously throw an error
x = np.maximum(a, b)
# TODO
# self.assertEqual(x.units, ureg.m * ureg.s)

def test_linalg_solve(self):
ureg = self.ureg
A = np.eye(2) * ureg.s
b = np.ones(2) * ureg.m
# this line would previously throw an error
x = np.linalg.solve(A, b)
# TODO
# self.assertEqual(x.units, ureg.m * ureg.s)