Skip to content

Commit

Permalink
[bug] Support indexing via np.integer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailing Zhang committed Aug 10, 2022
1 parent ca5bf7d commit 7ed0a02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/taichi/lang/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ def __getitem__(self, key):
# Check for potential slicing behaviour
# for instance: x[0, :]
padded_key = self._pad_key(key)
import numpy as np # pylint: disable=C0415
for key in padded_key:
if not isinstance(key, int):
if not isinstance(key, (int, np.integer)):
raise TypeError(
f"Detected illegal element of type: {type(key)}. "
f"Please be aware that slicing a ti.field is not supported so far."
Expand Down
8 changes: 8 additions & 0 deletions tests/python/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
To test our new `ti.field` API is functional (#1500)
'''

import numpy as np
import pytest
from taichi.lang import impl
from taichi.lang.misc import get_host_arch_list
Expand Down Expand Up @@ -282,6 +283,13 @@ def test_invalid_slicing():
val[0, :]


@test_utils.test()
def test_slicing_with_np_int():
val = ti.field(ti.i32, shape=(2))
idx = np.int32(0)
val[idx]


@test_utils.test(exclude=[ti.cc], debug=True)
def test_field_fill():
x = ti.field(int, shape=(3, 3))
Expand Down

0 comments on commit 7ed0a02

Please sign in to comment.