Skip to content

Add a proper exception handling for nested lists in dpnp.repeat #2972

Description

@antonwolfy
import dpnp as np

np.__version__
# Out: '0.21.0dev1+27.gfdb1c53d735.dirty'

x = np.array([3])
np.repeat(x, [[4]])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 np.repeat(x, [[4]])

File ~/code/dpnp/dpnp/dpnp_iface_manipulation.py:2841, in repeat(a, repeats, axis)
   2838     a = dpnp.ravel(a)
   2840 usm_arr = dpnp.get_usm_ndarray(a)
-> 2841 usm_res = dpt.repeat(usm_arr, repeats, axis=axis)
   2842 return dpnp_array._create_from_usm_ndarray(usm_res)

File ~/code/dpnp/dpnp/tensor/_manipulation_functions.py:631, in repeat(x, repeats, axis)
    629 if len_reps == 1:
    630     repeats = repeats[0]
--> 631     if repeats < 0:
    632         raise ValueError("`repeats` elements must be positive")
    633     scalar = True

TypeError: '<' not supported between instances of 'list' and 'int'

while NumPy raising more explicit exception:

np.__version__
# Out: '2.5.0'

np.repeat(x, [[4]])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 np.repeat(x, [[4]])

File ~/.local/share/mamba/envs/dpnp_dev/lib/python3.13/site-packages/numpy/_core/fromnumeric.py:481, in repeat(a, repeats, axis)
    437 @array_function_dispatch(_repeat_dispatcher)
    438 def repeat(a, repeats, axis=None):
    439     """
    440     Repeat each element of an array after themselves
    441
   (...)    479
    480     """
--> 481     return _wrapfunc(a, 'repeat', repeats, axis=axis)

File ~/.local/share/mamba/envs/dpnp_dev/lib/python3.13/site-packages/numpy/_core/fromnumeric.py:54, in _wrapfunc(obj, method, *args, **kwds)
     51     return _wrapit(obj, method, *args, **kwds)
     53 try:
---> 54     return bound(*args, **kwds)
     55 except TypeError:
     56     # A TypeError occurs if the object does have such a method in its
     57     # class, but its signature is not identical to that of NumPy's. This
   (...)     61     # Call _wrapit from within the except clause to ensure a potential
     62     # exception has a traceback chain.
     63     return _wrapit(obj, method, *args, **kwds)

ValueError: setting an array element with a sequence. The requested array would exceed the maximum number of dimension of 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions