Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/arcadedb/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ def to_java_float_array(vector):

Returns:
Java float array compatible with ArcadeDB vector indexes

Raises:
ImportError: On the ``arcadedb`` (native) package when NumPy is not installed; the array it returns is a NumPy array.
"""
if _native_enabled():
if _np is None:
raise ImportError(
"to_java_float_array() needs NumPy on the arcadedb package: install it with 'uv add \"arcadedb[numpy]\"'"
)
return _np.asarray(
vector if hasattr(vector, "__array__") else list(vector), dtype=_np.float32
)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def test_to_java_byte_array_accepts_bytes_like_fast_paths(jvm):
) == [0, 127, -1]


def test_to_java_float_array_without_numpy_on_the_native_build_says_what_to_install(
monkeypatch,
):
"""NumPy is an optional extra of the ``arcadedb`` package. The native conversion read ``_np.asarray`` unguarded, so a list
raised "'NoneType' object has no attribute 'asarray'" instead of naming the extra.
"""
from arcadedb import vector

monkeypatch.setattr(vector, "_native_enabled", lambda: True)
monkeypatch.setattr(vector, "_np", None)
with pytest.raises(ImportError, match=r"arcadedb\[numpy\]"):
arcadedb.to_java_float_array([0.1, 0.2])


def test_to_java_float_array_accepts_numpy_directly(jvm):
"""to_java_float_array should accept NumPy arrays without Python-list copies."""
np = pytest.importorskip("numpy")
Expand Down
Loading