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: complain about CannotBeAwkward earlier, before reading data. #838

Merged
merged 2 commits into from
Feb 21, 2023
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
8 changes: 8 additions & 0 deletions src/uproot/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ def awkward_form(model, file, context):
_primitive_awkward_form[model] = awkward.forms.from_json('"float32"')
elif model == numpy.dtype(numpy.float64):
_primitive_awkward_form[model] = awkward.forms.from_json('"float64"')
elif model.fields is not None:
fields = []
contents = []
for field, (dtype, _) in model.fields.items():
fields.append(field)
contents.append(awkward_form(dtype, file, context))
# directly return; don't cache RecordForms in _primitive_awkward_form
return awkward.forms.RecordForm(contents, fields)
Comment on lines +573 to +580
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the main purpose of the PR, but calling the awkward_forth method at this point in the code revealed that the leaf-list case isn't handled. Somehow, test_0033-more-interpretations-2.py::test_leaflist_awkward was passing before because something must have figured out that it needs a RecordArray without calling awkward_forth. (I'm not sure which code path that is, but now, at least, this is consistent with that.)

else:
raise AssertionError(f"{model!r}: {type(model)}")

Expand Down
29 changes: 29 additions & 0 deletions src/uproot/behaviors/TBranch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,26 @@ def postprocess(self, chunk, cursor, context, file):

return self

def _awkward_check(self, interpretation):
try:
interpretation.awkward_form(self.file)
except uproot.interpretation.objects.CannotBeAwkward as err:
raise ValueError(
"""cannot produce Awkward Arrays for interpretation {} because

{}

instead, try library="np" instead of library="ak" or globally set uproot.default_library

in file {}
in object {}""".format(
repr(interpretation),
err.because,
self.file.file_path,
self.object_path,
)
) from err

def debug(
self,
entry,
Expand Down Expand Up @@ -2986,6 +3006,7 @@ def _ranges_or_baskets_to_arrays(
range_args = {}
range_original_index = {}
original_index = 0
branchid_to_branch = {}

for cache_key in branchid_interpretation:
branchid_num_baskets[cache_key] = 0
Expand All @@ -3006,12 +3027,20 @@ def _ranges_or_baskets_to_arrays(

original_index += 1

branchid_to_branch[branch.cache_key] = branch

for cache_key, interpretation in branchid_interpretation.items():
if branchid_num_baskets[cache_key] == 0 and cache_key not in arrays:
arrays[cache_key] = interpretation.final_array(
{}, 0, 0, [0], library, None, interp_options
)

# check for CannotBeAwkward errors on the main thread before reading any data
if isinstance(library, uproot.interpretation.library.Awkward) and isinstance(
interpretation, uproot.interpretation.objects.AsObjects
):
branchid_to_branch[cache_key]._awkward_check(interpretation)

hasbranches._file.source.chunks(ranges, notifications=notifications)

def replace(ranges_or_baskets, original_index, basket):
Expand Down
24 changes: 3 additions & 21 deletions src/uproot/interpretation/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,27 +568,9 @@ def finalize(self, array, branch, interpretation, entry_start, entry_stop, optio
return _awkward_add_doc(awkward, awkward.Array(layout), branch, ak_add_doc)

elif isinstance(interpretation, uproot.interpretation.objects.AsObjects):
try:
form = json.loads(
interpretation.awkward_form(interpretation.branch.file).to_json()
)
except uproot.interpretation.objects.CannotBeAwkward as err:
raise ValueError(
"""cannot produce Awkward Arrays for interpretation {} because

{}

instead, try library="np" instead of library="ak" or globally set uproot.default_library

in file {}
in object {}""".format(
repr(interpretation),
err.because,
interpretation.branch.file.file_path,
interpretation.branch.object_path,
)
) from err

form = json.loads(
interpretation.awkward_form(interpretation.branch.file).to_json()
)
unlabeled = awkward.from_iter(
(_object_to_awkward_json(form, x) for x in array), highlevel=False
)
Expand Down