-
Notifications
You must be signed in to change notification settings - Fork 87
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: don't use np.asarray
on Index
or Content
objects
#2740
fix: don't use np.asarray
on Index
or Content
objects
#2740
Conversation
Codecov Report
Additional details and impacted files
|
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.
Okay, that makes sense. How did you find all of the instances where __array__
was implicitly being called?
My guess (how I'd go about it): by adding raise Exception("HERE!")
in __array__
and seeing where the tests fail.
Nearly; I removed the support for For index nodes, a manual regex was used. We could also remove |
nplike.asarray
ultimately delegates to the underlying NumPy-like library'sasarray
. For NumPy, this invokesobj.__array__()
. If our object is e.g. anIndex
that wraps a placeholder, this conversion fails because NumPy et al. are not aware of our placeholder type, and eagerly tries to convert it to a buffer.This would also fail during typetracing time, but it may well be that there are branches where this gets skipped.
We could add our own protocol to the
Content
andIndex
types that permitsContent
andIndex
objects to pass throughasarray
, e.g.but on balance I think it might be easier just to pull out
.data
by hand, because most of the time we should know that we have aContent
/Index
object.At the same time, we've long prohibited
np.asarray
on e.g.RegularArray
, and I think we should just do this for all Content nodes to be more thorough. Thus, this PR removesNumpyArray.__array__
, and the same forEmptyArray
et al.I didn't change
Index
, because I suspect that's used everywhere in the wild (in legitimate NumPy-only cases), and the argument forak.Array
being the high-levelnp.asarray
interface does not apply. Thus, library authors must take care not to useasarray
even in NumPy-only contexts iff. there's a chance that the incoming array is a placeholder.So, this PR changes two things:
Content
orIndex
arguments tonplike.asarray
, as they nest the true buffers within themselves.Content.__array__
for any content type, for the reason discussed above.