Skip to content

Commit

Permalink
fix: proposal for #2947 (#3191)
Browse files Browse the repository at this point in the history
* fix: cast numpy integers to integers in ak.from_buffers (fixes #2947)

* Add test for 3502225e9246ce0197d6143c9d94c9e1aeec88b8

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
maxgalli and pre-commit-ci[bot] authored Jul 31, 2024
1 parent 904b3a8 commit d5bc304
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/awkward/operations/ak_from_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def _impl(
elif isinstance(form, dict):
form = ak.forms.from_dict(form)

if isinstance(length, np.integer):
length = int(length)

if not (is_integer(length) and length >= 0):
raise TypeError("'length' argument must be a non-negative integer")

Expand Down
18 changes: 18 additions & 0 deletions tests/test_2947_to_list_numpy_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
from __future__ import annotations

import numpy as np

import awkward as ak


def test():
"""Check that to_list() does not break when the array is built from buffers
with a length of type np.int64.
"""
awk = ak.Array(np.ones((7, 0)))
form, length, container = ak.to_buffers(ak.to_packed(awk))
awk_from_buf = ak.from_buffers(form, np.int64(length), container)
lst = awk_from_buf.to_list()

assert len(lst) == length

0 comments on commit d5bc304

Please sign in to comment.