Repository: numba/numba

Issue:
typed.List's empty_list function cannot accept allocated parameter in nopython mode 
<!--

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).

For more general "how do I do X?" type questions, please speak to us in real
time on https://gitter.im/numba/numba or post to the Numba mailing list
https://groups.google.com/a/continuum.io/forum/#!forum/numba-users.

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I am 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).

Hi, 

It seems the overload for 'empty_list' function has no allocated parameter, hence it's not possible to use pre-allocated typed.Lists in nopython mode, i.e. with the following reproducer compilation fails in nopython mode and passes in obj mode:

```
    def test_numba_typed_list_allocated(self):
        import numba
        from numba import types
        from numba.typed import Dict, List
        list_type1 = types.ListType(types.int64)

        def test_impl():
            # allocated doesn't compile in nopython mode, but works in objmode
            # problem in overload of empty_list in typedlist.py
            alloc_list = List.empty_list(types.int64, 10)
            return len(alloc_list)
        jitted = numba.njit(test_impl)

        print(jitted())
```

The error:
```
======================================================================
ERROR: test_numba_typed_list_allocated (sdc.tests.test_series.TestSeries)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\akozlov\AppData\Local\Continuum\anaconda3\hpat\sdc\tests\test_series.py
", line 6817, in test_numba_typed_list_allocated
    print(jitted())
  File "c:\users\akozlov\appdata\local\continuum\anaconda3\numba_master\numba\numba\disp
atcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "c:\users\akozlov\appdata\local\continuum\anaconda3\numba_master\numba\numba\disp
atcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "c:\users\akozlov\appdata\local\continuum\anaconda3\numba_master\numba\numba\six.
py", line 668, in reraise
    raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function typedlist_empty at 0x0000015A9C481550>) with argument(
s) of type(s): (typeref[<class 'numba.types.containers.ListType'>], class(int64), int64)

 * parameterized
In definition 0:
    TypeError: typedlist_empty() takes 2 positional arguments but 3 were given
    raised from c:\users\akozlov\appdata\local\continuum\anaconda3\numba_master\numba\nu
mba\typing\templates.py:539
In definition 1:
    TypeError: typedlist_empty() takes 2 positional arguments but 3 were given
    raised from c:\users\akozlov\appdata\local\continuum\anaconda3\numba_master\numba\nu
mba\typing\templates.py:539
This error is usually caused by passing an argument of a type that is unsupported by the
 named function.
[1] During: resolving callee type: BoundFunction((<class 'numba.types.abstract.TypeRef'>
, 'empty_list') for typeref[<class 'numba.types.containers.ListType'>])
[2] During: typing of call at C:\Users\akozlov\AppData\Local\Continuum\anaconda3\hpat\sd
c\tests\test_series.py (6813)


File "sdc\tests\test_series.py", line 6813:
        def test_impl():
            <source elided>
            # problem in overload of empty_list in typedlist.py
            alloc_list = List.empty_list(types.int64, 10)
            ^


----------------------------------------------------------------------
Ran 1 test in 0.075s

FAILED (errors=1)
```

Numba version:
# Name                    Version                   Build  Channel
numba                     0.48.0                    dev_0    <develop>

Best Regards,
Alexey.

Discussion:

Patch:
diff --git a/numba/core/codegen.py b/numba/core/codegen.py
index 1a6472b62..41721ea21 100644
--- a/numba/core/codegen.py
+++ b/numba/core/codegen.py
@@ -34,10 +34,9 @@ def dump(header, body):
             from pygments import highlight
             from pygments.lexers import GasLexer as lexer
             from pygments.formatters import Terminal256Formatter
-            from numba.misc.dump_style import by_colorscheme
             def printer(arg):
                 print(highlight(arg, lexer(),
-                      Terminal256Formatter(style=by_colorscheme())))
+                      Terminal256Formatter(style='solarized-light')))
     else:
         printer = print
     print('=' * 80)
diff --git a/numba/core/ir.py b/numba/core/ir.py
index a2115b34c..21ac931bf 100644
--- a/numba/core/ir.py
+++ b/numba/core/ir.py
@@ -1462,11 +1462,9 @@ class FunctionIR(object):
                     raise ValueError(msg)
                 else:
                     from pygments import highlight
-                    from numba.misc.dump_style import NumbaIRLexer as lexer
-                    from numba.misc.dump_style import by_colorscheme
+                    from pygments.lexers import DelphiLexer as lexer
                     from pygments.formatters import Terminal256Formatter
-                    print(highlight(text, lexer(), Terminal256Formatter(
-                        style=by_colorscheme())))
+                    print(highlight(text, lexer(), Terminal256Formatter()))
             else:
                 print(text)
 
diff --git a/numba/core/lowering.py b/numba/core/lowering.py
index 3e5c9bde2..e3cd2d4eb 100644
--- a/numba/core/lowering.py
+++ b/numba/core/lowering.py
@@ -192,10 +192,9 @@ class BaseLower(object):
                     from pygments import highlight
                     from pygments.lexers import LlvmLexer as lexer
                     from pygments.formatters import Terminal256Formatter
-                    from numba.misc.dump_style import by_colorscheme
                     print(highlight(self.module.__repr__(), lexer(),
                                     Terminal256Formatter(
-                                        style=by_colorscheme())))
+                                        style='solarized-light')))
                 except ImportError:
                     msg = "Please install pygments to see highlighted dumps"
                     raise ValueError(msg)
diff --git a/numba/misc/dump_style.py b/numba/misc/dump_style.py
deleted file mode 100644
index 95b5ed5be..000000000
--- a/numba/misc/dump_style.py
+++ /dev/null
@@ -1,84 +0,0 @@
-try:
-    from pygments.styles.default import DefaultStyle
-except ImportError:
-    msg = "Please install pygments to see highlighted dumps"
-    raise ImportError(msg)
-
-import numba.core.config
-from pygments.styles.manni import ManniStyle
-from pygments.styles.monokai import MonokaiStyle
-from pygments.styles.native import NativeStyle
-
-from pygments.lexer import RegexLexer, include, bygroups, words
-from pygments.token import Text, Name, String,  Punctuation, Keyword, \
-    Operator, Number
-
-from pygments.style import Style
-
-
-class NumbaIRLexer(RegexLexer):
-    """
-    Pygments style lexer for Numba IR (for use with highlighting etc).
-    """
-    name = 'Numba_IR'
-    aliases = ['numba_ir']
-    filenames = ['*.numba_ir']
-
-    identifier = r'\$[a-zA-Z0-9._]+'
-    fun_or_var = r'([a-zA-Z_]+[a-zA-Z0-9]*)'
-
-    tokens = {
-        'root' : [
-            (r'(label)(\ [0-9]+)(:)$',
-                bygroups(Keyword, Name.Label, Punctuation)),
-
-            (r' = ', Operator),
-            include('whitespace'),
-            include('keyword'),
-
-            (identifier, Name.Variable),
-            (fun_or_var + r'(\()',
-                bygroups(Name.Function, Punctuation)),
-            (fun_or_var + r'(\=)',
-                bygroups(Name.Attribute, Punctuation)),
-            (fun_or_var, Name.Constant),
-            (r'[0-9]+', Number),
-
-            # <built-in function some>
-            (r'<[^>\n]*>', String),
-
-            (r'[=<>{}\[\]()*.,!\':]|x\b', Punctuation)
-        ],
-
-        'keyword':[
-            (words((
-                'del', 'jump', 'call', 'branch',
-            ), suffix=' '), Keyword),
-        ],
-
-        'whitespace': [
-            (r'(\n|\s)', Text),
-        ],
-    }
-
-
-def by_colorscheme():
-    """
-    Get appropriate style for highlighting according to
-    NUMBA_COLOR_SCHEME setting
-    """
-    styles = DefaultStyle.styles.copy()
-    styles.update({
-        Name.Variable:        "#888888",
-    })
-    custom_default = type('CustomDefaultStyle', (Style, ), {'styles': styles})
-
-    style_map = {
-        'no_color' : custom_default,
-        'dark_bg' : MonokaiStyle,
-        'light_bg' : ManniStyle,
-        'blue_bg' : NativeStyle,
-        'jupyter_nb' : DefaultStyle,
-    }
-
-    return style_map[numba.core.config.COLOR_SCHEME]
diff --git a/numba/typed/listobject.py b/numba/typed/listobject.py
index 379f12aba..df6987fef 100644
--- a/numba/typed/listobject.py
+++ b/numba/typed/listobject.py
@@ -50,6 +50,8 @@ INDEXTY = types.intp
 
 index_types = types.integer_domain
 
+DEFAULT_ALLOCATED = 0
+
 
 @register_model(ListType)
 class ListModel(models.StructModel):
@@ -258,7 +260,7 @@ def _imp_dtor(context, module):
     return fn
 
 
-def new_list(item, allocated=0):
+def new_list(item, allocated=DEFAULT_ALLOCATED):
     """Construct a new list. (Not implemented in the interpreter yet)
 
     Parameters
@@ -369,7 +371,7 @@ def _list_new(typingctx, itemty, allocated):
 
 
 @overload(new_list)
-def impl_new_list(item, allocated=0):
+def impl_new_list(item, allocated=DEFAULT_ALLOCATED):
     """Creates a new list.
 
     Parameters
@@ -385,7 +387,7 @@ def impl_new_list(item, allocated=0):
 
     itemty = item
 
-    def imp(item, allocated=0):
+    def imp(item, allocated=DEFAULT_ALLOCATED):
         if allocated < 0:
             raise RuntimeError("expecting *allocated* to be >= 0")
         lp = _list_new(itemty, allocated)
diff --git a/numba/typed/typedlist.py b/numba/typed/typedlist.py
index 0b0f73d6d..6567edff3 100644
--- a/numba/typed/typedlist.py
+++ b/numba/typed/typedlist.py
@@ -25,9 +25,11 @@ from numba.core.extending import (
 )
 from numba.typed import listobject
 
+DEFAULT_ALLOCATED = listobject.DEFAULT_ALLOCATED
+
 
 @njit
-def _make_list(itemty, allocated=0):
+def _make_list(itemty, allocated=DEFAULT_ALLOCATED):
     return listobject._as_meminfo(listobject.new_list(itemty,
                                                       allocated=allocated))
 
@@ -172,14 +174,14 @@ class List(MutableSequence):
     Implements the MutableSequence interface.
     """
 
-    def __new__(cls, lsttype=None, meminfo=None, allocated=None):
+    def __new__(cls, lsttype=None, meminfo=None, allocated=DEFAULT_ALLOCATED):
         if config.DISABLE_JIT:
             return list.__new__(list)
         else:
             return object.__new__(cls)
 
     @classmethod
-    def empty_list(cls, item_type, allocated=0):
+    def empty_list(cls, item_type, allocated=DEFAULT_ALLOCATED):
         """Create a new empty List.
 
         Parameters
@@ -213,7 +215,7 @@ class List(MutableSequence):
         else:
             self._list_type = None
 
-    def _parse_arg(self, lsttype, meminfo=None, allocated=0):
+    def _parse_arg(self, lsttype, meminfo=None, allocated=DEFAULT_ALLOCATED):
         if not isinstance(lsttype, ListType):
             raise TypeError('*lsttype* must be a ListType')
 
@@ -247,7 +249,7 @@ class List(MutableSequence):
 
     def _allocated(self):
         if not self._typed:
-            return 0
+            return DEFAULT_ALLOCATED
         else:
             return _allocated(self)
 
@@ -364,12 +366,12 @@ class List(MutableSequence):
 
 # XXX: should we have a better way to classmethod
 @overload_method(TypeRef, 'empty_list')
-def typedlist_empty(cls, item_type):
+def typedlist_empty(cls, item_type, allocated=DEFAULT_ALLOCATED):
     if cls.instance_type is not ListType:
         return
 
-    def impl(cls, item_type):
-        return listobject.new_list(item_type)
+    def impl(cls, item_type, allocated=DEFAULT_ALLOCATED):
+        return listobject.new_list(item_type, allocated=allocated)
 
     return impl
 


Test patch:
diff --git a/numba/tests/test_typedlist.py b/numba/tests/test_typedlist.py
index 6ab938601..3d61cdeef 100644
--- a/numba/tests/test_typedlist.py
+++ b/numba/tests/test_typedlist.py
@@ -616,6 +616,25 @@ class TestAllocation(MemoryLeakMixin, TestCase):
             tl = List.empty_list(types.int32, i)
             self.assertEqual(tl._allocated(), i)
 
+    def test_allocation_njit(self):
+        # kwarg version
+        @njit
+        def foo(i):
+            tl = List.empty_list(types.int32, allocated=i)
+            return tl._allocated()
+
+        for j in range(16):
+            self.assertEqual(foo(j), j)
+
+        # posarg version
+        @njit
+        def foo(i):
+            tl = List.empty_list(types.int32, i)
+            return tl._allocated()
+
+        for j in range(16):
+            self.assertEqual(foo(j), j)
+
     def test_growth_and_shrinkage(self):
         tl = List.empty_list(types.int32)
         growth_before = {0: 0, 4:4, 8:8, 16:16}