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

feat: made 'very optional' arguments keyword-only #1905

Merged
merged 6 commits into from
Nov 28, 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
2 changes: 1 addition & 1 deletion docs/user-guide/how-to-create-arraybuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ new_array
new_array.layout
```

Above, we see that `new_array` is just making references ({class}`ak.layout.IndexedArray`) of an {classY}`ak.layout.RecordArray` with `x = [1, 2, 3]` and `y = [1.1, 2.2, 3.3]`.
Above, we see that `new_array` is just making references ({class}`ak.layout.IndexedArray`) of an {class}`ak.layout.RecordArray` with `x = [1, 2, 3]` and `y = [1.1, 2.2, 3.3]`.

+++

Expand Down
3 changes: 1 addition & 2 deletions src/awkward/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def behavior_of(*arrays, **kwargs):
highs = (
ak.highlevel.Array,
ak.highlevel.Record,
# ak.highlevel.ArrayBuilder,
# ak.highlevel.ArrayBuilder,
)
for x in arrays[::-1]:
if isinstance(x, highs) and x.behavior is not None:
Expand All @@ -429,7 +429,6 @@ def behavior_of(*arrays, **kwargs):
return behavior


# maybe_wrap and maybe_wrap_like go here
def wrap(content, behavior=None, highlevel=True, like=None):
assert content is None or isinstance(
content, (ak.contents.Content, ak.record.Record)
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/behaviors/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def register(cls):
return register


def mixin_class_method(ufunc, rhs=None, transpose=True):
def mixin_class_method(ufunc, rhs=None, *, transpose=True):
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 one that I should ask @nsmith- about: is it the case that rhs is likely to be positional but transpose should always be written out as keyword-only?

Copy link
Member

@nsmith- nsmith- Nov 22, 2022

Choose a reason for hiding this comment

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

Correct. Any binary method would need the rhs specified but transpose is more rare.
As an aside, in retrospect, I should have made transpose default false but it is too late. That was the cause of scikit-hep/coffea#674

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, good to know about rhs.

About transpose: changing the default from True to False would be dangerous, even at the 2.0.0 boundary, unless we also change the name. As a keyword-only argument, the written-out old name would be different from the written-out new name, so old code can't pass through accidentally. After the 2.0.0 boundary, we'd have to have a period of time in which both names exist, with a warning on the old name, but we could make a clean switch to a new name if we do it before the 2.0.0 boundary.

What do you think? Is there a better word than transpose that would justify the change in default?

Copy link
Member

Choose a reason for hiding this comment

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

I think this will just have to stay. It is not a well-trodden path anyway.

"""
Args:
ufunc (numpy.ufunc): A universal function (or NEP18 callable) that is
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/contents/listarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,9 @@ def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
)

def _to_numpy(self, allow_missing):
return ak.operations.to_numpy(self.toRegularArray(), allow_missing)
return ak.operations.to_numpy(
self.toRegularArray(), allow_missing=allow_missing
)

def _completely_flatten(self, nplike, options):
if (
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/contents/listoffsetarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,9 @@ def _to_numpy(self, allow_missing):
if array_param in {"bytestring", "string"}:
return self._nplike.array(self.to_list())

return ak.operations.to_numpy(self.toRegularArray(), allow_missing)
return ak.operations.to_numpy(
self.toRegularArray(), allow_missing=allow_missing
)

def _completely_flatten(self, nplike, options):
if (
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/bitmaskedform.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _type(self, typestrs):
return ak.types.OptionType(
self._content._type(typestrs),
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
).simplify_option_union()

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/bytemaskedform.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _type(self, typestrs):
return ak.types.OptionType(
self._content._type(typestrs),
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
).simplify_option_union()

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/emptyform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _to_dict_part(self, verbose, toplevel):
def _type(self, typestrs):
return ak.types.UnknownType(
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/indexedoptionform.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _type(self, typestrs):
return ak.types.OptionType(
self._content._type(typestrs),
parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
).simplify_option_union()

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/listform.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _type(self, typestrs):
return ak.types.ListType(
self._content._type(typestrs),
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/listoffsetform.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _type(self, typestrs):
return ak.types.ListType(
self._content._type(typestrs),
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/numpyform.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _type(self, typestrs):
out = ak.types.NumpyType(
self._primitive,
None,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)
for x in self._inner_shape[::-1]:
out = ak.types.RegularType(out, x)
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/recordform.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _type(self, typestrs):
[x._type(typestrs) for x in self._contents],
self._fields,
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/regularform.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _type(self, typestrs):
self._content._type(typestrs),
self._size,
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/unionform.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _type(self, typestrs):
return ak.types.UnionType(
[x._type(typestrs) for x in self._contents],
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
)

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/forms/unmaskedform.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _type(self, typestrs):
return ak.types.OptionType(
self._content._type(typestrs),
self._parameters,
ak._util.gettypestr(self._parameters, typestrs),
typestr=ak._util.gettypestr(self._parameters, typestrs),
).simplify_option_union()

def __eq__(self, other):
Expand Down
6 changes: 4 additions & 2 deletions src/awkward/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class Array(NDArrayOperatorsMixin, Iterable, Sized):
def __init__(
self,
data,
*,
behavior=None,
with_name=None,
check_valid=False,
Expand Down Expand Up @@ -351,7 +352,7 @@ def __getitem__(self, where):
with ak._errors.OperationErrorContext(
"ak.Array.mask", {0: self._array, 1: where}
):
return ak.operations.mask(self._array, where, True)
return ak.operations.mask(self._array, where, valid_when=True)

@property
def mask(self):
Expand Down Expand Up @@ -1475,6 +1476,7 @@ class Record(NDArrayOperatorsMixin):
def __init__(
self,
data,
*,
behavior=None,
with_name=None,
check_valid=False,
Expand Down Expand Up @@ -2168,7 +2170,7 @@ class ArrayBuilder(Sized):
be considered the "least effort" approach.
"""

def __init__(self, behavior=None, initial=1024, resize=1.5):
def __init__(self, *, behavior=None, initial=1024, resize=1.5):
self._layout = _ext.ArrayBuilder(initial=initial, resize=resize)
self.behavior = behavior

Expand Down
8 changes: 4 additions & 4 deletions src/awkward/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class Index:
_expected_dtype = None

def __init__(self, data, metadata=None, nplike=None):
def __init__(self, data, *, metadata=None, nplike=None):
if nplike is None:
nplike = ak.nplikes.nplike_of(data)
self._nplike = nplike
Expand Down Expand Up @@ -111,7 +111,7 @@ def forget_length(self):
data = self._data
else:
data = self.raw(tt)
return type(self)(data.forget_length(), self._metadata, tt)
return type(self)(data.forget_length(), metadata=self._metadata, nplike=tt)

def raw(self, nplike):
return self.nplike.index_nplike.raw(self.data, nplike.index_nplike)
Expand Down Expand Up @@ -186,8 +186,8 @@ def __copy__(self):
def __deepcopy__(self, memo):
return type(self)(
copy.deepcopy(self._data, memo),
copy.deepcopy(self._metadata, memo),
self._nplike,
metadata=copy.deepcopy(self._metadata, memo),
nplike=self._nplike,
)

def _nbytes_part(self):
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/operations/ak_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


@ak._connect.numpy.implements("all")
def all(array, axis=None, keepdims=False, mask_identity=False, flatten_records=False):
def all(
array, axis=None, *, keepdims=False, mask_identity=False, flatten_records=False
):
"""
Args:
array: Array-like data (anything #ak.to_layout recognizes).
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/operations/ak_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


@ak._connect.numpy.implements("any")
def any(array, axis=None, keepdims=False, mask_identity=False, flatten_records=False):
def any(
array, axis=None, *, keepdims=False, mask_identity=False, flatten_records=False
):
"""
Args:
array: Array-like data (anything #ak.to_layout recognizes).
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_argcartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def argcartesian(
arrays,
axis=1,
*,
nested=None,
parameters=None,
with_name=None,
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_argcombinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def argcombinations(
array,
n,
*,
replacement=False,
axis=1,
fields=None,
Expand Down
6 changes: 4 additions & 2 deletions src/awkward/operations/ak_argmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


@ak._connect.numpy.implements("argmax")
def argmax(array, axis=None, keepdims=False, mask_identity=True, flatten_records=False):
def argmax(
array, axis=None, *, keepdims=False, mask_identity=True, flatten_records=False
):
"""
Args:
array: Array-like data (anything #ak.to_layout recognizes).
Expand Down Expand Up @@ -59,7 +61,7 @@ def argmax(array, axis=None, keepdims=False, mask_identity=True, flatten_records

@ak._connect.numpy.implements("nanargmax")
def nanargmax(
array, axis=None, keepdims=False, mask_identity=True, flatten_records=False
array, axis=None, *, keepdims=False, mask_identity=True, flatten_records=False
):
"""
Args:
Expand Down
6 changes: 4 additions & 2 deletions src/awkward/operations/ak_argmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


@ak._connect.numpy.implements("argmin")
def argmin(array, axis=None, keepdims=False, mask_identity=True, flatten_records=False):
def argmin(
array, axis=None, *, keepdims=False, mask_identity=True, flatten_records=False
):
"""
Args:
array: Array-like data (anything #ak.to_layout recognizes).
Expand Down Expand Up @@ -59,7 +61,7 @@ def argmin(array, axis=None, keepdims=False, mask_identity=True, flatten_records

@ak._connect.numpy.implements("nanargmin")
def nanargmin(
array, axis=None, keepdims=False, mask_identity=True, flatten_records=False
array, axis=None, *, keepdims=False, mask_identity=True, flatten_records=False
):
"""
Args:
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/operations/ak_argsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


@ak._connect.numpy.implements("argsort")
def argsort(array, axis=-1, ascending=True, stable=True, highlevel=True, behavior=None):
def argsort(
array, axis=-1, *, ascending=True, stable=True, highlevel=True, behavior=None
):
"""
Args:
array: Data for which to get a sorting index, possibly within nested
Expand Down
3 changes: 2 additions & 1 deletion src/awkward/operations/ak_cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def cartesian(
arrays,
axis=1,
*,
nested=None,
parameters=None,
with_name=None,
Expand Down Expand Up @@ -320,7 +321,7 @@ def _impl(arrays, axis, nested, parameters, with_name, highlevel, behavior):
ak.index.Index64(x.reshape(-1), nplike=nplike)
for x in nplike.index_nplike.meshgrid(
*[nplike.index_nplike.arange(len(x), dtype=np.int64) for x in layouts],
indexing="ij"
indexing="ij",
)
]
outs = [
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def combinations(
array,
n,
*,
replacement=False,
axis=1,
fields=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@ak._connect.numpy.implements("concatenate")
def concatenate(
arrays, axis=0, merge=True, mergebool=True, highlevel=True, behavior=None
arrays, axis=0, *, merge=True, mergebool=True, highlevel=True, behavior=None
):
"""
Args:
Expand Down
11 changes: 8 additions & 3 deletions src/awkward/operations/ak_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def corr(
y,
weight=None,
axis=None,
*,
keepdims=False,
mask_identity=False,
flatten_records=False,
Expand Down Expand Up @@ -71,15 +72,19 @@ def corr(


def _impl(x, y, weight, axis, keepdims, mask_identity, flatten_records):
behavior = ak._util.behavior_of(x, y, weight)
x = ak.highlevel.Array(
ak.operations.to_layout(x, allow_record=False, allow_other=False)
ak.operations.to_layout(x, allow_record=False, allow_other=False),
behavior=behavior,
)
y = ak.highlevel.Array(
ak.operations.to_layout(y, allow_record=False, allow_other=False)
ak.operations.to_layout(y, allow_record=False, allow_other=False),
behavior=behavior,
)
if weight is not None:
weight = ak.highlevel.Array(
ak.operations.to_layout(weight, allow_record=False, allow_other=False)
ak.operations.to_layout(weight, allow_record=False, allow_other=False),
behavior=behavior,
)

with np.errstate(invalid="ignore", divide="ignore"):
Expand Down
4 changes: 3 additions & 1 deletion src/awkward/operations/ak_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
np = ak.nplikes.NumpyMetadata.instance()


def count(array, axis=None, keepdims=False, mask_identity=False, flatten_records=False):
def count(
array, axis=None, *, keepdims=False, mask_identity=False, flatten_records=False
):
"""
Args:
array: Array-like data (anything #ak.to_layout recognizes).
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_count_nonzero.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@ak._connect.numpy.implements("count_nonzero")
def count_nonzero(
array, axis=None, keepdims=False, mask_identity=False, flatten_records=False
array, axis=None, *, keepdims=False, mask_identity=False, flatten_records=False
):
"""
Args:
Expand Down
Loading