Skip to content

API: register NEP 35 functions as array_functions - #27490

Merged
seberg merged 4 commits into
numpy:mainfrom
neutrinoceros:api/register_nep35_functions
Nov 22, 2024
Merged

seberg merged 4 commits into
numpy:mainfrom
neutrinoceros:api/register_nep35_functions

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Close gh-27451
I aimed for a small footprint. If the additional responsibilities to add_newdoc are not acceptable it should be possible to avoid it, at the cost of more repetitions.
I also wasn't able to run the test suite locally yet, so I'm opening as a draft for now.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks quite nice! Since this all is (and was) a little fragile, I think it might be good to add a test that these functions are in fact in the list. Not completely sure what is the best place, but perhaps a new np.testing.tests.test_overrides.py?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

I wasn't sure how to test this. Are you suggesting a test with a hard-coded list of expected results ?

@mhvk

mhvk commented Oct 4, 2024

Copy link
Copy Markdown
Contributor

I wasn't sure how to test this. Are you suggesting a test with a hard-coded list of expected results ?

No, more a spot check that, e.g., np.asarray is included. Basically to guard against the implementation being changed.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Gotcha. I did something in between. Let me know if you think it's too much.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice! I'll approve but will let @seberg have a look too.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but the test needs to move. Other comments are tweaks, but don't have a quick idea on how and it's also fine as is.

Comment thread numpy/testing/tests/test_overrides.py Outdated
np.frombuffer, np.fromfile, np.fromiter, np.fromstring,
np.zeros,
}
assert nep35_C_functions.issubset(array_functions)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

testing is the folder for testing utils, not the folder for tests. This function needs to be in _core/tests/test_overrides.py.

I would expect that the file already has parametrizations for all (or at least most) like= functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks ! indeed, the test can easily be rewritten (at least partially) in this module. I just moved it and rewrote it in a redundant style for now, as I see two ways to trim off the redundancy and would like your opinion first:

  • only check for the subset that's already parametrized
  • expand the existing parametrization to cover the whole (current) set
    what option would you prefer ?

new = getattr(__import__(place, globals(), {}, [obj]), obj)
if isinstance(doc, str):
if "${ARRAY_FUNCTION_LIKE}" in doc:
doc = overrides.get_array_function_like_doc(new, doc)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure I love the implicit nature. I think I would prefer a kwarg for this. But I suppose that may be a matter of opinion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

happy to go with your prefered style. What would you suggest we name the argument ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just go with something that seems sensible to you, I promise not to bike-shed ;).

@neutrinoceros neutrinoceros Oct 8, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just to be clear though, did you mean a kwarg on overrides.get_array_function_like_doc or add_newdoc ? (I assume the former ?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I meant add_newdoc so that the registration as a like= function isn't based on finding ${ARRAY_FUNCTION_LIKE} in the documentation.
Somehow, I don't love it, but it doesn't matter much. Let's just put it in. Marten also fixed another thing around like= so this will be nice to have.

Comment thread numpy/_core/overrides.py
"${ARRAY_FUNCTION_LIKE}",
array_function_like_doc,
)
def get_array_function_like_doc(public_api, docstring_template=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be nice to mention the registration in the name. But if we don't have a quick idea, no worries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agreed, I don't love that this function does two things, happy to take suggestions for a better name or pattern, but I don't have one now.

@neutrinoceros
neutrinoceros force-pushed the api/register_nep35_functions branch from a3d59cc to 2596bb6 Compare October 8, 2024 13:50
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@seberg just in case you missed it: I'm waiting on your reply on this thread #27490 (comment) (no rush though)

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: overridable functions from NEP 35 do not show up in ARRAY_FUNCTIONS

3 participants