Skip to content

Commit

Permalink
Merge pull request #5517 from esc/typed-list/extend_refine_tuple
Browse files Browse the repository at this point in the history
Fix numba.typed.List extend for singleton and empty iterable
  • Loading branch information
sklam committed Apr 23, 2020
2 parents 82c7d37 + d8119b6 commit 1dabbf6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 27 additions & 0 deletions numba/tests/test_typedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,33 @@ def impl():
got = impl()
self.assertEqual(expected, got)

def test_extend_single_value_container(self):
@njit
def impl():
l = List()
l.extend((100,))
return l

expected = impl.py_func()
got = impl()
self.assertEqual(expected, got)

def test_extend_empty_unrefined(self):
# Extending an unrefined list with an empty iterable doesn't work in a
# jit compiled function as the list remains untyped.
l = List()
l.extend(tuple())
self.assertEqual(len(l), 0)
self.assertFalse(l._typed)

def test_extend_empty_refiend(self):
# Extending a refined list with an empty iterable doesn't work in a
# jit compiled function as the (empty) argument can't be typed
l = List((1,))
l.extend(tuple())
self.assertEqual(len(l), 1)
self.assertTrue(l._typed)


@njit
def cmp(a, b):
Expand Down
5 changes: 3 additions & 2 deletions numba/typed/typedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,14 @@ def pop(self, i=-1):
return _pop(self, i)

def extend(self, iterable):
# Empty iterable, do nothing
if len(iterable) == 0:
return self
if not self._typed:
# Need to get the first element of the iterable to initialise the
# type of the list. FIXME: this may be a problem if the iterable
# can not be sliced.
self._initialise_list(iterable[0])
self.append(iterable[0])
return _extend(self, iterable[1:])
return _extend(self, iterable)

def remove(self, item):
Expand Down

0 comments on commit 1dabbf6

Please sign in to comment.