Skip to content

sqrt raises AttributeError on unexpected input #7961

Closed

Description

@silvioricardoc
>>> 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"...

Activity

  1. charris commented on Aug 22, 2016

    @charris
    Member

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

  2. timhoffm commented on Jan 5, 2019

    @timhoffm
    Contributor

    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.

  3. eric-wieser commented on Jan 5, 2019

    @eric-wieser
    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

  4. added a commit that references this issue on Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions