From e03d8ff35985bb3ce1bdb159793303c6b7b297fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Mon, 28 Aug 2023 15:50:08 +0200 Subject: [PATCH 1/3] API: Update lib.shape_base namespace --- numpy/__init__.py | 22 +++++++++---------- numpy/__init__.pyi | 3 +-- numpy/_expired_attrs_2_0.py | 3 ++- numpy/core/tests/test_deprecations.py | 2 +- numpy/lib/__init__.py | 8 +++---- numpy/lib/__init__.pyi | 19 ---------------- .../{shape_base.py => _shape_base_impl.py} | 15 +++++++++++-- .../{shape_base.pyi => _shape_base_impl.pyi} | 0 numpy/lib/tests/test_shape_base.py | 4 ++-- numpy/testing/tests/test_utils.py | 2 +- numpy/tests/test_public_api.py | 2 -- numpy/typing/tests/data/reveal/shape_base.pyi | 3 --- 12 files changed, 35 insertions(+), 48 deletions(-) rename numpy/lib/{shape_base.py => _shape_base_impl.py} (99%) rename numpy/lib/{shape_base.pyi => _shape_base_impl.pyi} (100%) diff --git a/numpy/__init__.py b/numpy/__init__.py index 9ee3c7bc7e97..ac1099b675e5 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -183,19 +183,13 @@ from . import lib from .lib import ( - DataSource, apply_along_axis, apply_over_axes, - array_split, c_, column_stack, diag_indices, - diag_indices_from, dsplit, dstack, - emath, expand_dims, fill_diagonal, - fromregex, get_array_wrap, genfromtxt, - hsplit, index_exp, ix_, kron, load, loadtxt, - mgrid, ndenumerate, ndindex, ogrid, + DataSource, c_, diag_indices, diag_indices_from, emath, + fromregex, genfromtxt, index_exp, ix_, load, loadtxt, + mgrid, ndenumerate, ndindex, ogrid, fill_diagonal, packbits, poly, poly1d, polyadd, polyder, polydiv, polyfit, polyint, polymul, polysub, polyval, - put_along_axis, r_, ravel_multi_index, - roots, row_stack, s_, save, savetxt, savez, - savez_compressed, split, take_along_axis, tile, - unpackbits, unravel_index, vsplit + r_, ravel_multi_index, roots, s_, save, savetxt, savez, + savez_compressed, unpackbits, unravel_index ) from .lib._histograms_impl import ( histogram, histogram_bin_edges, histogramdd @@ -217,6 +211,11 @@ histogram2d, mask_indices, tril_indices, tril_indices_from, triu_indices, triu_indices_from ) + from .lib._shape_base_impl import ( + apply_over_axes, apply_along_axis, array_split, column_stack, dsplit, + dstack, expand_dims, hsplit, kron, put_along_axis, row_stack, split, + take_along_axis, tile, vsplit + ) from .lib._type_check_impl import ( iscomplexobj, isrealobj, imag, iscomplex, isreal, nan_to_num, real, real_if_close, typename, mintypecode, common_type @@ -296,6 +295,7 @@ set(lib._nanfunctions_impl.__all__) | set(lib._function_base_impl.__all__) | set(lib._twodim_base_impl.__all__) | + set(lib._shape_base_impl.__all__) | set(lib._type_check_impl.__all__) | set(lib._arraysetops_impl.__all__) | set(lib._ufunclike_impl.__all__) | diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index ef483ced0354..720e5fc33e80 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -517,7 +517,7 @@ from numpy.lib.polynomial import ( polyfit as polyfit, ) -from numpy.lib.shape_base import ( +from numpy.lib._shape_base_impl import ( column_stack as column_stack, row_stack as row_stack, dstack as dstack, @@ -531,7 +531,6 @@ from numpy.lib.shape_base import ( apply_along_axis as apply_along_axis, kron as kron, tile as tile, - get_array_wrap as get_array_wrap, take_along_axis as take_along_axis, put_along_axis as put_along_axis, ) diff --git a/numpy/_expired_attrs_2_0.py b/numpy/_expired_attrs_2_0.py index eb5156603ffd..9350d70ff3b9 100644 --- a/numpy/_expired_attrs_2_0.py +++ b/numpy/_expired_attrs_2_0.py @@ -65,5 +65,6 @@ "`scalar_types` argument, use `numpy.result_type` and pass the " "Python values `0`, `0.0`, or `0j`.", "round_": "Use `np.round` instead.", - "nbytes": "Use `np.dtype().itemsize` instead." + "nbytes": "Use `np.dtype().itemsize` instead.", + "get_array_wrap": "", } diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 510fabfefb80..c5d331e83375 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -752,7 +752,7 @@ def test_lib_functions_deprecation_call(self): from numpy.lib._utils_impl import safe_eval from numpy.lib.npyio import recfromcsv, recfromtxt from numpy.lib._function_base_impl import disp - from numpy.lib.shape_base import get_array_wrap + from numpy.lib._shape_base_impl import get_array_wrap from numpy.core.numerictypes import maximum_sctype from numpy.lib.tests.test_io import TextIO diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index cad38af841d5..6d55ae0fd5fb 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -23,8 +23,8 @@ from . import index_tricks from . import _nanfunctions_impl from . import _function_base_impl -from . import shape_base from . import _stride_tricks_impl +from . import _shape_base_impl from . import stride_tricks from . import _twodim_base_impl from . import _ufunclike_impl @@ -33,12 +33,12 @@ from . import _utils_impl from . import _arraysetops_impl from . import npyio +from . import stride_tricks from . import arrayterator from . import _arraypad_impl from . import _version from .index_tricks import * -from .shape_base import * from .polynomial import * from .npyio import * from .arrayterator import Arrayterator @@ -49,7 +49,6 @@ __all__ = ['emath'] __all__ += index_tricks.__all__ -__all__ += shape_base.__all__ __all__ += polynomial.__all__ __all__ += npyio.__all__ @@ -70,7 +69,8 @@ def __getattr__(attr): return math elif attr in ( "histograms", "type_check", "nanfunctions", "function_base", - "arraypad", "arraysetops", "ufunclike", "utils", "twodim_base" + "arraypad", "arraysetops", "ufunclike", "utils", "twodim_base", + "shape_base" ): raise AttributeError( f"`np.lib.{attr}` is now private. If you are using a public " diff --git a/numpy/lib/__init__.pyi b/numpy/lib/__init__.pyi index 6470622cbd44..dfaa3e4955c5 100644 --- a/numpy/lib/__init__.pyi +++ b/numpy/lib/__init__.pyi @@ -70,25 +70,6 @@ from numpy.lib.polynomial import ( poly1d as poly1d, ) -from numpy.lib.shape_base import ( - column_stack as column_stack, - row_stack as row_stack, - dstack as dstack, - array_split as array_split, - split as split, - hsplit as hsplit, - vsplit as vsplit, - dsplit as dsplit, - apply_over_axes as apply_over_axes, - expand_dims as expand_dims, - apply_along_axis as apply_along_axis, - kron as kron, - tile as tile, - get_array_wrap as get_array_wrap, - take_along_axis as take_along_axis, - put_along_axis as put_along_axis, -) - from numpy.core.multiarray import ( add_docstring as add_docstring, tracemalloc_domain as tracemalloc_domain, diff --git a/numpy/lib/shape_base.py b/numpy/lib/_shape_base_impl.py similarity index 99% rename from numpy/lib/shape_base.py rename to numpy/lib/_shape_base_impl.py index 2510131da719..e940008a9af0 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/_shape_base_impl.py @@ -16,7 +16,7 @@ __all__ = [ 'column_stack', 'row_stack', 'dstack', 'array_split', 'split', 'hsplit', 'vsplit', 'dsplit', 'apply_over_axes', 'expand_dims', - 'apply_along_axis', 'kron', 'tile', 'get_array_wrap', 'take_along_axis', + 'apply_along_axis', 'kron', 'tile', 'take_along_axis', 'put_along_axis' ] @@ -1039,8 +1039,19 @@ def dsplit(ary, indices_or_sections): def get_array_prepare(*args): """Find the wrapper for the array with the highest priority. - In case of ties, leftmost wins. If no wrapper is found, return None + In case of ties, leftmost wins. If no wrapper is found, return None. + + .. deprecated:: 2.0 """ + + # Deprecated in NumPy 2.0, 2023-08-28 + warnings.warn( + "`get_array_prepare` is deprecated. " + "(deprecated in NumPy 2.0)", + DeprecationWarning, + stacklevel=2 + ) + wrappers = sorted((getattr(x, '__array_priority__', 0), -i, x.__array_prepare__) for i, x in enumerate(args) if hasattr(x, '__array_prepare__')) diff --git a/numpy/lib/shape_base.pyi b/numpy/lib/_shape_base_impl.pyi similarity index 100% rename from numpy/lib/shape_base.pyi rename to numpy/lib/_shape_base_impl.pyi diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 1d0544079bee..a839cd46621e 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -3,12 +3,12 @@ import sys import pytest -from numpy.exceptions import AxisError -from numpy.lib.shape_base import ( +from numpy import ( apply_along_axis, apply_over_axes, array_split, split, hsplit, dsplit, vsplit, dstack, column_stack, kron, tile, expand_dims, take_along_axis, put_along_axis ) +from numpy.exceptions import AxisError from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises, assert_warns ) diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index b9e541afc3bb..a95f29300234 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -1351,7 +1351,7 @@ def warn(arr): sup.record(UserWarning) # suppress warning from other module (may have .pyc ending), # if apply_along_axis is moved, had to be changed. - sup.filter(module=np.lib.shape_base) + sup.filter(module=np.lib._shape_base_impl) warnings.warn("Some warning") warn_other_module() # Check that the suppression did test the file correctly (this module diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 1f9ac6431ac9..063876552e37 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -35,7 +35,6 @@ def test_numpy_namespace(): # NumPy namespace (some are useful though, others need to be cleaned up) undocumented = { 'compare_chararrays': 'numpy.core._multiarray_umath.compare_chararrays', - 'get_array_wrap': 'numpy.lib.shape_base.get_array_wrap', 'show_config': 'numpy.__config__.show', } # We override dir to not show these members @@ -197,7 +196,6 @@ def test_NPY_NO_EXPORT(): "lib.index_tricks", "lib.npyio", "lib.polynomial", - "lib.shape_base", "lib.user_array", # note: not in np.lib, but probably should just be deleted "linalg.lapack_lite", "linalg.linalg", diff --git a/numpy/typing/tests/data/reveal/shape_base.pyi b/numpy/typing/tests/data/reveal/shape_base.pyi index 298d9e31e6bc..011b1550011f 100644 --- a/numpy/typing/tests/data/reveal/shape_base.pyi +++ b/numpy/typing/tests/data/reveal/shape_base.pyi @@ -43,9 +43,6 @@ reveal_type(np.vsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[ reveal_type(np.dsplit(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] reveal_type(np.dsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.lib.shape_base.get_array_prepare(AR_i8)) # E: lib.shape_base._ArrayPrepare -reveal_type(np.lib.shape_base.get_array_prepare(AR_i8, 1)) # E: Union[None, lib.shape_base._ArrayPrepare] - reveal_type(np.kron(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] reveal_type(np.kron(AR_b, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] reveal_type(np.kron(AR_f8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] From 4708dca39e8c678025bce8c8f1daf39d9fef7ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Wed, 30 Aug 2023 20:15:32 +0200 Subject: [PATCH 2/3] MAINT: Remove get_array_prepare --- numpy/lib/_shape_base_impl.py | 24 ------------------------ numpy/lib/_shape_base_impl.pyi | 5 ----- 2 files changed, 29 deletions(-) diff --git a/numpy/lib/_shape_base_impl.py b/numpy/lib/_shape_base_impl.py index e940008a9af0..634f7b743e23 100644 --- a/numpy/lib/_shape_base_impl.py +++ b/numpy/lib/_shape_base_impl.py @@ -1036,30 +1036,6 @@ def dsplit(ary, indices_or_sections): return split(ary, indices_or_sections, 2) -def get_array_prepare(*args): - """Find the wrapper for the array with the highest priority. - - In case of ties, leftmost wins. If no wrapper is found, return None. - - .. deprecated:: 2.0 - """ - - # Deprecated in NumPy 2.0, 2023-08-28 - warnings.warn( - "`get_array_prepare` is deprecated. " - "(deprecated in NumPy 2.0)", - DeprecationWarning, - stacklevel=2 - ) - - wrappers = sorted((getattr(x, '__array_priority__', 0), -i, - x.__array_prepare__) for i, x in enumerate(args) - if hasattr(x, '__array_prepare__')) - if wrappers: - return wrappers[-1][-1] - return None - - def get_array_wrap(*args): """Find the wrapper for the array with the highest priority. diff --git a/numpy/lib/_shape_base_impl.pyi b/numpy/lib/_shape_base_impl.pyi index 7cd9608b42fc..76ca75e4da15 100644 --- a/numpy/lib/_shape_base_impl.pyi +++ b/numpy/lib/_shape_base_impl.pyi @@ -183,11 +183,6 @@ def dsplit( indices_or_sections: _ShapeLike, ) -> list[NDArray[Any]]: ... -@overload -def get_array_prepare(*args: _SupportsArrayPrepare) -> _ArrayPrepare: ... -@overload -def get_array_prepare(*args: object) -> None | _ArrayPrepare: ... - @overload def get_array_wrap(*args: _SupportsArrayWrap) -> _ArrayWrap: ... @overload From 271211fc0958acf07da618989cb032ed380ef5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Thu, 31 Aug 2023 16:24:39 +0200 Subject: [PATCH 3/3] Fix duplicated import --- numpy/lib/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index 6d55ae0fd5fb..427649e9cb4a 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -33,7 +33,6 @@ from . import _utils_impl from . import _arraysetops_impl from . import npyio -from . import stride_tricks from . import arrayterator from . import _arraypad_impl from . import _version