Skip to content

BUG: Cannot create zero-length array-like object via __array_interface__ providing null pointer as data #26037

@ischurov

Description

@ischurov

Describe the issue:

I am trying to create a custom array-like object that stores zero-length one-dimensional array using __array_interface__. As the array is zero-length and therefore does not contain any data, I provide a null pointer as the data field of __array_interface__ (i.e. "data": (0, False)). Then my object is seemingly treated as a scalar by np.array, despite the fact that shape is (0,). If I provide anything different from 0 as the data, everything works.

Reproduce the code example:

import numpy as np

class TestArray:
    def __init__(self):
        self.__array_interface__ = {
            "data": (0, False),
            "strides": None,
            "descr": [("", "<f8")],
            "typestr": "<f8",
            "shape": (0,),
            "version": 3,
        }

np.array(TestArray())

Error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 np.array(TestArray())

TypeError: float() argument must be a string or a real number, not 'TestArray'

Python and NumPy Versions:

1.26.1
3.11.6 (main, Oct 2 2023, 13:45:54) [GCC 12.3.0]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions