Skip to content

Conversation

@seberg
Copy link
Member

@seberg seberg commented Jan 18, 2023

This wraps trapz into a proper python function, I could also just copy over all the relevant attributes. In case we change things so that trapz is a Python function anyway, the assert would fail.

EDIT: Changed the approach to just copy over the stuff that SciPy needs to "clone" the function.

@functools.wraps(_trapz_orig)
def trapz(y, x=None, dx=1.0, axis=-1):
# Hack so that SciPy has some python code it can steal
return _trapz_orig(y, x=x, dx=dx, axis=axis)
Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, this means we always pass defaults in __array_function__ (which is probably OK). We can also go back to *args, **kwargs. Or, I can just override all the attributes used by SciPy (__code__, and a couple of others).

Copy link
Member Author

Choose a reason for hiding this comment

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

Finding @dhomeier comment at astropy:

E           astropy.utils.exceptions.AstropyWarning: function 'trapz' is not known to astropy's Quantity. Will run it anyway, hoping it will treat ndarray subclasses correctly. Please raise an issue at https://github.com/astropy/astropy/issues.

The issue seems to be that with the proposed fix np.trapz identifies as _trapz_orig, which is not in function_helpers.SUBCLASS_SAFE_FUNCTIONS.

Wrapping things up seems to confuse astropy a bit (the function is just an entry-point).

So I will fix this up to just steal all the attributes so that pretending that this is a Python function works (what SciPy does) to allow creating a new Python function from the non-python version.

Copy link
Contributor

@dhomeier dhomeier Jan 20, 2023

Choose a reason for hiding this comment

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

Thanks @seberg, this version (fbb915f) resolves astropy/astropy#14287 out of the box!

Copy link
Member

@mattip mattip left a comment

Choose a reason for hiding this comment

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

LGTM. NIT: Should there be a test somewhere? Maybe not since this is a SciPy support shim that should go away in a few years.

@seberg
Copy link
Member Author

seberg commented Jan 20, 2023

NIT: Should there be a test somewhere? Maybe not since this is a SciPy support shim that should go away in a few years.

Dunno, if its worthwhile, but seemed easier to add one than ponder whether or not :)

@seberg seberg force-pushed the wrap-trapz branch 2 times, most recently from fbb915f to eff8a9b Compare January 20, 2023 12:13
This wraps `trapz` into a proper python function and then copies all
attributes expected on a Python function over from the "fake" version
to the real one.

This allows SciPy to pretend `trapz` is a Python function to create
their own version.
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Works like a charm, so LGTM too. Thanks @seberg!

@rgommers rgommers merged commit 707c3bf into numpy:main Jan 20, 2023
@seberg seberg deleted the wrap-trapz branch January 20, 2023 14:27
@neutrinoceros
Copy link
Contributor

thank you @seberg !

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.

BUG: numpy dev breaks scipy.stats

5 participants