Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ak.Record dict constructor should retain type. #1981

Merged
merged 2 commits into from
Dec 8, 2022
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
12 changes: 8 additions & 4 deletions src/awkward/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,30 @@ def numpy_at_least(version):
return parse_version(numpy.__version__) >= parse_version(version)


def in_module(obj, modulename):
def in_module(obj, modulename: str) -> bool:
m = type(obj).__module__
return m == modulename or m.startswith(modulename + ".")


def is_file_path(x):
def is_file_path(x) -> bool:
try:
return os.path.isfile(x)
except ValueError:
return False


def is_sized_iterable(obj):
def is_sized_iterable(obj) -> bool:
return isinstance(obj, Iterable) and isinstance(obj, Sized)


def is_integer(x):
def is_integer(x) -> bool:
return isinstance(x, numbers.Integral) and not isinstance(x, bool)


def is_non_string_iterable(obj) -> bool:
return not isinstance(obj, str) and isinstance(obj, Iterable)


def tobytes(array):
if hasattr(array, "tobytes"):
return array.tobytes()
Expand Down
11 changes: 10 additions & 1 deletion src/awkward/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,16 @@ def __init__(
layout = ak.operations.from_json(data, highlevel=False)

elif isinstance(data, dict):
layout = ak.operations.from_iter([data], highlevel=False)[0]
fields = []
contents = []
for k, v in data.items():
fields.append(k)
if ak._util.is_non_string_iterable(v):
contents.append(Array(v).layout[np.newaxis])
else:
contents.append(Array([v]).layout)

layout = ak.record.Record(ak.contents.RecordArray(contents, fields), at=0)

elif isinstance(data, Iterable):
raise ak._errors.wrap_error(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_1978-akRecord-constructor-should-retain-type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import numpy as np
import pytest # noqa: F401

import awkward as ak


def test():
j1 = ak.from_numpy(np.empty(0, np.int32))
assert str(ak.Record({"d": j1}).type) == "{d: 0 * int32}"