Skip to content

Commit

Permalink
refactor: add new module and name arguments to `high_level_functi…
Browse files Browse the repository at this point in the history
…on` decorator (#2620)
  • Loading branch information
agoose77 authored Aug 8, 2023
1 parent 47bfdaa commit 6e69067
Show file tree
Hide file tree
Showing 103 changed files with 131 additions and 114 deletions.
21 changes: 19 additions & 2 deletions src/awkward/_dispatch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from collections.abc import Callable, Collection, Generator
from functools import wraps
from inspect import isgenerator
Expand All @@ -10,13 +12,28 @@
HighLevelType: TypeAlias = "Callable[..., T]"


def high_level_function(func: DispatcherType) -> HighLevelType:
def high_level_function(
module: str = "ak", name: str | None = None
) -> Callable[[DispatcherType], HighLevelType]:
"""Decorate a high-level function such that it may be overloaded by third-party array objects"""

def capture_func(func: DispatcherType) -> HighLevelType:
if name is None:
captured_name = func.__qualname__
else:
captured_name = name
return named_high_level_function(func, f"{module}.{captured_name}")

return capture_func


def named_high_level_function(func: DispatcherType, name: str) -> HighLevelType:
"""Decorate a named high-level function such that it may be overloaded by third-party array objects"""

@wraps(func)
def dispatch(*args, **kwargs):
# NOTE: this decorator assumes that the operation is exposed under `ak.`
with OperationErrorContext(f"ak.{func.__qualname__}", args, kwargs):
with OperationErrorContext(name, args, kwargs):
gen_or_result = func(*args, **kwargs)
if isgenerator(gen_or_result):
array_likes = next(gen_or_result)
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def all(
array,
axis=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def almost_equal(
left,
right,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def any(
array,
axis=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_argcartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cpu = NumpyBackend.instance()


@high_level_function
@high_level_function()
def argcartesian(
arrays,
axis=1,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_argcombinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def argcombinations(
array,
n,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/operations/ak_argmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def argmax(
array,
axis=None,
Expand Down Expand Up @@ -67,7 +67,7 @@ def argmax(
return _impl(array, axis, keepdims, mask_identity, highlevel, behavior)


@high_level_function
@high_level_function()
def nanargmax(
array,
axis=None,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/operations/ak_argmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def argmin(
array,
axis=None,
Expand Down Expand Up @@ -67,7 +67,7 @@ def argmin(
return _impl(array, axis, keepdims, mask_identity, highlevel, behavior)


@high_level_function
@high_level_function()
def nanargmin(
array,
axis=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_argsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def argsort(
array, axis=-1, *, ascending=True, stable=True, highlevel=True, behavior=None
):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from awkward._dispatch import high_level_function


@high_level_function
@high_level_function()
def backend(*arrays):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_broadcast_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
cpu = NumpyBackend.instance()


@high_level_function
@high_level_function()
def broadcast_arrays(
*arrays,
depth_limit=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_broadcast_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
cpu = NumpyBackend.instance()


@high_level_function
@high_level_function()
def broadcast_fields(*arrays, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
cpu = NumpyBackend.instance()


@high_level_function
@high_level_function()
def cartesian(
arrays,
axis=1,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from awkward._layout import wrap_layout


@high_level_function
@high_level_function()
def categories(array, highlevel=True):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def combinations(
array,
n,
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 @@ -18,7 +18,7 @@


@ak._connect.numpy.implements("concatenate")
@high_level_function
@high_level_function()
def concatenate(arrays, axis=0, *, mergebool=True, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def copy(array):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def corr(x, y, weight=None, axis=None, *, keepdims=False, mask_identity=False):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def count(
array,
axis=None,
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 @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def count_nonzero(
array,
axis=None,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_covar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def covar(x, y, weight=None, axis=None, *, keepdims=False, mask_identity=False):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_drop_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def drop_none(array, axis=None, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_enforce_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def enforce_type(array, type, *, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def fields(array):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_fill_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
cpu = NumpyBackend.instance()


@high_level_function
@high_level_function()
def fill_none(array, value, axis=-1, *, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_firsts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def firsts(array, axis=1, *, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def flatten(array, axis=1, *, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def from_arrow(array, *, generate_bitmasks=False, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_arrow_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def from_arrow_schema(schema):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_avro_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def from_avro_file(
file, limit_entries=None, *, debug_forth=False, highlevel=True, behavior=None
):
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
numpy = Numpy.instance()


@high_level_function
@high_level_function()
def from_buffers(
form,
length,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from awkward._layout import wrap_layout


@high_level_function
@high_level_function()
def from_categorical(array, *, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from awkward._layout import from_arraylib, wrap_layout


@high_level_function
@high_level_function()
def from_cupy(array, *, regulararray=False, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
np = NumpyMetadata.instance()


@high_level_function
@high_level_function()
def from_iter(
iterable,
*,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from awkward._layout import from_arraylib, wrap_layout


@high_level_function
@high_level_function()
def from_jax(array, *, regulararray=False, highlevel=True, behavior=None):
"""
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
numpy = Numpy.instance()


@high_level_function
@high_level_function()
def from_json(
source,
*,
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/ak_from_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from awkward._layout import from_arraylib, wrap_layout


@high_level_function
@high_level_function()
def from_numpy(
array, *, regulararray=False, recordarray=True, highlevel=True, behavior=None
):
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/operations/ak_from_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from awkward._regularize import is_integer


@high_level_function
@high_level_function()
def from_parquet(
path,
*,
Expand Down Expand Up @@ -76,7 +76,7 @@ def from_parquet(
)


@high_level_function
@high_level_function()
def metadata(
path,
storage_options=None,
Expand Down
Loading

0 comments on commit 6e69067

Please sign in to comment.