8000 DOC, MAINT: Enforce np.ndarray arg for np.put and np.place by gfyoung · Pull Request #7000 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC, MAINT: Enforce np.ndarray arg for np.put and np.place #7000

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

Merged
merged 1 commit into from
Jan 15, 2016
Merged
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 numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,13 @@ def put(a, ind, v, mode='raise'):
array([ 0, 1, 2, 3, -5])

"""
return a.put(ind, v, mode)
try:
put = a.put
except AttributeError:
raise TypeError("argument 1 must be numpy.ndarray, "
"not {name}".format(name=type(a).__name__))

return put(ind, v, mode)


def swapaxes(a, axis1, axis2):
Expand Down
17 changes: 17 additions & 0 deletions numpy/core/tests/test_fromnumeric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import division, absolute_import, print_function

from numpy import put
from numpy.testing import TestCase, assert_raises


class TestPut(TestCase):

def test_bad_array(self):
# We want to raise a TypeError in the
# case that a non-ndarray object is passed
# in since `np.put` modifies in place and
# hence would do nothing to a non-ndarray
v = 5
indx = [0, 2]
bad_array = [1, 2, 3]
assert_raises(TypeError, put, bad_array, indx, v)
6 changes: 5 additions & 1 deletion numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ def place(arr, mask, vals):

Parameters
----------
arr : array_like
arr : ndarray
Array to put data into.
mask : array_like
Boolean mask array. Must have the same size as `a`.
Expand All @@ -1801,6 +1801,10 @@ def place(arr, mask, vals):
[44, 55, 44]])

"""
if not isinstance(arr, np.ndarray):
Copy link
Member

Choose a reason for hiding this comment

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

Could we use the same duck-typing solution as above, with the try/except clause?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It feels little more awkward in this case because _insert is not an ndarray method. I suppose you could do something like the following:

try:
   put = arr.put
except AttributeError:
   raise TypeError(...)
return _insert(arr, mask, vals)

However, it seems like more contrived compared to the others since we're relying on a method that has nothing to do with what is being called.

Copy link
Member

Choose a reason for hiding this comment

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

What error message do you get when place is not valid? What sort of duck typing does it currently support, if any?

It might be worth updating _insert instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shoyer : See #7003, which does a complete revamp of arr_insert (that is tied to _insert) and does the type checking. In the case of np.place, this PR should serve as a stopgap measure until #7003 can be merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shoyer : Sorry, I didn't answer the remainder of your question; I only had seen the "It might be worth" part on my phone when I was reading it. place doesn't actually do any duck-typing if you pass in a non-ndarray to it. Rather, it does nothing, which is why I added that isinstance check to the code. However, #7003 will embed that check into the C code, which is preferable. However, I wouldn't mind merging the isinstance check into the codebase FTTB.

Copy link
Member

Choose a reason for hiding this comment

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

OK, agreed!

raise TypeError("argument 1 must be numpy.ndarray, "
"not {name}".format(name=type(arr).__name__))

return _insert(arr, mask, vals)


Expand Down
4 changes: 4 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ def test_basic(self):
assert_array_equal(b, [3, 2, 2, 3, 3])

def test_place(self):
# Make sure that non-np.ndarray objects
# raise an error instead of doing nothing
assert_raises(TypeError, place, [1, 2, 3], [True, False], [0, 1])

a = np.array([1, 4, 3, 2, 5, 8, 7])
place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6])
assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7])
Expand Down
4 changes: 0 additions & 4 deletions numpy/lib/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ def test_poly_eq(self, level=rlevel):
assert_(x != y)
assert_(x == x)

def test_mem_insert(self, level=rlevel):
# Ticket #572
np.lib.place(1, 1, 1)

def test_polyfit_build(self):
# Ticket #628
ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01,
Expand Down
0