-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add array dispatcher (inline) (#2531)
- Loading branch information
Showing
103 changed files
with
733 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from collections.abc import Callable, Collection, Generator | ||
from functools import wraps | ||
from inspect import isgenerator | ||
|
||
from awkward._errors import OperationErrorContext | ||
from awkward._typing import Any, TypeAlias, TypeVar | ||
|
||
T = TypeVar("T") | ||
DispatcherType: TypeAlias = "Callable[..., Generator[Collection[Any], None, T]]" | ||
HighLevelType: TypeAlias = "Callable[..., T]" | ||
|
||
|
||
def high_level_function(func: DispatcherType) -> HighLevelType: | ||
"""Decorate a 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): | ||
gen_or_result = func(*args, **kwargs) | ||
if isgenerator(gen_or_result): | ||
array_likes = next(gen_or_result) | ||
assert isinstance(array_likes, Collection) | ||
|
||
# Permit a third-party array object to intercept the invocation | ||
for array_like in array_likes: | ||
try: | ||
custom_impl = array_like.__awkward_function__ | ||
except AttributeError: | ||
continue | ||
else: | ||
result = custom_impl(dispatch, array_likes, args, kwargs) | ||
|
||
# Future proof the implementation by permitting the `__awkward_function__` to return `NotImplemented` | ||
# This may later be used to signal that another overload should be used. | ||
if result is NotImplemented: | ||
raise NotImplementedError | ||
else: | ||
return result | ||
|
||
# Failed to find a custom overload, so resume the original function | ||
try: | ||
next(gen_or_result) | ||
except StopIteration as err: | ||
return err.value | ||
else: | ||
raise AssertionError( | ||
"high-level functions should only implement a single yield statement" | ||
) | ||
|
||
return gen_or_result | ||
|
||
return dispatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.