numpy / numpy

The fundamental package for scientific computing with Python.
https://numpy.org
Other
27.97k stars 10.05k forks source link

DOC: Document if a function is affected by NPY_MAXARGS #27625

Open sergiopasra opened 1 week ago

sergiopasra commented 1 week ago

Issue with current documentation:

Some C-API functions have a limit in the number of arguments that can handle, given by the macro NPY_MAXARGS. But that limit is not described in the documentation of the function NpyIter_MultiNew. Other functions related with iterators might be affected, but you don't know it by reading the documentation.

Idea or request for content:

Those functions that have a limit in its arguments should have that limit documented.

m-clare commented 6 days ago

I'd like to contribute to this if possible. I took a look at which C-API functions called NpyIter_MultiNew with a variable number of arguments (there are a few that call with nop=2 or nop=3 and turned up the following functions:

I think the first one is just for a test, but would the other two require documentation?

ngoldbaum commented 6 days ago

@m-clare, no I don't think so, since none of the functions you linked to are in the public C API.

Of course we could probably internally document this in a docstring, but the issue is about functions in the public API, and I think internal documentation like that is a pretty low priority.

Functions are in the public API if their docstring is prepended with /*NUMPY_API or /*UFUNC_API.

One example of a function in the public API that is affected by this issue is PyArray_EinsteinSum, since the nop restriction is enforced at runtime and it uses NPY_MAXARGS in its definition.

m-clare commented 2 days ago

Ah ok; thanks for the tip about public functions. I had rgreped looking for NPY_MAXARGS but I actually thought PyArray_EinsteinSum was already covered, because it does have a value error:

    /* nop+1 (+1 is for the output) must fit in NPY_MAXARGS */
    if (nop >= NPY_MAXARGS) {
        PyErr_SetString(PyExc_ValueError,
                    "too many operands provided to einstein sum function");
        return NULL;
    }

I'll take a closer look at other functions in the public API that are missing any handling.

ngoldbaum commented 2 days ago

The runtime error is one thing, but I think the ask in the issue is that the limit should be explicitly included in the docs. Note the C API docs are entered manually in sphinx and are not generated from the docstrings.

sergiopasra commented 1 day ago

Yes, what I was asking is to document the limit in the public API docs, in both the Python and C API.