summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/release/1.15.0-notes.rst5
-rw-r--r--numpy/core/fromnumeric.py6
-rw-r--r--numpy/core/tests/test_deprecations.py6
3 files changed, 17 insertions, 0 deletions
diff --git a/doc/release/1.15.0-notes.rst b/doc/release/1.15.0-notes.rst
index cff75c477..46ffb7913 100644
--- a/doc/release/1.15.0-notes.rst
+++ b/doc/release/1.15.0-notes.rst
@@ -38,6 +38,11 @@ Deprecations
* `np.ma.load`, `np.ma.dump` - these functions already failed on python 3,
when called with a string.
+* Giving a generator to `np.sum` is now deprecated. This was undocumented, but
+ worked. Previously, it would calculate the sum of the generator expression.
+ In the future, it might return a different result. Use `np.sum(np.from_iter(generator))`
+ or the built-in Python `sum` instead.
+
Future Changes
==============
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index 5f1aadbf5..948c2139d 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -1900,6 +1900,12 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
"""
if isinstance(a, _gentype):
+ # 2018-02-25, 1.15.0
+ warnings.warn(
+ "Calling np.sum(generator) is deprecated, and in the future will give a different result. "
+ "Use np.sum(np.from_iter(generator)) or the python sum builtin instead.",
+ DeprecationWarning, stacklevel=2)
+
res = _sum_(a)
if out is not None:
out[...] = res
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index 2c2900e6c..cc4c058d1 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -481,5 +481,11 @@ class TestBincount(_DeprecationTestCase):
self.assert_deprecated(lambda: np.bincount([1, 2, 3], minlength=None))
+class TestGeneratorSum(_DeprecationTestCase):
+ # 2018-02-25, 1.15.0
+ def test_generator_sum(self):
+ self.assert_deprecated(np.sum, args=((i for i in range(5)),))
+
+
if __name__ == "__main__":
run_module_suite()