Skip to content

Commit 696f11a

Browse files
committed
ENH: show warning when np.maximum receives more than 2 inputs
1 parent 7d2e441 commit 696f11a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

numpy/_core/src/umath/ufunc_object.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef struct {
8888
provided, then this is NULL. */
8989
} ufunc_full_args;
9090

91+
extern PyUFuncObject *UFUNC_MAXIMUM;
9192

9293
/* ---------------------------------------------------------------- */
9394

@@ -4325,7 +4326,21 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
43254326
ufunc_get_name_cstr(ufunc) , nin, nop, len_args);
43264327
goto fail;
43274328
}
4328-
4329+
// 2) Only when there *are* extra pos-args *and* no keywords...
4330+
if (len_args > nin && (kwnames==NULL || PyTuple_Size(kwnames)==0)) {
4331+
/* 3) ...then check if it’s the "maximum" ufunc */
4332+
// if (strcmp(ufunc->name, "maximum") == 0) {
4333+
if (ufunc == UFUNC_MAXIMUM) {
4334+
PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
4335+
"Passing more than 2 positional arguments to np.maximum "
4336+
"is deprecated; use out=keyword or np.maximum.reduce.");
4337+
PyErr_Format(PyExc_TypeError,
4338+
"np.maximum() takes exactly 2 input arguments (%zd given).",
4339+
len_args);
4340+
goto fail;
4341+
}
4342+
}
4343+
43294344
/* Fetch input arguments. */
43304345
full_args.in = PyArray_TupleFromItems(ufunc->nin, args, 0);
43314346
if (full_args.in == NULL) {

numpy/_core/src/umath/umathmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141

4242
static PyUFuncGenericFunction pyfunc_functions[] = {PyUFunc_On_Om};
43+
PyUFuncObject *UFUNC_MAXIMUM = NULL;
4344

4445
static int
4546
object_ufunc_type_resolver(PyUFuncObject *ufunc,
@@ -278,6 +279,7 @@ int initumath(PyObject *m)
278279
if (_PyArray_SetNumericOps(d) < 0) {
279280
return -1;
280281
}
282+
UFUNC_MAXIMUM = (PyUFuncObject *)PyDict_GetItemString(d, "maximum");
281283

282284
PyDict_SetItemString(d, "conj", s);
283285
PyDict_SetItemString(d, "mod", s2);

numpy/_core/tests/test_umath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ def test_strided_array(self):
24202420
assert_equal(np.maximum(arr1[:4:], arr2[::2]), np.array([-2.0, np.nan, 10.0, 1.0]))
24212421
assert_equal(np.maximum(arr1[::3], arr2[:3:]), np.array([-2.0, 0.0, np.nan]))
24222422
assert_equal(np.maximum(arr1[:6:2], arr2[::3], out=out[::3]), np.array([-2.0, 10., np.nan]))
2423+
24232424
assert_equal(out, out_maxtrue)
24242425

24252426
def test_precision(self):
@@ -2443,6 +2444,9 @@ def test_precision(self):
24432444
assert_equal(np.maximum([v1], [v2]), [expected])
24442445
assert_equal(np.maximum.reduce([v1, v2]), expected)
24452446

2447+
def test_maximum_too_many_args(self):
2448+
with pytest.raises(TypeError, match=r"np\.maximum\(\) takes exactly 2 input arguments \(3 given\)"):
2449+
np.maximum(1, 2, 3)
24462450

24472451
class TestMinimum(_FilterInvalids):
24482452
def test_reduce(self):

0 commit comments

Comments
 (0)