Skip to content

i8 conversion deprecation warnings in CPython #2114

Open
@rebcabin

Description

@rebcabin

The following is a minimal example that works in both LPython and CPython, honoring many prior issues about initialization of classes and whatnot. It still generates very noisy deprecation warnings in CPython. In my real application, these warnings are far to numerous to tolerate. To mitigate this, I must write my LPython so that every assignment from an i8 into an array of int8 subtracts i8(256) or something else equally grotesque like converting to i32 and masking off the low bits (the subtraction trick doesn't always work). The mitigation litters my code with many unsemantical constructs plus slows it down needlessly.

I can't propose a great solution, yet, but I'm bringing it to your attention:

from lpython import (i8, # i32, i64, f32, f64,
                     dataclass
                     )
from numpy import (empty,
                   int8,
                   )

r : i8 = i8(240)

@dataclass
class Foo:
    a : i8[4] = empty(4, dtype=int8)

foo : Foo = Foo()
foo.a = empty(4, dtype=int8)

############# ATTENTION: GENERATES DEPRECATION WARNING IN CPYTHON
foo.a[0] = r
############# DOES NOT GENERATE WARNING IN CPYTHON
foo.a[0] = r - i8(256)

print(foo.a[0])

runs:

(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm)─────────────────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s000)─┐
└─(18:08:55 on brian-lasr ✹ ✭)──> ~/CLionProjects/lpython/src/bin/lpython -I. issue2108.py                                                                ──(Wed,Jul05)─┘
-16
(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm)─────────────────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s000)─┐
└─(18:10:30 on brian-lasr ✹ ✭)──> PYTHONPATH='../../src/runtime/lpython' python issue2108.py                                                              ──(Wed,Jul05)─┘
/Users/brian/CLionProjects/lpython/lasr/LP-pycharm/issue2108.py:21: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 240 to int8 will fail in the future.
For the old behavior, usually:
    np.array(value).astype(dtype)
will give the desired result (the cast overflows).
  foo.a[0] = r
-16

Metadata

Metadata

Assignees

No one assigned

    Labels

    severenot blocker, but causes undesirable workaroundsusability

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions