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

Add __int__, __float__, and __bool__ to the array object #127

Merged
merged 5 commits into from
Feb 21, 2021
Merged

Conversation

asmeurer
Copy link
Member

These are only required for dimension-0 arrays of a matching dtype.

See numpy/numpy#10404 for why it is not a good idea to require this for higher dimension size 1 arrays.

These are only required for dimension-0 arrays of a matching dtype.

- **out**: _<bool>_

- a Python `bool` object representing the single element of the array `x`.
Copy link
Contributor

Choose a reason for hiding this comment

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

What should the behavior be if the array does not meet the requirement (no zero-dimensional, or different type)?.

Does the standard care whether ValueError or TypeError are raised?

NumPy can raise both:

In [2]: np.__version__
Out[2]: '1.20.0'

In [3]: X = np.zeros((2,2),dtype='d')

In [4]: X.__bool__
Out[4]: <method-wrapper '__bool__' of numpy.ndarray object at 0x2b67bdea50f0>

In [5]: X.__bool__()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-2acecd4ea032> in <module>
----> 1 X.__bool__()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [6]: X.__float__()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-78817c2ae92b> in <module>
----> 1 X.__float__()

TypeError: only size-1 arrays can be converted to Python scalars

Copy link
Member

Choose a reason for hiding this comment

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

Does the standard care whether ValueError or TypeError are raised?

In general we've refrained from specifying this, because (a) error handling is broad, (b) existing libraries are very inconsistent, and (c) there are situations where error handling strategy for accelerators may be different from that on CPU on purpose.

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.

LGTM. Fixed a merge conflict, in it goes. Thanks @asmeurer, and thanks @kgryte and @oleksandr-pavlyk for reviewing.

@rgommers rgommers merged commit ed19a40 into main Feb 21, 2021
@rgommers rgommers deleted the __int__ branch February 21, 2021 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants