Repository: numba/numba

Issue:
`typed.List` ignores constructor arguments when jitting is disabled
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->
```python
>>> from numba.typed import List
>>> List([1])
ListType[int64]([1])

>>> from numba import config
>>> config.DISABLE_JIT = True
>>> List([1])
[]  # where did the 1 go?
```
`typed.List` ignores constructor arguments when jitting is disabled
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->
```python
>>> from numba.typed import List
>>> List([1])
ListType[int64]([1])

>>> from numba import config
>>> config.DISABLE_JIT = True
>>> List([1])
[]  # where did the 1 go?
```


Discussion:
@eric-wieser thanks for reporting, it's a bug indeed. Probably a good first or second issue for someone, although the `List` construction/initialization is somewhat contrived.
Yeah, I'm very puzzled by the arguments to `typed.List.__new__` which I don't seem to be able to actually pass, and don't seem to include this argument at all.
perhaps it is a matter of adding `*args`?
Maybe worth noting from the [docs for `__new()__`](https://docs.python.org/3/reference/datamodel.html#object.__new__):

> If __new__() is invoked during object construction and it returns an instance or subclass of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor.
>
> If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.


@eric-wieser thanks for reporting, it's a bug indeed. Probably a good first or second issue for someone, although the `List` construction/initialization is somewhat contrived.
Yeah, I'm very puzzled by the arguments to `typed.List.__new__` which I don't seem to be able to actually pass, and don't seem to include this argument at all.
perhaps it is a matter of adding `*args`?
Maybe worth noting from the [docs for `__new()__`](https://docs.python.org/3/reference/datamodel.html#object.__new__):

> If __new__() is invoked during object construction and it returns an instance or subclass of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor.
>
> If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.



Resolving patch:
diff --git a/numba/typed/typedlist.py b/numba/typed/typedlist.py
--- a/numba/typed/typedlist.py
+++ b/numba/typed/typedlist.py
@@ -200,12 +200,15 @@ class List(MutableSequence, pt.Generic[T]):
     _legal_kwargs = ["lsttype", "meminfo", "allocated"]
 
     def __new__(cls,
+                *args,
                 lsttype=None,
                 meminfo=None,
                 allocated=DEFAULT_ALLOCATED,
                 **kwargs):
         if config.DISABLE_JIT:
-            return list.__new__(list)
+            pylist = list.__new__(list)
+            pylist.__init__(*args, **kwargs)
+            return pylist
         else:
             return object.__new__(cls)
 


Test patch:
diff --git a/numba/tests/test_listobject.py b/numba/tests/test_listobject.py
--- a/numba/tests/test_listobject.py
+++ b/numba/tests/test_listobject.py
@@ -20,7 +20,7 @@
 from numba.core.errors import TypingError
 from numba.tests.support import (TestCase, MemoryLeakMixin, override_config,
                                  forbid_codegen)
-from numba.typed import listobject
+from numba.typed import listobject, List
 
 
 class TestCreateAppendLength(MemoryLeakMixin, TestCase):
@@ -43,6 +43,14 @@ def test_list_create_no_jit(self):
                 l = listobject.new_list(int32)
                 self.assertEqual(type(l), list)
 
+    def test_nonempty_list_create_no_jit(self):
+        # See Issue #6001: https://github.com/numba/numba/issues/6001
+        with override_config('DISABLE_JIT', True):
+            with forbid_codegen():
+                l = List([1, 2, 3])
+                self.assertEqual(type(l), list)
+                self.assertEqual(l, [1, 2, 3])
+
 
 class TestBool(MemoryLeakMixin, TestCase):
     """Test list bool."""