Skip to content

Commit

Permalink
fix: add allow_none
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Oct 22, 2023
1 parent d5997c6 commit 3ce35d4
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/awkward/_connect/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def array_function(func, types, args, kwargs: dict[str, Any], behavior: Mapping
result,
allow_record=True,
allow_other=True,
allow_none=True,
regulararray=True,
use_from_iter=True,
scalar_policy="allow",
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def normalise_item(item, backend: Backend) -> SliceItem:
item,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=False,
use_from_iter=False,
scalar_policy="error",
Expand All @@ -303,7 +304,8 @@ def normalise_item(item, backend: Backend) -> SliceItem:
layout = ak.operations.ak_to_layout._impl(
item,
allow_record=False,
allow_other=True,
allow_other=False,
allow_none=False,
regulararray=False,
use_from_iter=True,
scalar_policy="error",
Expand Down
2 changes: 2 additions & 0 deletions src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def _getitem(self, where):
where,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=False,
use_from_iter=False,
scalar_policy="error",
Expand All @@ -679,6 +680,7 @@ def _getitem(self, where):
where,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=False,
use_from_iter=True,
scalar_policy="error",
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_is_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def _impl(array):
array,
allow_record=True,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="error",
Expand Down
18 changes: 15 additions & 3 deletions src/awkward/operations/ak_nan_to_num.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,33 @@ def _impl(array, copy, nan, posinf, neginf, highlevel, behavior):
broadcasting.append(layout)

nan_layout = ak.operations.to_layout(
nan, allow_other=False, scalar_policy="allow", allow_record=False
nan,
allow_other=False,
allow_none=True,
scalar_policy="allow",
allow_record=False,
)
if isinstance(nan_layout, ak.contents.Content):
broadcasting_ids[id(nan)] = len(broadcasting)
broadcasting.append(nan_layout)

posinf_layout = ak.operations.to_layout(
posinf, allow_other=False, scalar_policy="allow", allow_record=False
posinf,
allow_other=False,
allow_none=True,
scalar_policy="allow",
allow_record=False,
)
if isinstance(posinf_layout, ak.contents.Content):
broadcasting_ids[id(posinf)] = len(broadcasting)
broadcasting.append(posinf_layout)

neginf_layout = ak.operations.to_layout(
neginf, allow_other=False, scalar_policy="allow", allow_record=False
neginf,
allow_other=False,
allow_none=True,
scalar_policy="allow",
allow_record=False,
)
if isinstance(neginf_layout, ak.contents.Content):
broadcasting_ids[id(neginf)] = len(broadcasting)
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_to_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def _impl(
array,
allow_record=True,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="error",
Expand Down
56 changes: 32 additions & 24 deletions src/awkward/operations/ak_to_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def to_layout(
*,
allow_record=True,
allow_other=False,
allow_none=False,
use_from_iter=True,
scalar_policy="promote",
string_as_characters=True,
Expand All @@ -45,7 +46,9 @@ def to_layout(
allow_record (bool): If True, allow #ak.record.Record as an output;
otherwise, if the output would be a scalar record, raise an error.
allow_other (bool): If True, allow non-Awkward outputs; otherwise,
if the output would be another type, raise an error.
raise an error.
allow_none (bool): If True, allow None outputs; otherwise, raise an
error.
use_from_iter (bool): If True, allow conversion of iterable inputs to
arrays using #ak.from_iter; otherwise, throw an Exception.
scalar_policy ("error", "allow", "promote"): If "error", throw an Exception
Expand Down Expand Up @@ -73,6 +76,7 @@ def to_layout(
array,
allow_record,
allow_other,
allow_none,
regulararray,
use_from_iter,
scalar_policy,
Expand All @@ -91,19 +95,23 @@ def maybe_merge_mappings(primary, secondary):
return {**primary, **secondary}


def _handle_array_like(obj, layout, *, scalar_policy):
def _handle_as_scalar(obj, layout, *, scalar_policy):
assert scalar_policy in ("allow", "promote", "error")

if scalar_policy == "allow":
return layout[0]
elif scalar_policy == "promote":
return layout
else:
assert scalar_policy == "error"
raise TypeError(
f"Encountered a scalar ({type(obj).__name__}), but scalars conversion/promotion is disabled"
)


def _handle_array_like(obj, layout, *, scalar_policy):
if obj.ndim == 0:
if scalar_policy == "allow":
return layout[0]
elif scalar_policy == "promote":
return layout
else:
assert scalar_policy == "error"
raise TypeError(
f"Encountered a scalar ({type(obj).__name__}), but scalars conversion/promotion is disabled"
)
return _handle_as_scalar(obj, layout, scalar_policy=scalar_policy)
else:
return layout

Expand All @@ -112,6 +120,7 @@ def _impl(
obj,
allow_record,
allow_other,
allow_none,
regulararray,
use_from_iter,
scalar_policy,
Expand Down Expand Up @@ -182,28 +191,27 @@ def _impl(
return _handle_array_like(obj, promoted_layout, scalar_policy=scalar_policy)
# Scalars
elif isinstance(obj, (str, bytes)):
maybe_layout = ak.operations.from_iter([obj], highlevel=False)
layout = ak.operations.from_iter([obj], highlevel=False)
if scalar_policy == "allow":
if string_as_characters:
return maybe_layout[0]
return layout[0]
else:
return obj
elif scalar_policy == "promote":
return maybe_layout
return layout
else:
raise TypeError(
f"Encountered a scalar ({type(obj).__name__}), but scalars conversion/promotion is disabled"
)
elif isinstance(obj, (datetime, date, time, Number, bool)): # or obj is None:
maybe_layout = ak.operations.from_iter([obj], highlevel=False)
if scalar_policy == "allow":
return maybe_layout[0]
elif scalar_policy == "promote":
return maybe_layout
elif isinstance(obj, (datetime, date, time, Number, bool)):
layout = ak.operations.from_iter([obj], highlevel=False)
return _handle_as_scalar(obj, layout, scalar_policy=scalar_policy)
elif obj is None:
if allow_none:
layout = ak.operations.from_iter([obj], highlevel=False)
return _handle_as_scalar(obj, layout, scalar_policy=scalar_policy)
else:
raise TypeError(
f"Encountered a scalar ({type(obj).__name__}), but scalars conversion/promotion is disabled"
)
raise TypeError("Encountered None value, and `allow_none` is `False`")
# Iterables
elif isinstance(obj, Iterable):
if use_from_iter:
Expand All @@ -219,5 +227,5 @@ def _impl(
return obj
else:
raise TypeError(
f"Encountered unknown type {type(obj).__name__}, and `pass_through_unknown` is `False`"
f"Encountered unknown type {type(obj).__name__}, and `allow_other` is `False`"
)
1 change: 1 addition & 0 deletions src/awkward/operations/ak_to_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def to_parquet(
data,
allow_record=True,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="error",
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_to_rdataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _impl(
array,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="forbid",
Expand Down
2 changes: 2 additions & 0 deletions src/awkward/operations/ak_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def _impl(
array,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="error",
Expand All @@ -477,6 +478,7 @@ def _impl(
x,
allow_record=False,
allow_other=False,
allow_none=False,
regulararray=True,
use_from_iter=True,
scalar_policy="error",
Expand Down
6 changes: 5 additions & 1 deletion src/awkward/operations/ak_with_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def _impl(base, what, where, highlevel, behavior):
base, allow_record=True, allow_other=False
).to_backend(backend)
what = ak.operations.to_layout(
what, allow_record=True, allow_other=True, scalar_policy="allow"
what,
allow_record=True,
allow_other=False,
allow_none=True,
scalar_policy="allow",
)
if isinstance(what, (ak.contents.Content, ak.record.Record)):
what = what.to_backend(backend)
Expand Down

0 comments on commit 3ce35d4

Please sign in to comment.