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

sqrt raises AttributeError on unexpected input #7961

Closed
silvioricardoc opened this issue Aug 22, 2016 · 3 comments
Closed

sqrt raises AttributeError on unexpected input #7961

silvioricardoc opened this issue Aug 22, 2016 · 3 comments

Comments

@silvioricardoc
Copy link

>>> numpy.sqrt(9)
3.0
>>> numpy.sqrt(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: sqrt

I expected a TypeError or ValueError, and spent quite some time trying to understand why sqrt was "missing"...

@charris
Copy link
Member

charris commented Aug 22, 2016

None is treated as an object, which is why a sqrt attribute is looked for. Perhaps the error/error_message can be improved.

@timhoffm
Copy link
Contributor

timhoffm commented Jan 5, 2019

Same problem with sympy integers:

>>> import numpy as np
>>> x = Integer(10)
>>> np.sqrt(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Integer' object has no attribute 'sqrt'

Reported in matplotlib/matplotlib#13106.

A better error message would really help to find out what's going on.

@eric-wieser
Copy link
Member

+1 from me on improving the error message. Raising TypeError("Object loop of ufunc {} does not support object of type {} which has no {} method") if the attribute is absent would be helpful.

I'm not sure if we should also try and check is the attribute is callable.

Perhaps chaining the exception would be useful here

eric-wieser added a commit that referenced this issue Jan 13, 2019
ENH: chain additional exception on ufunc method lookup error
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

4 participants