Skip to content

accessing undefined field raises incorrect Exception #7641

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

Closed
andsok75 opened this issue May 15, 2016 · 8 comments
Closed

accessing undefined field raises incorrect Exception #7641

andsok75 opened this issue May 15, 2016 · 8 comments

Comments

@andsok75
Copy link

In numpy 1.11.0, trying to access an undefined field of a structured array raises ValueError, rather than IndexError as in version 1.8

a = np.empty(1,dtype=[('f','u4')])
a['g']

yields

ValueError Traceback (most recent call last)
in ()
----> 1 a['g']

ValueError: no field of name g

Is that a feature? In any case, shouldn't it raise lookup error instead.

@charris
Copy link
Member

charris commented May 15, 2016

IIRC, it was a deliberate change. Do you mean KeyError?

@andsok75
Copy link
Author

I had a quick look at the recent release notes, but I could not find this change documented there. I'm using Python 2.7

@ahaldane
Copy link
Member

Are you sure? I just checked out 1.8.0 and it gives ValueError. 1.8.3 and 1.9.3 also give ValueError.

I made a couple PRs starting in 1.10 which modified a lot of this code, but I made sure to leave it as ValueError to preserve compatibility. As you can see in #5636, for example, I wanted to change it to KeyError, however I held back because of backward compatibility. There are a number of unit tests that explicitly check for ValueError.

@andsok75
Copy link
Author

Interesting. Perhaps there is a bug on our end, that only manifests with the older version 1.8.

@ahaldane
Copy link
Member

Curious. What does numpy.__version__ say? And what is the exact error message when you get the IndexError?

@david-jennings
Copy link

On numpy 1.11.0, the following code raises ValueError as expected, but with numpy 1.8.2 it raises IndexError:

import numpy as np
print np.__version__
records = np.empty(5, dtype=[('a', 'f8')] )
for record in records: record['foo']

yields

1.8.2
Traceback (most recent call last):
File "", line 1, in
IndexError: invalid index

$ python --version
Python 2.7.6

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"

@ahaldane
Copy link
Member

Oh OK, that makes sense. What actually changed behavior is indexing a "structured scalar" rather than an array (very subtle, I know!). Indeed, in #5947 I changed it so structured scalars behave like structured arrays, so they give the same errors:

>>> arr = np.empty(5, dtype=[('a', 'f8')] )
>>> scalar = arr[0]
>>> arr['b']
ValueError: no field of name b
>>> scalar['b']  # this is what changed in 1.10
ValueError: no field of name b

I didn't think to put the change in the release notes. @charris is it worth putting a belated note in the 1.11 notes?

@seberg
Copy link
Member

seberg commented May 8, 2019

I think we can close this issue. That the exception is not ideal is also mentioned in gh-8519.

@seberg seberg closed this as completed May 8, 2019
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

No branches or pull requests

5 participants