From bfe0921f9bdd6b347982a9c998b8b1890040ba5b Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sat, 27 Oct 2018 12:23:15 -0700 Subject: DEP: deprecate passing a generator to stack functions Fixes gh-12263 We can't support generators with dispatch for ``__array_function__``. --- numpy/core/shape_base.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'numpy/core/shape_base.py') diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 71a23f438..4cb7a30f4 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -5,6 +5,8 @@ __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack', import functools import operator +import types +import warnings from . import numeric as _nx from . import overrides @@ -204,11 +206,23 @@ def atleast_3d(*arys): return res -def _vstack_dispatcher(tup): - return tup +def _arrays_for_stack_dispatcher(arrays, stacklevel=4): + if isinstance(arrays, types.GeneratorType): + warnings.warn('arrays to stack should be passed as a sequence, not a ' + 'generator. Support for generators is deprecated as of ' + 'NumPy 1.16 and will raise an error in the future. ' + 'Note also that dispatch with __array_function__ is not ' + 'supported when passing arrays as a generator.', + FutureWarning, stacklevel=stacklevel) + return () + return arrays -@array_function_dispatch(_vstack_dispatcher) +def _vhstack_dispatcher(tup): + return _arrays_for_stack_dispatcher(tup) + + +@array_function_dispatch(_vhstack_dispatcher) def vstack(tup): """ Stack arrays in sequence vertically (row wise). @@ -264,11 +278,7 @@ def vstack(tup): return _nx.concatenate([atleast_2d(_m) for _m in tup], 0) -def _hstack_dispatcher(tup): - return tup - - -@array_function_dispatch(_hstack_dispatcher) +@array_function_dispatch(_vhstack_dispatcher) def hstack(tup): """ Stack arrays in sequence horizontally (column wise). @@ -325,6 +335,7 @@ def hstack(tup): def _stack_dispatcher(arrays, axis=None, out=None): + arrays = _arrays_for_stack_dispatcher(arrays, stacklevel=6) for a in arrays: yield a if out is not None: -- cgit v1.2.1