Skip to content

Commit 5ba8c96

Browse files
authored
DEP: show warning when np.maximum receives more than 2 inputs (#29052)
Deprecate not using `out=` for np.minimum and maximum due to it being a common confusion.
1 parent cbf648e commit 5ba8c96

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Positional ``out`` argument to `np.maximum`, `np.minimum` is deprecated
2+
-----------------------------------------------------------------------
3+
Passing the output array ``out`` positionally to `numpy.maximum` and
4+
`numpy.minimum` is deprecated. For example, ``np.maximum(a, b, c)`` will
5+
emit a deprecation warning, since ``c`` is treated as the output buffer
6+
rather than a third input.
7+
8+
Always pass the output with the keyword form, e.g.
9+
``np.maximum(a, b, out=c)``. This makes intent clear and simplifies
10+
type annotations.

numpy/_core/src/umath/ufunc_object.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "mapping.h"
6666
#include "npy_static_data.h"
6767
#include "multiarraymodule.h"
68+
#include "number.h"
6869

6970
/********** PRINTF DEBUG TRACING **************/
7071
#define NPY_UF_DBG_TRACING 0
@@ -4368,6 +4369,21 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
43684369
Py_INCREF(tmp);
43694370
PyTuple_SET_ITEM(full_args.out, i-nin, tmp);
43704371
}
4372+
4373+
/* Extra positional args but no keywords */
4374+
/* DEPRECATED NumPy 2.4, 2025-08 */
4375+
if ((PyObject *)ufunc == n_ops.maximum || (PyObject *)ufunc == n_ops.minimum) {
4376+
4377+
if (DEPRECATE(
4378+
"Passing more than 2 positional arguments to np.maximum and np.minimum "
4379+
"is deprecated. If you meant to use the third argument as an output, "
4380+
"use the `out` keyword argument instead. If you hoped to work with "
4381+
"more than 2 inputs, combine them into a single array and get the extrema "
4382+
"for the relevant axis.") < 0) {
4383+
return NULL;
4384+
}
4385+
}
4386+
43714387
if (all_none) {
43724388
Py_SETREF(full_args.out, NULL);
43734389
}

numpy/_core/tests/test_deprecations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,12 @@ def use_suppress_warnings():
535535
sup.filter(RuntimeWarning, 'invalid value encountered in divide')
536536

537537
self.assert_deprecated(use_suppress_warnings)
538+
539+
540+
class TestTooManyArgsExtremum(_DeprecationTestCase):
541+
# Deprecated in Numpy 2.4, 2025-08, gh-27639
542+
message = "Passing more than 2 positional arguments to np.maximum and np.minimum "
543+
544+
@pytest.mark.parametrize("ufunc", [np.minimum, np.maximum])
545+
def test_extremem_3_args(self, ufunc):
546+
self.assert_deprecated(ufunc, args=(np.ones(1), np.zeros(1), np.empty(1)))

0 commit comments

Comments
 (0)