Skip to content

MAINT: cleanup and better document core/overrides.py #12042

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

Merged
merged 2 commits into from
Sep 30, 2018

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Sep 27, 2018

I think this organization makes a little more sense, especially when thinking
about what the functions we want to write in C should look like.

CC @mhvk

xref #12028

I think this organization makes a little more sense, especially when thinking
about what the functions we want to write in C should look like.
@charris charris changed the title CLN: cleanup and better document core/overrides.py MAINT: cleanup and better document core/overrides.py Sep 27, 2018
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

I like the reorganization very much. My comments are only nitpicks about names, partially because I find half-abbreviated endings like _func somewhat annoying. Feel free to ignore.

def array_function_override(overloaded_args, func, types, args, kwargs):
"""Call __array_function__ implementations."""
def array_function_implementation_or_override(
implementation_func, api_func, dispatcher, args, kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Might it make more sense to call the first one ndarray_implementation? That is what it would be if we actually worked via ndarray.__array_ufunc__. A nice aspect of that is that one could rename api_func to function.

Or perhaps just implementation and function?

Copy link
Member Author

Choose a reason for hiding this comment

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

How about implementation and public_api?


if result is not NotImplemented:
return result

raise TypeError('no implementation found for {} on types that implement '
'__array_function__: {}'
.format(func, list(map(type, overloaded_args))))
.format(api_func, list(map(type, overloaded_args))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Only noticing this now, but wouldn't it make sense to just print types in the error message? This will include ndarray if an array was present, but I think that is logically correct: in a way, its __array_function__ has been (implicitly) tried, and ti cannot handle it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm would lean against this since we don't actually call ndarray.__array_function__.

Copy link
Contributor

Choose a reason for hiding this comment

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

We only skip it because we know its answer! But not a big deal either way.

Copy link
Member Author

Choose a reason for hiding this comment

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

We also skip it because ndarray.__array_function__ calls the public API function rather than the implementation function, so we need to avoid the infinite loop :)

def decorator(implementation):
@functools.wraps(implementation)
def public_api(*args, **kwargs):
relevant_args = dispatcher(*args, **kwargs)
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved the call to dispatcher(*args, **kwargs) into array_function_dispatcher() to leave things slightly more flexible for array_function_implementation_or_override(), e.g., for functions with complex argument parsing such as np.einsum().

@charris
Copy link
Member

charris commented Sep 30, 2018

Thanks Stephan.

@shoyer shoyer deleted the py-overrides-cleanup branch September 30, 2018 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants