Is there a way to type the signature of readonly arrays? I did not find a way to specify readonly on the documentation page or StackOverflow (or maybe specifying array should work on readonly arrays as well?).
In [1]: import numba
In [2]: arr = np.array([1])
In [3]: arr.setflags(write=False)
In [4]: @numba.jit((numba.int64[:],))
...: def f(x):
...: return x
...:
In [5]: f(arr)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-0e64fe0f2f72> in <module>
----> 1 f(arr)
/lib/python3.7/site-packages/numba/dispatcher.py in _explain_matching_error(self, *args, **kws)
498 msg = ("No matching definition for argument type(s) %s"
499 % ', '.join(map(str, args)))
--> 500 raise TypeError(msg)
501
502 def _search_new_conversions(self, *args, **kws):
TypeError: No matching definition for argument type(s) readonly array(int64, 1d, C)
In [6]: numba.__version__
Out[6]: '0.45.1'
Thanks for the report. Unfortunately there's no short cut for specifying an array that is read only, the full numba.types API calls have to be used. Here's an example:
In [1]: from numba import njit, types
In [2]: import numpy as np
In [3]: arr = np.array([1])
In [4]: arr.setflags(write=False)
In [5]: arr.flags
Out[5]:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : False
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
In [6]: @njit((types.Array(types.int64, 1, 'C', readonly=True),)) # this is an int64, 1D, C order, read only array
...: def f(x):
...: return x
...:
In [7]: f(arr)
Out[7]: array([1])
In [8]: f.signatures
Out[8]: [(readonly array(int64, 1d, C),)]
hope this helps.
@mroeschke I will close this issue now as I believe this issue has been resolved. Thanks again for asking about this.
Great, thanks for your response!
Most helpful comment
Thanks for the report. Unfortunately there's no short cut for specifying an array that is read only, the full
numba.typesAPI calls have to be used. Here's an example:hope this helps.