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

Skip to content

DOC: Update __array__ copy keyword docs #26245

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 2 commits into from
Apr 10, 2024

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Apr 9, 2024

Hi @ngoldbaum,

As discussed in #25941 here's a few docs updates to avoid ambiguity with copy keyword argument.

@mtsokol mtsokol self-assigned this Apr 9, 2024
@mtsokol mtsokol force-pushed the copy-kw-docs-update branch from b71456a to 04dddd9 Compare April 9, 2024 16:43
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Apr 9, 2024
Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

All the changes LGTM, just one suggestion to improve clarity.

@jorisvandenbossche do the doc changes look OK to you given your recent experience updating __array__ implementations?


3. For any ``__array__`` method on a non-NumPy array-like object,
``dtype=None`` and ``copy=None`` keywords must be added to the signature -
this will work with older NumPy versions as well. If the keywords are actually
Copy link
Member

Choose a reason for hiding this comment

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

Just to make it a teeny bit clearer what will happen

Suggested change
this will work with older NumPy versions as well. If the keywords are actually
this will work with older NumPy versions as well (although older numpy versions will never pass in these keywords). If the keywords are actually

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@mtsokol mtsokol force-pushed the copy-kw-docs-update branch from e42d077 to ae1f038 Compare April 10, 2024 08:32
3. For any ``__array__`` method on a non-NumPy array-like object,
``dtype=None`` and ``copy=None`` keywords must be added to the signature -
this will work with older NumPy versions as well (although older numpy versions
will never pass in these keywords). If the keywords are actually used in the ``__array__``
Copy link
Contributor

Choose a reason for hiding this comment

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

It's only copy that is not passed in older versions?

Copy link
Member Author

@mtsokol mtsokol Apr 10, 2024

Choose a reason for hiding this comment

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

Ah right, should be (although older numpy versions will never pass in copy keyword). Updated!

Comment on lines 426 to 427
will never pass in these keywords). If the keywords are actually used in the ``__array__``
method implementation, then for:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
will never pass in these keywords). If the keywords are actually used in the ``__array__``
method implementation, then for:
will never pass in these keywords). If the keywords are added to the ``__array__``
signature, then for:

I still find "if the keywords are actually used in the implementation" a bit confusing, as it sounds that you are allowed to ignore them.

For dtype that is actually maybe true? (if the returned array doesn't match the requested dtype, I suppose that numpy will still do the cast?)

But once copy is added to the signature, it should always be honored in the implementation, right? (to ensure correct behaviour)

Copy link
Member Author

@mtsokol mtsokol Apr 10, 2024

Choose a reason for hiding this comment

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

Right, I agree that we should indicate that these arguments should be honored immediately (although copy=True is going to be passed to __array__ starting from 2.1 release). Done!

method implementation, then for:
* ``copy=True`` and any ``dtype`` value always return a new copy,
* ``copy=None`` create a copy if required (for example by ``dtype``),
* ``copy=False`` a copy must never be made. If ``dtype`` needs a copy then raise an exception.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* ``copy=False`` a copy must never be made. If ``dtype`` needs a copy then raise an exception.
* ``copy=False`` a copy must never be made. If a copy is needed to return a numpy array or satisfy ``dtype``, then raise an exception (``ValueError``).

Regardless of dtype a copy might be needed. And we can be more specific this should be a ValueError?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right - that's more precise. Done!

``dtype`` is not given or a new array of provided data type if ``dtype``
For ``dtype`` parameter it returns a new reference to self if
``dtype`` is not given or it matches array's data type.
A new array of provided data type is returned if ``dtype``
is different from the current data type of the array.
For ``copy`` parameter it returns a new reference to self if
``copy=False`` or ``copy=None`` and copying isn't enforced by ``dtype``
Copy link
Contributor

Choose a reason for hiding this comment

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

(not about this line, but can't comment on the lines more below)

Not strictly related to this clarification, but I think for implementors of __array__ it might be useful to add a link to https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method here. I know this is the docstring for the numpy __array__ method which has some different semantics (like returning self). But when googling for __array__ you can still end up here.

Copy link
Member

Choose a reason for hiding this comment

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

To do this you'll need to add a reference anchor to that subsection of the docs and then reference it here. You should be able to build the docs locally to double check that everything is wired up correctly. Let me know if you need a hand wrangling sphinx.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a reference anchor - works locally and in the CI artifact.

@mtsokol mtsokol force-pushed the copy-kw-docs-update branch from ae1f038 to 1f35b27 Compare April 10, 2024 09:32
@mtsokol mtsokol force-pushed the copy-kw-docs-update branch from 36b74ce to bff75cf Compare April 10, 2024 16:13
@mtsokol mtsokol requested a review from ngoldbaum April 10, 2024 16:33
@ngoldbaum ngoldbaum merged commit 6e8f6ca into numpy:main Apr 10, 2024
@ngoldbaum
Copy link
Member

Thanks @mtsokol!

@mtsokol mtsokol deleted the copy-kw-docs-update branch April 10, 2024 19:40
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 10, 2024
@charris charris changed the title DOC: Update __array__ copy keyword docs DOC: Update __array__ copy keyword docs Apr 10, 2024
@jorisvandenbossche
Copy link
Contributor

Thanks @mtsokol !

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.

4 participants