Skip to content

BUG: numpy.isscalar thinks that memoryview is always a scalar #8538

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

Open
sashkab opened this issue Jan 27, 2017 · 5 comments
Open

BUG: numpy.isscalar thinks that memoryview is always a scalar #8538

sashkab opened this issue Jan 27, 2017 · 5 comments
Labels
00 - Bug 57 - Close? Issues which may be closable unless discussion continued

Comments

@sashkab
Copy link

sashkab commented Jan 27, 2017

isscalar doesn't support memoryview object:

>>> import numpy
>>> numpy.__version__
'1.13.0.dev0+c5e1773'
>>> numpy.isscalar(memoryview(numpy.array([])))
True
@sashkab sashkab changed the title BUG: numpy.iscalar thinks that memoryview is always a scalar BUG: numpy.isscalar thinks that memoryview is always a scalar Jan 27, 2017
@seberg
Copy link
Member

seberg commented Jan 27, 2017

Aaah, a bit weird and probably wrong. It would seem (from the code) that the thought it that a buffer/memoryview is something like a void python object (which to some degree may even be true, though I have my doubts). Just to note though, its all in numpy/core/numerictypes.py....

@eric-wieser
Copy link
Member

Worse yet, gh-9898 means that this only happens on python 3

@OmerJog
Copy link

OmerJog commented May 15, 2019

Are there any plans to fix this soon?

@eric-wieser
Copy link
Member

From the docs of isscalar

In almost all cases np.ndim(x) == 0 should be used instead of this function, as that will also return true for 0d arrays.

I think that returning false on 0d memoryviews is actually correct, since it's consistent with 0d arrays.

@rgommers
Copy link
Member

# 0-D and 1-D arrays
>>> x0 = np.array(0)
>>> x1 = np.array([])
>>> np.isscalar(x0)
False
>>> np.isscalar(x1)
False

# A NumPy scalar
>>> dt0 = np.float64(0)
>>> np.isscalar(dt0)
True

# When going through `memoryview` for all those things
>>> np.isscalar(memoryview(dt0))
True
>>> np.isscalar(memoryview(x0))
True
>>> np.isscalar(memoryview(x1))
True

# Using `.ndim`:
>>> x0.ndim
0
>>> x1.ndim
1
>>> dt0.ndim
0
>>> memoryview(x0).ndim
0
>>> memoryview(x1).ndim
1
>>> memoryview(dt0).ndim
0

So conclusion: yes, memorview combined with isscalar indeed results in an inconsistency. However, .ndim is preferred and does do the correct thing. The isscalar docstring already discusses why .ndim == 0 should be preferred.

Not sure that this is worth keeping open; isscalar should perhaps get an even stronger warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

6 participants