Skip to content

BUG: Implement ArrayFunctionDispatcher.__get__ #23039

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 1 commit into from
Jan 19, 2023

Conversation

seberg
Copy link
Member

@seberg seberg commented Jan 18, 2023

While functions should not normally need this, Python functions do provide it (C functions do not, but we are a fatter object anyway).

By implementing __get__ we also ensure that inspect.isroutine() passes. And by that we ensure that Sphinx considers these a py:function: role.

Closes gh-23032

@seberg seberg changed the title BUG: Impelement ArrayFunctionDispatcher.__get__ BUG: Implement ArrayFunctionDispatcher.__get__ Jan 18, 2023
While functions should not normally need this, Python functions do
provide it (C functions do not, but we are a fatter object anyway).

By implementing `__get__` we also ensure that `inspect.isroutine()`
passes.  And by that we ensure that Sphinx considers these a `py:function:`
role.

Closes numpygh-23032
@seberg seberg force-pushed the fixup-array-func-C branch from a814bec to c7b1269 Compare January 18, 2023 21:00
@mattip
Copy link
Member

mattip commented Jan 19, 2023

I can confirm that downloading the object.inv from the build and inspecting it gives the correct output now:

$ sphobjinv suggest ~/Downloads/objects.inv np.where
...

Project: NumPy
Version: 1.25.dev0

8110 objects in inventory.

--------------------------------------------------------------------

2 results found at/above current threshold of 75.

:py:function:`numpy.where`
:std:doc:`reference/generated/numpy.where`

@mattip mattip merged commit e2fccd9 into numpy:main Jan 19, 2023
@mattip
Copy link
Member

mattip commented Jan 19, 2023

Thanks @seberg this is a very clean fix

@seberg seberg deleted the fixup-array-func-C branch January 19, 2023 08:52
jjerphan added a commit to jjerphan/scikit-learn that referenced this pull request Jan 27, 2023
The behavior in NumPy slightly have changed with [1],
and the type of NumPy functions (like np.mean) isn't 'function'
but 'np._ArrayFunctionDispatcher'.

This makes some test fail.

This commit makes accepting 'np._ArrayFunctionDispatcher' as
an allowed type

[1]: numpy/numpy#23039
@jjerphan
Copy link

jjerphan commented Jan 27, 2023

Hi, it seems like this PR changed the type of NumPy functions from function to np._ArrayFunctionDispatcher.

We have seen this changed of behavior via scikit-learn/scikit-learn#25202 (see this log)

For now, we have this temporary fix: scikit-learn/scikit-learn#25498

PS: I do not know how the tone of this message was like, but I was probably annoyed by other reasons when writing it. Sorry for the annoyance.

@seberg
Copy link
Member Author

seberg commented Jan 27, 2023

I would might suggest to spell it as type(np.median) or so, since otherwise we should move this somewhere else (the problem there is if someone has the idea to make median a ufunc :), which is rather theoretical, but maybe there is another less ufunc-like function).

Yes, the previous PR changed it for much faster dispatching with keyword arguments especially. Does this break anything important to users (i.e. in released versions?) or just a small thing?

At least from my perspective there would have to be a decent amount of fallout to change it back, since it just mean unnecessary overhead. Note that the current logic also seems to be missing the built-in (C defined) methods in theory. (NumPy effectively doesn't have those, because since 1.17 all functions were wrapped into Python functions unless opted out).

@jjerphan
Copy link

Yes, the previous PR changed it for much faster dispatching with keyword arguments especially. Does this break anything important to users (i.e. in released versions?) or just a small thing?

For now, nothing breaks in our release version, it's more that I wanted to raise awareness about this change of behavior which impact downstream maintainers, such as scikit-learn's.

I do not suggest nor demand making changes (I have not spent time understanding NumPy internals in this regards), but would like to know if you want to address it in NumPy so that type inspection for (some) NumPy objects remains unchanged. 🙂

@seberg
Copy link
Member Author

seberg commented Jan 27, 2023

I do not think there is a way to guarantee that you get a Python function back other than wrapping everything or reverting. Meaning you would buy it at a cost of probably something like 50-100ns per call.
The current logic also for example doesn't allow ufuncs. So no, unless we have to go into firefighting 🔥🚨🚒 mode, I don't see a reason to change anything.

@jjerphan
Copy link

OK, then my interjection is irrelevant and can be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrong intersphinx prefix for many numpy functions
3 participants