diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-04-06 16:32:39 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-06 16:32:39 -0600 |
commit | 70c060dfdab9e0c31eeec248ed40c26d3555033c (patch) | |
tree | 6fb60daf9c51a908c2dc1f92ec9358420f217f3e | |
parent | 1f9c30ef0f4f7a0c3e0227c3de681a11fcc24861 (diff) | |
parent | 6d950b383bff4ac2448b3f6a1e7573e7ea176051 (diff) | |
download | numpy-70c060dfdab9e0c31eeec248ed40c26d3555033c.tar.gz |
Merge pull request #10670 from eric-wieser/deprecate-sum-generator
DEP: Deprecate np.sum(generator)
-rw-r--r-- | doc/release/1.15.0-notes.rst | 5 | ||||
-rw-r--r-- | numpy/core/fromnumeric.py | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 6 |
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() |