Skip to content

TypeError: bool() conversion fails on single-element arrays with ndim > 0 #2784

@abagusetty

Description

@abagusetty

dpnp raises a TypeError when evaluating the truth value of a single-element array with shape (1, 1), while NumPy and CuPy both handle this correctly.

import dpnp

xy_norm = dpnp.ones((1, 1)) * 1.5

if xy_norm > 1e-14:  # TypeError raised here
    print("works")

Output:

  File "/home/abagusetty/gpu4pyscf-testing/gpu4pyscf/./test_dpnp_newissue_v1.py", line 6, in <module>
    if xy_norm > 1e-14:
       ^^^^^^^^^^^^^^^
  File "/home/abagusetty/gpu4pyscf-testing/dpnp/dpnp/dpnp_array.py", line 199, in __bool__
    return self._array_obj.__bool__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dpctl/tensor/_usmarray.pyx", line 1143, in dpctl.tensor._usmarray.usm_ndarray.__bool__
  File "dpctl/tensor/_usmarray.pyx", line 130, in dpctl.tensor._usmarray._check_0d_scalar_conversion
TypeError: only 0-dimensional arrays can be converted to Python scalars

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions