8000 TST: Test tuple subclass index and broken sequence index · numpy/numpy@9af5028 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9af5028

Browse files
committed
TST: Test tuple subclass index and broken sequence index
1 parent c85dd45 commit 9af5028

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

numpy/core/tests/test_indexing.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_reverse_strides_and_subspace_bufferinit(self):
193193
c = np.arange(10).reshape(5, 2)[::-1]
194194
a[b, :] = c
195195
assert_equal(a[0], [0, 1])
196-
196+
197197
def test_reversed_strides_result_allocation(self):
198198
# Test a bug when calculating the output strides for a result array
199199
# when the subspace size was 1 (and test other cases as well)
@@ -367,6 +367,37 @@ def test_unaligned(self):
367367
d[b % 2 == 0]
368368
d[b % 2 == 0] = x[::2]
369369

370+
def test_tuple_subclass(self):
371+
arr = np.ones((5, 5))
372+
373+
# A tuple subclass should also be an nd-index
374+
class TupleSubclass(tuple):
375+
pass
376+
index = ([1], [1])
377+
index = TupleSubclass(index)
378+
assert_(arr[index].shape == (1,))
379+
# Unlike the non nd-index:
380+
assert_(arr[index,].shape != (1,))
381+
382+
def test_broken_sequence_not_nd_index(self):
383+
# See gh-5063:
384+
# If we have an object which claims to be a sequence, but fails
385+
# on item getting, this should not be converted to an nd-index (tuple)
386+
# If this object happens to be a valid index otherwise, it should work
387+
# This object here is very dubious and probably bad though:
388+
class SequenceLike(object):
389+
def __index__(self):
390+
return 0
391+
392+
def __len__(self):
393+
return 1
394+
395+
def __getitem__(self, item):
396+
raise IndexError('Not possible')
397+
398+
arr = np.arange(10)
399+
assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])
400+
370401

371402
class TestFieldIndexing(TestCase):
372403
def test_scalar_return_type(self):

0 commit comments

Comments
 (0)
0