Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Make ndarray.__array_finalize__ a callable no-op #20766

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 5 commits into from
Jan 11, 2022

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Jan 7, 2022

Inspired by #20217, this gives ndarray a proper __array_finalize__ method (which does nothing) rather than just set it to None. This means the typing that was added can be consistent with the documentation for subclasses (the only place for which it is useful). It also means subclasses can safely do super().__array_finalize__(obj).

One question is whether having __array_finalize__ = None should be deprecated (I doubt many subclasses would set that explicitly, but right now the code still checks).

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Great, thanks, looks all good to me. I suppose someone might find a way to fool fetching the attribute from the type, but I don't really see this plausible in practice (and it is generally the correct thing for a dunder).

I assume we won't backport this, so @BvB93 maybe you can finalize how this should look like with respect to the typing issues?

@mhvk
Copy link
Contributor Author

mhvk commented Jan 7, 2022

@seberg - what do you think regarding deprecating __array_finalize__ = None? It would mean a bit of simplification in other code (e.g., stride_tricks). I can quite easily add that, but I guess we can also decide it is not worth the trouble.

@seberg
Copy link
Member

seberg commented Jan 7, 2022

I am happy to deprecate it, but I it needs to go through a proper deprecation. Implementing __array_finalize__ = None AFAIK, is not used anywhere (unlike __array_priority__ = None).

@mhvk
Copy link
Contributor Author

mhvk commented Jan 7, 2022

Yes, I meant raising a DeprecationWarning when __array_finalize__ is None - since the use is so nicely isolated, should be easy -- I'll add an extra commit in a little bit.

@BvB93
Copy link
Member

BvB93 commented Jan 7, 2022

I assume we won't backport this, so @BvB93 maybe you can finalize how this should look like with respect to the typing issues?

Something like this should do the trick here:

--- a/numpy/__init__.pyi
+++ b/numpy/__init__.pyi
@@ -1495,8 +1495,7 @@ def __array_function__(
-    @property
-    def __array_finalize__(self) -> None: ...
+    def __array_finalize__(self, obj: NDArray[Any], /) -> None: ...

@mhvk
Copy link
Contributor Author

mhvk commented Jan 8, 2022

Note that obj can also be None (for subclass initialization). I'll try to have a stab and will let you correct me!

mhvk added 2 commits January 8, 2022 17:44
This helps subclasses, who can now do super() in their own
implementation.
In the process, __array_finalized__ is looked up on the subclass
instead of the instance, which is more like python for methods like these.
It cannot make a difference, since the instance is created in the
same routine, so the instance method is guaranteed to be the same as
that on the class.
@mhvk mhvk force-pushed the ndarray_array_finalize branch from 7548232 to 485d45e Compare January 8, 2022 22:44
@mhvk
Copy link
Contributor Author

mhvk commented Jan 8, 2022

OK, I pushed the change, plus the deprecationwarning for __array_finalize__ = None, and what I hope is the correct typing info. Thanks all!

@mhvk mhvk force-pushed the ndarray_array_finalize branch from 485d45e to ecc7339 Compare January 8, 2022 22:46
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Thanks for the followups, looks good. We probably should add a second release note for the deprecation as well to be complete. Could move the deprecation tests since we do that for almost all (but I don't care too much about it).
If you like, I can do those final touches.

"should be done is deprecated. Instead, just inherit from "
"ndarray or, if that is not possible, explicitly set to "
"ndarray.__array_function__; this will raise a TypeError "
"in the future.") < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"in the future.") < 0) {
"in the future. (Deprecated since NumPy 1.23)") < 0) {

with pytest.warns(DeprecationWarning,
match="__array_finalize__ = None"):
a = np.array(1).view(NoFinalize)
assert isinstance(a, NoFinalize)
Copy link
Member

Choose a reason for hiding this comment

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

Could move to test_deprecations or add a test for when it is raised (the DeprecationTestCase always does this). But not super important, the ref-counting is obviously correct and that is the easiest error.

@mhvk mhvk force-pushed the ndarray_array_finalize branch from ecc7339 to 854dafa Compare January 9, 2022 22:35
@mhvk
Copy link
Contributor Author

mhvk commented Jan 9, 2022

@seberg - all makes sense. I pushed the changes (added to the deprecation commit, now the last one)

@seberg
Copy link
Member

seberg commented Jan 11, 2022

Pushed a brief deprecation release note as well, planning on merging tomorrow, thanks.

@mhvk
Copy link
Contributor Author

mhvk commented Jan 11, 2022

@seberg - I thought I had added a note myself, but I now see I forgot to add it (too used to git commit -a I guess...). Anyway, yours is better than what I had, so let's go with that!

@seberg
Copy link
Member

seberg commented Jan 11, 2022

Ah, was a bit surprised :). Anyway thanks for taking care of the deprecation as well! Lets put this in, if you are happy.

@seberg seberg merged commit acf33eb into numpy:main Jan 11, 2022
@mhvk mhvk deleted the ndarray_array_finalize branch January 11, 2022 15:35
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.

DOC: __array_finalize__ arguments inconsistent between docs and interface
3 participants