Skip to content

NdArray inconsistently coerces floats and ints #1914

@nathanjmcdougall

Description

@nathanjmcdougall

Initial Checks

  • I have read and followed the docs and still think this is a bug

Description

I would expect floats and ints to be consistently either able to be coerced to NdArrays or not.

Floats cannot be coerced.

Ints can be coerced - but not in the expected way. It seems to use np.empty to initialize an array with N element where N is the int being coerced.

Generally, I would expect that an int or a float would be coerced to a 1-dimensional array with a single element.

Example Code

from docarray import BaseDoc
from docarray.typing import NdArray

class MyDoc(BaseDoc):
    arr: NdArray

MyDoc(arr=10).arr # Undefined behaviour; uninitialized array
>> NdArray([7.17677097e-312, 1.77863633e-322, 0.00000000e+000,
         0.00000000e+000, 5.28555753e+180, 5.98847221e+174,
         5.92957407e-038, 7.42262642e-091, 2.80246406e-032,
                     nan])
MyDoc(arr=10.0).arr # ValidationError

I would expect instead:

MyDoc(arr=10).arr
>> NdArray([10])
MyDoc(arr=10.0).arr
>> NdArray([10.0])

Python, DocArray & OS Version

Python 3.11.9
docarray 0.40.0
Windows 10

Affected Components

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