Skip to content

Commit

Permalink
Raise ValueError for tuple arguments to Tuple instead of NotImplement…
Browse files Browse the repository at this point in the history
…edError

These only work in NumPy as fancy indices. However, there is too much of a
risk of API confusion in thinking that Tuple() works like Tuple((1, 2, 3))
instead of Tuple(1, 2, 3). So we make this an unconditional error with helpful
error messages. Fancy indices should use either a list or array.

c.f. issue Quansight-Labs#17.
  • Loading branch information
asmeurer committed Aug 7, 2020
1 parent 313727b commit 21c288f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ndindex/tests/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
from .helpers import check_same, Tuples, prod, shapes, iterslice, ndindices


def test_tuple_constructor():
# Test things in the Tuple constructor that are not tested by the other
# tests below.
raises(ValueError, lambda: Tuple((1, 2, 3)))
raises(ValueError, lambda: Tuple(0, (1, 2, 3)))

def test_tuple_exhaustive():
# Exhaustive tests here have to be very limited because of combinatorial
# explosion.
Expand Down
4 changes: 3 additions & 1 deletion ndindex/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def _typecheck(self, *args):
for arg in args:
newarg = ndindex(arg)
if isinstance(newarg, Tuple):
raise NotImplementedError("tuples of tuples are not yet supported")
if len(args) == 1:
raise ValueError("tuples inside of tuple indices are not supported. Did you mean to call Tuple(*args) instead of Tuple(args)?")
raise ValueError("tuples inside of tuple indices are not supported. If you meant to use a fancy index, use a list or array instead.")
newargs.append(newarg)

if newargs.count(ellipsis()) > 1:
Expand Down

0 comments on commit 21c288f

Please sign in to comment.