summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/release/numpy2_changes/23089.change.rst2
-rw-r--r--numpy/lib/function_base.py2
-rw-r--r--numpy/lib/tests/test_function_base.py7
3 files changed, 11 insertions, 0 deletions
diff --git a/doc/release/numpy2_changes/23089.change.rst b/doc/release/numpy2_changes/23089.change.rst
new file mode 100644
index 000000000..fbd87e0aa
--- /dev/null
+++ b/doc/release/numpy2_changes/23089.change.rst
@@ -0,0 +1,2 @@
+* ``np.gradient()`` now returns a tuple rather than list making the
+ return value immutable.
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 0ca3c21c1..1f39ebc71 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -1311,6 +1311,8 @@ def gradient(f, *varargs, axis=None, edge_order=1):
if len_axes == 1:
return outvals[0]
+ elif np._numpy2_behavior:
+ return tuple(outvals)
else:
return outvals
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index ecba35f2d..fb98a94cd 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -1217,6 +1217,13 @@ class TestGradient:
dfdx = gradient(f, x)
assert_array_equal(dfdx, [0.5, 0.5])
+ def test_return_type(self):
+ res = np.gradient(([1, 2], [2, 3]))
+ if np._numpy2_behavior:
+ assert type(res) is tuple
+ else:
+ assert type(res) is list
+
class TestAngle: