Skip to content

JPype "unable to convert" 2d boolean NumPy array as of NumPy 2.3 #1307

@john-hen

Description

@john-hen

Not sure if it's a problem with JPype or NumPy (could be the latter), but as of NumPy 2.3, the following script

import jpype
from jpype import JArray, JBoolean
from numpy import array

jpype.startJVM()
bool_2d = JArray(JBoolean, dims=2)(array([[True, False], [False, True]]))

fails with

Traceback (most recent call last):
  File "convert_bool2d.py", line 6, in <module>
    bool_2d = JArray(JBoolean, dims=2)(array([[True, False], [False, True]]))
TypeError: Unable to convert

It does not fail with NumPy 2.2.6, but does with NumPy 2.3.0 and 2.3.1. (Regardless of the JPype version, 1.5.2 or 1.6.0.)

It still works for 1d boolean arrays (dims=1), and for JInt and JDouble arrays of any dimension (that I've tested).

The work-around is to call .tolist() on the boolean NumPy array object, then it works again. My test suite caught this, and yes, I could just use the .tolist() call going forward. Just wanted to bring it to your attention, maybe there's a reason for this. If it is a bug in NumPy, then I wouldn't know how to describe it.

Tested on Windows with Java 17 (via winget install Azul.ZuluFX.17.JDK).

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