Skip to content
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

API, DEP: Move ufunc signature parsing to the start #18718

Merged
merged 4 commits into from Apr 12, 2021

Conversation

seberg
Copy link
Member

@seberg seberg commented Apr 2, 2021

This may have slight affects on users, see release notes.

Mainly, we have to parse the type tuple up-front (because we need
to replace the current type resolution). Since we must de-facto
replace the current type resolution, I do not see why we should
duplicate code for the odd possibility of someone actually calling
ufunc.type_resolver() with a typetup that is not a actually a
type tuple.

This commit also deprecates signature="l" as meaning (normally)
the same as dtype="l".

@seberg seberg force-pushed the simplify-typetup branch 2 times, most recently from 3374c37 to c85fcd2 Compare April 2, 2021 20:44
@seberg seberg added 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. 07 - Deprecation labels Apr 2, 2021
@@ -4,7 +4,7 @@
np.sin([1, 2, 3])
np.sin(1, out=np.empty(1))
np.matmul(np.ones((2, 2, 2)), np.ones((2, 2, 2)), axes=[(0, 1), (0, 1), (0, 1)])
np.sin(1, signature="D")
np.sin(1, signature="D->D")
Copy link
Member Author

Choose a reason for hiding this comment

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

@BvB93 I assme that this doesn't need any further changes related to typing, since typing probably doesn't make a difference between the one dtype vs. all dtypes signature versions?

Copy link
Member

Choose a reason for hiding this comment

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

Naah, it's fine. signature is currently typed as taking either a string or tuple of strings, while this PR only changes which specific strings are allowed, correct?

signature: Union[str, Tuple[str]] = ...,

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.

Since this touched the ufuncs, I thought I would have look, but didn't really get to any depth. Still, the few comments may be useful. Overall, it certainly makes sense!

PyErr_SetString(PyExc_TypeError,
"cannot specify both 'sig' and 'signature'");
Py_SETREF(*out_typetup, NULL);
*out_signature = NULL;
return -1;
}
Py_INCREF(sig_obj);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good spot. Removed.

}
}
if (nonecount == n) {
PyErr_SetString(PyExc_ValueError,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it is unambiguous, maybe just ignore an all-None type tuple? I think we do the same with out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree... The old code deferring parsing to the end had to do this, because by the time it was parsed the normal resolver information would be lost. But if we parse it up-front (which seems clearly better), there is not really a reason not to support it.

continue;
}
if (!PyArray_DescrCheck(item)) {
/* bad type tuple (maybe not normalized correctly?) */
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't bad items lead to a warning or exception?

Copy link
Member Author

Choose a reason for hiding this comment

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

It drops through to the default resolver, which will raise an error. Tweaked the comment.

This may have slight affects on users, see release notes.

Mainly, we have to parse the type tuple up-front (because we need
to replace the current type resolution).  Since we _must_ de-facto
replace the current type resolution, I do not see why we should
duplicate code for the odd possibility of someone actually calling
`ufunc.type_resolver()` with a `typetup` that is not a actually a
type tuple.

This commit also deprecates `signature="l"` as meaning (normally)
the same as `dtype="l"`.
@seberg
Copy link
Member Author

seberg commented Apr 8, 2021

Updated as per meeting yesterday:

  1. I now raise an error instead of giving a UserWarning, this is a "big break" for the rare users who run into dtype=... with actual dtype instances that do not use the default byte-order or type unit.
  2. Edited the NEP to note the type-tuple "break" so to speak. Using it just shouldn't be reliable anymore, but nobody is using it hopefully.

@charris charris changed the title API,DEP: Move ufunc signature parsing to the start API, DEP: Move ufunc signature parsing to the start Apr 12, 2021
@charris
Copy link
Member

charris commented Apr 12, 2021

Let's get this in for some testing. The coverage defects look like they could be fixed by added tests, but that can be left to a later PR.

@charris charris merged commit 635b3ad into numpy:main Apr 12, 2021
@charris
Copy link
Member

charris commented Apr 12, 2021

Thanks Sebastian.

@seberg
Copy link
Member Author

seberg commented Apr 12, 2021

Thanks for the reviews. Hopefully nobody will notice :).

I think most coverage defects are due to the move: The "old code path" can only be reached by calling the resolver directly, which nobody should be doing. (Test can be added though, just has to be done on the C level)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
07 - Deprecation 30 - API 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants