Skip to content

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

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

Activity

  1. added this to the 0.36 milestone on Feb 25, 2026
  2. gdementen commented on Feb 25, 2026

    @gdementen
    ContributorAuthor

    Silent string to int conversion happens too, which makes me really sad 😢 :

    >>> source = np.array(["1", "2"])
    >>> target[:] = source
    >>> target
    [1 2]
  3. gdementen commented on Feb 25, 2026

    @gdementen
    ContributorAuthor

    I just realized this problem is not specific to CheckedSession:

    >>> target = zeros(a, dtype=int)
    >>> source = ndtest(a) * 1.6
    >>> source
    a   a0   a1
       0.0  1.6
    >>> target[:] = source
    >>> target
    a  a0  a1
        0   1
    >>> source = Array(["1", "2"], a)
    >>> source
    a  a0  a1
        1   2
    >>> target[:] = source
    >>> target
    a  a0  a1
        1   2

    The only thing specific to CheckedSession is that it is even more confusing because cs.v = arr actually does cs.v[:] = arr cs.v = arr.astype(int) behind the scene :

    >>> cs.v = arr
    >>> cs.v
    a  a0  a1
        0   1
  4. changed the title [-]CheckedSession silently coerce to declared dtype even when unsafe[/-] [+]array[:] = value silently coerce value to array type even when unsafe[/+] on Feb 25, 2026
  5. gdementen commented on Feb 25, 2026

    @gdementen
    ContributorAuthor

    Even though the problem lies with numpy, I think that LArray as the more-user-friendly-less-performance project should tackle this issue directly without waiting for numpy to fix it

  6. modified the milestones: 0.36, 0.35.1 on Mar 3, 2026
  7. gdementen commented on Mar 5, 2026

    @gdementen
    ContributorAuthor

    Given how CheckedSession are implemented (contrary to what I thought, cs.v = arr does NOT do cs.v[:] = arr but rather cs.v = arr.astype(declared_dtype), I think the issue needs to be fixed explicitly for CheckedSession in addition to the general fix.

  8. self-assigned this
    on Mar 10, 2026
  9. added 2 commits that reference this issue on Mar 27, 2026
    244da52
    6385a39
  10. added a commit that references this issue on Apr 3, 2026
    5492157
  11. added a commit that references this issue on Apr 14, 2026
    15f33ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions