Skip to content

BUG: Missing signatures for ufuncs #30095

@jacobtomlinson

Description

@jacobtomlinson

Describe the issue:

Numpy ufuncs do not have valid signatures.

Reproduce the code example:

import numpy as np
import inspect

inspect.signature(np.ufunc.accumulate)
inspect.signature(np.ufunc.at)
inspect.signature(np.ufunc.outer)
inspect.signature(np.ufunc.reduce)
inspect.signature(np.ufunc.reduceat)
inspect.signature(np.ufunc.resolve_dtypes)

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[28], line 1
----> 1 inspect.signature(np.ufunc.accumulate)

File ~/miniconda3/lib/python3.10/inspect.py:3254, in signature(obj, follow_wrapped, globals, locals, eval_str)
   3252 def signature(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False):
   3253     """Get a signature object for the passed callable."""
-> 3254     return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
   3255                                    globals=globals, locals=locals, eval_str=eval_str)

File ~/miniconda3/lib/python3.10/inspect.py:3002, in Signature.from_callable(cls, obj, follow_wrapped, globals, locals, eval_str)
   2998 @classmethod
   2999 def from_callable(cls, obj, *,
   3000                   follow_wrapped=True, globals=None, locals=None, eval_str=False):
   3001     """Constructs Signature for the given callable object."""
-> 3002     return _signature_from_callable(obj, sigcls=cls,
   3003                                     follow_wrapper_chains=follow_wrapped,
   3004                                     globals=globals, locals=locals, eval_str=eval_str)

File ~/miniconda3/lib/python3.10/inspect.py:2468, in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, globals, locals, eval_str, sigcls)
   2463     return _signature_from_function(sigcls, obj,
   2464                                     skip_bound_arg=skip_bound_arg,
   2465                                     globals=globals, locals=locals, eval_str=eval_str)
   2467 if _signature_is_builtin(obj):
-> 2468     return _signature_from_builtin(sigcls, obj,
   2469                                    skip_bound_arg=skip_bound_arg)
   2471 if isinstance(obj, functools.partial):
   2472     wrapped_sig = _get_signature_of(obj.func)

File ~/miniconda3/lib/python3.10/inspect.py:2275, in _signature_from_builtin(cls, func, skip_bound_arg)
   2273 s = getattr(func, "__text_signature__", None)
   2274 if not s:
-> 2275     raise ValueError("no signature found for builtin {!r}".format(func))
   2277 return _signature_fromstr(cls, func, s, skip_bound_arg)

ValueError: no signature found for builtin <method 'accumulate' of 'numpy.ufunc' objects>

Python and NumPy Versions:

2.2.6
3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ]

Runtime Environment:

No response

Context for the issue:

In Dask we fixed a bug where dask.array.cumprod wasn't passing the dtype down to the underlying function calls. To do this we inspect the funcs passed in for a dtype kwarg, and if set to use a partial to pass the dtype on.

When used with numpy ufuncs we are now seeing exceptions because the methods do not have valid signatures so we can't check if they have a dtype kwarg. See dask/dask#12117

We could special case this in Dask as all ufuncs have a dtype kwarg, but it would be better if we could inspect them to discover this at runtime.

#23993 is related

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions