Skip to content

array[:] = value silently coerce value to array type even when unsafe #1164

@gdementen

Description

@gdementen

Unsure if this should be a warning or an error, but doing this silently is clearly not acceptable to me for unsafe conversions.

class CS(CheckedSession):
    v: CheckedArray(a, dtype=int)

>>> arr = ndtest(a) * 1.6
>>> arr
a   a0   a1
   0.0  1.6
>>> cs = CS(v=arr)
>>> cs.v
a  a0  a1
    0   1

Surprisingly Numpy has the issue too (I suppose we inherit it from there):

>>> import numpy as np
>>> np.__version__
'2.3.5'
>>> target = np.zeros(2, dtype=np.int64)
>>> source = np.array([0., 1.6])
>>> source.dtype
dtype('float64')
>>> np.can_cast(source.dtype, target.dtype)
False
>>> target[:] = source
>>> target
[0 1]

This has been a known issue in Numpy for a looong time but even though the developers seem to agree it is unfortunate, it will be a while until that behavior can be changed (if ever) in Numpy. See numpy/numpy#7730 and numpy/numpy#8733

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions