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

__array_wrap__ broken for insert, etc. #682

Closed
dopplershift opened this issue Aug 26, 2018 · 2 comments · Fixed by #905
Closed

__array_wrap__ broken for insert, etc. #682

dopplershift opened this issue Aug 26, 2018 · 2 comments · Fixed by #905
Labels
numpy Numpy related bug/enhancement

Comments

@dopplershift
Copy link
Contributor

The following code:

import numpy as np
import pint
ureg = pint.UnitRegistry()
a = np.arange(3) * ureg.meter
np.insert(a, 2, 4 * ureg.meter)

produces:

~/miniconda3/envs/py36/lib/python3.6/site-packages/numpy/lib/function_base.py in insert(arr, obj, values, axis)
   4439         new[tuple(slobj)] = arr[tuple(slobj2)]
   4440         if wrap:
-> 4441             return wrap(new)
   4442         return new
   4443     elif indices.size == 0 and not isinstance(obj, np.ndarray):

~/miniconda3/envs/py36/lib/python3.6/site-packages/pint/quantity.py in __array_wrap__(self, obj, context)
   1342 
   1343     def __array_wrap__(self, obj, context=None):
-> 1344         uf, objs, i_out = context
   1345 
   1346         # if this ufunc is not handled by Pint, pass it to the magnitude.

TypeError: 'NoneType' object is not iterable

I have no idea if insert has any hope of working right now, but that exception comes from code that is obviously wrong:

pint/pint/quantity.py

Lines 1428 to 1430 in 241bf7d

def __array_wrap__(self, obj, context=None):
uf, objs, i_out = context

Here context defaults to None (as is documented for numpy), but the first line in __array_wrap__ tries to unpack it unconditionally. I have no idea what the right behavior is in this case.

@hgrecco
Copy link
Owner

hgrecco commented Dec 3, 2019

Revisit after #905

@hgrecco hgrecco added the numpy Numpy related bug/enhancement label Dec 3, 2019
@jthielen
Copy link
Contributor

jthielen commented Dec 4, 2019

Based on https://docs.scipy.org/doc/numpy-1.17.0/reference/arrays.classes.html#numpy.class.__array_ufunc__, it looks like __array_wrap__ is disabled since __array_ufunc__ is defined (thus why __array_wrap__ has been removed in #905), so I don't think this problematic code is reached even now?

np.insert would be a good __array_function__ implementation to add either in #905 or a follow-up.

@bors bors bot closed this as completed in 43fbae2 Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numpy Numpy related bug/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants