summaryrefslogtreecommitdiff
path: root/numpy/core/_internal.py
diff options
context:
space:
mode:
authorStephan Hoyer <shoyer@gmail.com>2017-04-24 13:58:49 -0700
committerCharles Harris <charlesr.harris@gmail.com>2017-04-27 13:37:51 -0600
commit3272a860129a7192a0e499c59e273da3dd35d998 (patch)
tree3733e47e5130cc705bf2c9ef62af9dbe61f028a8 /numpy/core/_internal.py
parent256a8ae75fc36f7d4531557f9572a046508afa07 (diff)
downloadnumpy-3272a860129a7192a0e499c59e273da3dd35d998.tar.gz
ENH: Better error message for __array_ufunc__ not implemented
* ENH: Better error message for __array_ufunc__ not implemented New behavior: >>> import numpy as np >>> class Dummy: ... def __array_ufunc__(self, *args, **kwargs): ... return NotImplemented >>> np.negative(Dummy()) TypeError: operand type(s) do not implement __array_ufunc__( <ufunc 'negative'>, '__call__', <__main__.Dummy object at 0x1106df8d0>): 'Dummy' * check for null errmsg_formatter
Diffstat (limited to 'numpy/core/_internal.py')
-rw-r--r--numpy/core/_internal.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index f25159d8e..01741cd1a 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -645,3 +645,15 @@ class AxisError(ValueError, IndexError):
msg = "{}: {}".format(msg_prefix, msg)
super(AxisError, self).__init__(msg)
+
+
+def array_ufunc_errmsg_formatter(ufunc, method, *inputs, **kwargs):
+ """ Format the error message for when __array_ufunc__ gives up. """
+ args_string = ', '.join(['{!r}'.format(arg) for arg in inputs] +
+ ['{}={!r}'.format(k, v)
+ for k, v in kwargs.items()])
+ args = inputs + kwargs.get('out', ())
+ types_string = ', '.join(repr(type(arg).__name__) for arg in args)
+ return ('operand type(s) do not implement __array_ufunc__'
+ '({!r}, {!r}, {}): {}'
+ .format(ufunc, method, args_string, types_string))