numba icon indicating copy to clipboard operation
numba copied to clipboard

Problem with structured array with bool element

Open EltonCN opened this issue 3 months ago • 3 comments

Reporting a bug

  • [x ] I have tried using the latest released version of Numba (most recent is visible in the release notes (https://numba.readthedocs.io/en/stable/release-notes-overview.html).
  • [ x] I have included a self contained code sample to reproduce the problem. i.e. it's possible to run as 'python bug.py'.

Numba can't compile when a parameter is a NumPy structured array with a bool element.

The code:

import numpy as np
import numba

array = np.empty(1, dtype=[("mask", bool,2)])

def test_func(array):
    return array[0]["mask"][0]

print(test_func(array))
print(numba.jit(test_func)(array))

Results in:

False

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 10
      7     return array[0]["mask"][0]
      9 print(test_func(array))
---> 10 print(numba.jit(test_func)(array))

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\core\dispatcher.py:687, in _DispatcherBase.typeof_pyval(self, val)
    681 """
    682 Resolve the Numba type of Python value *val*.
    683 This is called from numba._dispatcher as a fallback if the native code
    684 cannot decide the type.
    685 """
    686 try:
--> 687     tp = typeof(val, Purpose.argument)
    688 except ValueError:
    689     tp = types.pyobject

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\core\typing\typeof.py:33, in typeof(val, purpose)
     31 # Note the behaviour for Purpose.argument must match _typeof.c.
     32 c = _TypeofContext(purpose)
---> 33 ty = typeof_impl(val, c)
     34 if ty is None:
     35     msg = _termcolor.errmsg(
     36         f"Cannot determine Numba type of {type(val)}")

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\core\typing\typeof.py:264, in _typeof_ndarray(val, c)
    262     raise errors.NumbaTypeError(msg)
    263 try:
--> 264     dtype = numpy_support.from_dtype(val.dtype)
    265 except errors.NumbaNotImplementedError:
    266     raise errors.NumbaValueError(f"Unsupported array dtype: {val.dtype}")

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\np\numpy_support.py:117, in from_dtype(dtype)
    115     dtype = np.dtype(dtype)
    116 elif getattr(dtype, "fields", None) is not None:
--> 117     return from_struct_dtype(dtype)
    119 try:
    120     return FROM_DTYPE[dtype]

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\np\numpy_support.py:584, in from_struct_dtype(dtype)
    581 [elemdtype, offset] = info[:2]
    582 title = info[2] if len(info) == 3 else None
--> 584 ty = from_dtype(elemdtype)
    585 infos = {
    586     'type': ty,
    587     'offset': offset,
    588     'title': title,
    589 }
    590 fields.append((name, infos))

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\np\numpy_support.py:135, in from_dtype(dtype)
    133     if char in 'V' and dtype.subdtype is not None:
    134         subtype = from_dtype(dtype.subdtype[0])
--> 135         return types.NestedArray(subtype, dtype.shape)
    137 raise errors.NumbaNotImplementedError(dtype)

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\core\types\abstract.py:67, in _TypeMetaclass.__call__(cls, *args, **kwargs)
     61 def __call__(cls, *args, **kwargs):
     62     """
     63     Instantiate *cls* (a Type subclass, presumably) and intern it.
     64     If an interned instance already exists, it is returned, otherwise
     65     the new instance is returned.
     66     """
---> 67     inst = type.__call__(cls, *args, **kwargs)
     68     return cls._intern(inst)

File c:\Users\eltsu\AppData\Local\Programs\Python\Python312\Lib\site-packages\numba\core\types\npytypes.py:589, in NestedArray.__init__(self, dtype, shape)
    587     shape += dtype.shape
    588     dtype = tmp.dtype
--> 589 assert dtype.bitwidth % 8 == 0, \
    590     "Dtype bitwidth must be a multiple of bytes"
    591 self._shape = shape
    592 name = "nestedarray(%s, %s)" % (dtype, shape)

AttributeError: 'Boolean' object has no attribute 'bitwidth'

EltonCN avatar Sep 17 '25 17:09 EltonCN

Thank you for the report @EltonCN . I'm able to reproduce this issue with numba==0.62.0.

swap357 avatar Sep 18 '25 19:09 swap357

This issue is marked as stale as it has had no activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with any updates and confirm that this issue still needs to be addressed.

github-actions[bot] avatar Oct 19 '25 02:10 github-actions[bot]

PR with an idea for a fix in: https://github.com/numba/numba/pull/10275

gmarkall avatar Oct 20 '25 11:10 gmarkall