From ace444596424846058d8bfaacace29cd7e0c849a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Mon, 28 Aug 2023 17:37:14 +0200 Subject: [PATCH 1/4] API: Update lib.arraypad lib.arraysetops lib.ufunclike lib.utils namespaces --- benchmarks/benchmarks/__init__.py | 2 +- doc/source/reference/routines.set.rst | 6 +-- numpy/__init__.py | 28 ++++++---- numpy/__init__.pyi | 10 ++-- numpy/_pytesttester.py | 2 +- numpy/core/tests/test_deprecations.py | 2 +- numpy/lib/__init__.py | 17 ++---- numpy/lib/__init__.pyi | 28 ---------- numpy/lib/{arraypad.py => _arraypad_impl.py} | 0 .../lib/{arraypad.pyi => _arraypad_impl.pyi} | 0 .../{arraysetops.py => _arraysetops_impl.py} | 22 -------- ...{arraysetops.pyi => _arraysetops_impl.pyi} | 0 numpy/lib/_function_base_impl.py | 2 +- numpy/lib/_type_check_impl.py | 2 +- .../lib/{ufunclike.py => _ufunclike_impl.py} | 0 .../{ufunclike.pyi => _ufunclike_impl.pyi} | 0 numpy/lib/{utils.py => _utils_impl.py} | 3 ++ numpy/lib/{utils.pyi => _utils_impl.pyi} | 0 numpy/lib/format.py | 2 +- numpy/lib/tests/test_arraypad.py | 4 +- numpy/lib/tests/test_arraysetops.py | 6 +-- numpy/lib/tests/test_format.py | 2 +- numpy/lib/tests/test_ufunclike.py | 54 +++++++++---------- numpy/lib/tests/test_utils.py | 42 ++++++--------- numpy/ma/extras.py | 4 +- numpy/tests/test_public_api.py | 7 --- 26 files changed, 89 insertions(+), 156 deletions(-) rename numpy/lib/{arraypad.py => _arraypad_impl.py} (100%) rename numpy/lib/{arraypad.pyi => _arraypad_impl.pyi} (100%) rename numpy/lib/{arraysetops.py => _arraysetops_impl.py} (97%) rename numpy/lib/{arraysetops.pyi => _arraysetops_impl.pyi} (100%) rename numpy/lib/{ufunclike.py => _ufunclike_impl.py} (100%) rename numpy/lib/{ufunclike.pyi => _ufunclike_impl.pyi} (100%) rename numpy/lib/{utils.py => _utils_impl.py} (99%) rename numpy/lib/{utils.pyi => _utils_impl.pyi} (100%) diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index a4c1c3fb92a0..8efa67de33eb 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -3,7 +3,7 @@ import os def show_cpu_features(): - from numpy.lib.utils import _opt_info + from numpy.lib._utils_impl import _opt_info info = _opt_info() info = "NumPy CPU features: " + (info if info else 'nothing enabled') # ASV wrapping stdout & stderr, so we assume having a tty here diff --git a/doc/source/reference/routines.set.rst b/doc/source/reference/routines.set.rst index 149c33a8b610..274867870eea 100644 --- a/doc/source/reference/routines.set.rst +++ b/doc/source/reference/routines.set.rst @@ -3,11 +3,6 @@ Set routines .. currentmodule:: numpy -.. autosummary:: - :toctree: generated/ - - lib.arraysetops - Making proper sets ------------------ .. autosummary:: @@ -20,6 +15,7 @@ Boolean operations .. autosummary:: :toctree: generated/ + ediff1d in1d intersect1d isin diff --git a/numpy/__init__.py b/numpy/__init__.py index b634b526a5cd..52b02220282a 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -185,20 +185,18 @@ from .lib import ( DataSource, apply_along_axis, apply_over_axes, array_split, broadcast_arrays, broadcast_shapes, - broadcast_to, byte_bounds, c_, column_stack, diag_indices, + broadcast_to, c_, column_stack, diag_indices, diag_indices_from, dsplit, dstack, - ediff1d, emath, expand_dims, fill_diagonal, fix, + emath, expand_dims, fill_diagonal, fromregex, get_array_wrap, genfromtxt, - get_include, hsplit, in1d, index_exp, info, intersect1d, - isin, isneginf, ix_, kron, load, loadtxt, + hsplit, index_exp, ix_, kron, load, loadtxt, mgrid, ndenumerate, ndindex, ogrid, - packbits, pad, poly, poly1d, polyadd, polyder, + 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, - setdiff1d, setxor1d, show_runtime, split, - take_along_axis, tile, union1d, unique, unpackbits, - unravel_index, vsplit, isposinf + roots, row_stack, s_, save, savetxt, savez, + savez_compressed, split, take_along_axis, tile, + unpackbits, unravel_index, vsplit ) from .lib._histograms_impl import ( histogram, histogram_bin_edges, histogramdd @@ -224,6 +222,14 @@ iscomplexobj, isrealobj, imag, iscomplex, isreal, nan_to_num, real, real_if_close, typename, mintypecode, common_type ) + from .lib._arraysetops_impl import ( + ediff1d, in1d, intersect1d, isin, setdiff1d, setxor1d, union1d, unique + ) + from .lib._ufunclike_impl import fix, isneginf, isposinf + from .lib._arraypad_impl import pad + from .lib._utils_impl import ( + byte_bounds, show_runtime, get_include, info + ) from . import matrixlib as _mat from .matrixlib import ( asmatrix, bmat, matrix @@ -289,6 +295,10 @@ set(lib._function_base_impl.__all__) | set(lib._twodim_base_impl.__all__) | set(lib._type_check_impl.__all__) | + set(lib._arraysetops_impl.__all__) | + set(lib._ufunclike_impl.__all__) | + set(lib._arraypad_impl.__all__) | + set(lib._utils_impl.__all__) | {"show_config", "__version__"} ) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 57d43874ddaf..a928a2a5ec13 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -397,11 +397,11 @@ from numpy.lib import ( emath as emath, ) -from numpy.lib.arraypad import ( +from numpy.lib._arraypad_impl import ( pad as pad, ) -from numpy.lib.arraysetops import ( +from numpy.lib._arraysetops_impl import ( ediff1d as ediff1d, intersect1d as intersect1d, setxor1d as setxor1d, @@ -575,17 +575,15 @@ from numpy.lib._type_check_impl import ( common_type as common_type, ) -from numpy.lib.ufunclike import ( +from numpy.lib._ufunclike_impl import ( fix as fix, isposinf as isposinf, isneginf as isneginf, ) -from numpy.lib.utils import ( - issubdtype as issubdtype, +from numpy.lib._utils_impl import ( get_include as get_include, info as info, - source as source, byte_bounds as byte_bounds, show_runtime as show_runtime, ) diff --git a/numpy/_pytesttester.py b/numpy/_pytesttester.py index 1c38291ae331..d2b87dd8fef5 100644 --- a/numpy/_pytesttester.py +++ b/numpy/_pytesttester.py @@ -39,7 +39,7 @@ def _show_numpy_info(): print("NumPy version %s" % np.__version__) relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous print("NumPy relaxed strides checking option:", relaxed_strides) - info = np.lib.utils._opt_info() + info = np.lib._utils_impl._opt_info() print("NumPy CPU features: ", (info if info else 'nothing enabled')) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index b919be97046e..6b144838683a 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -753,7 +753,7 @@ def test_deprecated_np_lib_math(self): class TestLibImports(_DeprecationTestCase): # Deprecated in Numpy 1.26.0, 2023-09 def test_lib_functions_deprecation_call(self): - from numpy.lib.utils import safe_eval + 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 diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index 0845a27a30c3..bd97996abde0 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -26,27 +26,22 @@ from . import shape_base from . import stride_tricks from . import _twodim_base_impl -from . import ufunclike +from . import _ufunclike_impl from . import _histograms_impl from . import polynomial -from . import utils -from . import arraysetops +from . import _utils_impl +from . import _arraysetops_impl from . import npyio from . import arrayterator -from . import arraypad +from . import _arraypad_impl from . import _version from .index_tricks import * from .shape_base import * from .stride_tricks import * -from .ufunclike import * - from .polynomial import * -from .utils import * -from .arraysetops import * from .npyio import * from .arrayterator import Arrayterator -from .arraypad import * from ._version import * from numpy.core._multiarray_umath import add_docstring, tracemalloc_domain from numpy.core.function_base import add_newdoc @@ -56,11 +51,7 @@ __all__ += index_tricks.__all__ __all__ += shape_base.__all__ __all__ += stride_tricks.__all__ -__all__ += ufunclike.__all__ -__all__ += arraypad.__all__ __all__ += polynomial.__all__ -__all__ += utils.__all__ -__all__ += arraysetops.__all__ __all__ += npyio.__all__ from numpy._pytesttester import PytestTester diff --git a/numpy/lib/__init__.pyi b/numpy/lib/__init__.pyi index f78ca5232245..9bd016050e87 100644 --- a/numpy/lib/__init__.pyi +++ b/numpy/lib/__init__.pyi @@ -21,21 +21,6 @@ from numpy.lib._version import ( NumpyVersion as NumpyVersion, ) -from numpy.lib.arraypad import ( - pad as pad, -) - -from numpy.lib.arraysetops import ( - ediff1d as ediff1d, - intersect1d as intersect1d, - setxor1d as setxor1d, - union1d as union1d, - setdiff1d as setdiff1d, - unique as unique, - in1d as in1d, - isin as isin, -) - from numpy.lib.arrayterator import ( Arrayterator as Arrayterator, ) @@ -110,19 +95,6 @@ from numpy.lib.stride_tricks import ( broadcast_shapes as broadcast_shapes, ) -from numpy.lib.ufunclike import ( - fix as fix, - isposinf as isposinf, - isneginf as isneginf, -) - -from numpy.lib.utils import ( - get_include as get_include, - info as info, - byte_bounds as byte_bounds, - show_runtime as show_runtime, -) - from numpy.core.multiarray import ( add_docstring as add_docstring, tracemalloc_domain as tracemalloc_domain, diff --git a/numpy/lib/arraypad.py b/numpy/lib/_arraypad_impl.py similarity index 100% rename from numpy/lib/arraypad.py rename to numpy/lib/_arraypad_impl.py diff --git a/numpy/lib/arraypad.pyi b/numpy/lib/_arraypad_impl.pyi similarity index 100% rename from numpy/lib/arraypad.pyi rename to numpy/lib/_arraypad_impl.pyi diff --git a/numpy/lib/arraysetops.py b/numpy/lib/_arraysetops_impl.py similarity index 97% rename from numpy/lib/arraysetops.py rename to numpy/lib/_arraysetops_impl.py index 6f6e25f312bd..dc0078bf1dc7 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/_arraysetops_impl.py @@ -195,8 +195,6 @@ def unique(ar, return_index=False, return_inverse=False, See Also -------- - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. repeat : Repeat elements of an array. Notes @@ -404,12 +402,6 @@ def intersect1d(ar1, ar2, assume_unique=False, return_indices=False): The indices of the first occurrences of the common values in `ar2`. Only provided if `return_indices` is True. - - See Also - -------- - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. - Examples -------- >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1]) @@ -577,8 +569,6 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None): -------- isin : Version of this function that preserves the shape of ar1. - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. Notes ----- @@ -818,8 +808,6 @@ def isin(element, test_elements, assume_unique=False, invert=False, *, See Also -------- in1d : Flattened version of this function. - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. Notes ----- @@ -914,11 +902,6 @@ def union1d(ar1, ar2): union1d : ndarray Unique, sorted union of the input arrays. - See Also - -------- - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. - Examples -------- >>> np.union1d([-1, 0, 1], [-2, 0, 2]) @@ -961,11 +944,6 @@ def setdiff1d(ar1, ar2, assume_unique=False): is sorted when `assume_unique=False`, but otherwise only sorted if the input is sorted. - See Also - -------- - numpy.lib.arraysetops : Module with a number of other functions for - performing set operations on arrays. - Examples -------- >>> a = np.array([1, 2, 3, 2, 4, 1]) diff --git a/numpy/lib/arraysetops.pyi b/numpy/lib/_arraysetops_impl.pyi similarity index 100% rename from numpy/lib/arraysetops.pyi rename to numpy/lib/_arraysetops_impl.pyi diff --git a/numpy/lib/_function_base_impl.py b/numpy/lib/_function_base_impl.py index 9ce647af7540..16a62896373f 100644 --- a/numpy/lib/_function_base_impl.py +++ b/numpy/lib/_function_base_impl.py @@ -3901,7 +3901,7 @@ def _median(a, axis=None, out=None, overwrite_input=False): rout = mean(part[indexer], axis=axis, out=out) if supports_nans and sz > 0: # If nans are possible, warn and replace by nans like mean would. - rout = np.lib.utils._median_nancheck(part, rout, axis) + rout = np.lib._utils_impl._median_nancheck(part, rout, axis) return rout diff --git a/numpy/lib/_type_check_impl.py b/numpy/lib/_type_check_impl.py index 7059c05edf46..0af7ae441648 100644 --- a/numpy/lib/_type_check_impl.py +++ b/numpy/lib/_type_check_impl.py @@ -12,7 +12,7 @@ import numpy.core.numeric as _nx from numpy.core.numeric import asarray, asanyarray, isnan, zeros from numpy.core import overrides, getlimits -from .ufunclike import isneginf, isposinf +from ._ufunclike_impl import isneginf, isposinf array_function_dispatch = functools.partial( diff --git a/numpy/lib/ufunclike.py b/numpy/lib/_ufunclike_impl.py similarity index 100% rename from numpy/lib/ufunclike.py rename to numpy/lib/_ufunclike_impl.py diff --git a/numpy/lib/ufunclike.pyi b/numpy/lib/_ufunclike_impl.pyi similarity index 100% rename from numpy/lib/ufunclike.pyi rename to numpy/lib/_ufunclike_impl.pyi diff --git a/numpy/lib/utils.py b/numpy/lib/_utils_impl.py similarity index 99% rename from numpy/lib/utils.py rename to numpy/lib/_utils_impl.py index a2b29110da35..9b81afd64d55 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/_utils_impl.py @@ -16,6 +16,7 @@ ] +@set_module('numpy') def show_runtime(): """ Print information about various resources in the system @@ -69,6 +70,7 @@ def show_runtime(): pprint(config_found) +@set_module('numpy') def get_include(): """ Return the directory that contains the NumPy \\*.h header files. @@ -290,6 +292,7 @@ def deprecate_with_doc(msg): #-------------------------------------------- +@set_module('numpy') def byte_bounds(a): """ Returns pointers to the end-points of an array. diff --git a/numpy/lib/utils.pyi b/numpy/lib/_utils_impl.pyi similarity index 100% rename from numpy/lib/utils.pyi rename to numpy/lib/_utils_impl.pyi diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 29373cb91d97..87f35a7a4f60 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -167,7 +167,7 @@ import warnings import numpy -from numpy.lib.utils import drop_metadata +from numpy.lib._utils_impl import drop_metadata __all__ = [] diff --git a/numpy/lib/tests/test_arraypad.py b/numpy/lib/tests/test_arraypad.py index e6eb9ace39fd..3176b0c65943 100644 --- a/numpy/lib/tests/test_arraypad.py +++ b/numpy/lib/tests/test_arraypad.py @@ -5,7 +5,7 @@ import numpy as np from numpy.testing import assert_array_equal, assert_allclose, assert_equal -from numpy.lib.arraypad import _as_pairs +from numpy.lib._arraypad_impl import _as_pairs _numeric_dtypes = ( @@ -628,7 +628,7 @@ def test_check_constant_odd_pad_amount(self): def test_check_constant_pad_2d(self): arr = np.arange(4).reshape(2, 2) - test = np.lib.pad(arr, ((1, 2), (1, 3)), mode='constant', + test = np.pad(arr, ((1, 2), (1, 3)), mode='constant', constant_values=((1, 2), (3, 4))) expected = np.array( [[3, 1, 1, 4, 4, 4], diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index 3d8969425700..95682661c27e 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -3,12 +3,12 @@ """ import numpy as np -from numpy.testing import (assert_array_equal, assert_equal, - assert_raises, assert_raises_regex) -from numpy.lib.arraysetops import ( +from numpy import ( ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d, isin ) from numpy.exceptions import AxisError +from numpy.testing import (assert_array_equal, assert_equal, + assert_raises, assert_raises_regex) import pytest diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 3bbbb215bb77..344d46324e22 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -1022,7 +1022,7 @@ def test_metadata_dtype(dt, fail): else: arr2 = np.load(buf) # BUG: assert_array_equal does not check metadata - from numpy.lib.utils import drop_metadata + from numpy.lib._utils_impl import drop_metadata assert_array_equal(arr, arr2) assert drop_metadata(arr.dtype) is not arr.dtype assert drop_metadata(arr2.dtype) is arr2.dtype diff --git a/numpy/lib/tests/test_ufunclike.py b/numpy/lib/tests/test_ufunclike.py index 62d2ef72ebfb..8e0ccf7cdc97 100644 --- a/numpy/lib/tests/test_ufunclike.py +++ b/numpy/lib/tests/test_ufunclike.py @@ -1,59 +1,59 @@ import numpy as np -import numpy.core as nx -import numpy.lib.ufunclike as ufl + +from numpy import fix, isposinf, isneginf from numpy.testing import ( - assert_, assert_equal, assert_array_equal, assert_warns, assert_raises + assert_, assert_equal, assert_array_equal, assert_raises ) class TestUfunclike: def test_isposinf(self): - a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0]) - out = nx.zeros(a.shape, bool) - tgt = nx.array([True, False, False, False, False, False]) + a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0]) + out = np.zeros(a.shape, bool) + tgt = np.array([True, False, False, False, False, False]) - res = ufl.isposinf(a) + res = isposinf(a) assert_equal(res, tgt) - res = ufl.isposinf(a, out) + res = isposinf(a, out) assert_equal(res, tgt) assert_equal(out, tgt) a = a.astype(np.complex128) with assert_raises(TypeError): - ufl.isposinf(a) + isposinf(a) def test_isneginf(self): - a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0]) - out = nx.zeros(a.shape, bool) - tgt = nx.array([False, True, False, False, False, False]) + a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0]) + out = np.zeros(a.shape, bool) + tgt = np.array([False, True, False, False, False, False]) - res = ufl.isneginf(a) + res = isneginf(a) assert_equal(res, tgt) - res = ufl.isneginf(a, out) + res = isneginf(a, out) assert_equal(res, tgt) assert_equal(out, tgt) a = a.astype(np.complex128) with assert_raises(TypeError): - ufl.isneginf(a) + isneginf(a) def test_fix(self): - a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]]) - out = nx.zeros(a.shape, float) - tgt = nx.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]]) + a = np.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]]) + out = np.zeros(a.shape, float) + tgt = np.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]]) - res = ufl.fix(a) + res = fix(a) assert_equal(res, tgt) - res = ufl.fix(a, out) + res = fix(a, out) assert_equal(res, tgt) assert_equal(out, tgt) - assert_equal(ufl.fix(3.14), 3) + assert_equal(fix(3.14), 3) def test_fix_with_subclass(self): - class MyArray(nx.ndarray): + class MyArray(np.ndarray): def __new__(cls, data, metadata=None): - res = nx.array(data, copy=True).view(cls) + res = np.array(data, copy=True).view(cls) res.metadata = metadata return res @@ -66,17 +66,17 @@ def __array_finalize__(self, obj): self.metadata = getattr(obj, 'metadata', None) return self - a = nx.array([1.1, -1.1]) + a = np.array([1.1, -1.1]) m = MyArray(a, metadata='foo') - f = ufl.fix(m) - assert_array_equal(f, nx.array([1, -1])) + f = fix(m) + assert_array_equal(f, np.array([1, -1])) assert_(isinstance(f, MyArray)) assert_equal(f.metadata, 'foo') # check 0d arrays don't decay to scalars m0d = m[0,...] m0d.metadata = 'bar' - f0d = ufl.fix(m0d) + f0d = fix(m0d) assert_(isinstance(f0d, MyArray)) assert_equal(f0d.metadata, 'bar') diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index f4f17b5ac701..bceff04d5185 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -1,45 +1,37 @@ -import sys import pytest import numpy as np -from numpy.core import arange -from numpy.testing import assert_, assert_equal, assert_raises_regex -import numpy.lib.utils as utils +from numpy.testing import assert_equal, assert_raises_regex +import numpy.lib._utils_impl as _utils_impl from io import StringIO -@pytest.mark.filterwarnings("ignore:.*safe_eval.*:DeprecationWarning") -def test_safe_eval_nameconstant(): - # Test if safe_eval supports Python 3.4 _ast.NameConstant - utils.safe_eval('None') - - class TestByteBounds: def test_byte_bounds(self): # pointer difference matches size * itemsize # due to contiguity - a = arange(12).reshape(3, 4) - low, high = utils.byte_bounds(a) + a = np.arange(12).reshape(3, 4) + low, high = np.byte_bounds(a) assert_equal(high - low, a.size * a.itemsize) def test_unusual_order_positive_stride(self): - a = arange(12).reshape(3, 4) + a = np.arange(12).reshape(3, 4) b = a.T - low, high = utils.byte_bounds(b) + low, high = np.byte_bounds(b) assert_equal(high - low, b.size * b.itemsize) def test_unusual_order_negative_stride(self): - a = arange(12).reshape(3, 4) + a = np.arange(12).reshape(3, 4) b = a.T[::-1] - low, high = utils.byte_bounds(b) + low, high = np.byte_bounds(b) assert_equal(high - low, b.size * b.itemsize) def test_strided(self): - a = arange(12) + a = np.arange(12) b = a[::2] - low, high = utils.byte_bounds(b) + low, high = np.byte_bounds(b) # the largest pointer address is lost (even numbers only in the # stride), and compensate addresses for striding by 2 assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize) @@ -62,7 +54,7 @@ def first_method(): def _has_method_heading(cls): out = StringIO() - utils.info(cls, output=out) + np.info(cls, output=out) return 'Methods:' in out.getvalue() assert _has_method_heading(WithPublicMethods) @@ -76,7 +68,7 @@ def _compare_dtypes(dt1, dt2): # structured dtype dt = np.dtype([('l1', [('l2', np.dtype('S8', metadata={'msg': 'toto'}))])], metadata={'msg': 'titi'}) - dt_m = utils.drop_metadata(dt) + dt_m = _utils_impl.drop_metadata(dt) assert _compare_dtypes(dt, dt_m) is True assert dt_m.metadata is None assert dt_m['l1'].metadata is None @@ -86,21 +78,21 @@ def _compare_dtypes(dt1, dt2): dt = np.dtype([('x', ' Date: Mon, 28 Aug 2023 21:26:52 +0200 Subject: [PATCH 2/4] Apply review changes --- doc/source/reference/routines.set.rst | 1 - numpy/lib/__init__.py | 12 +++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/routines.set.rst b/doc/source/reference/routines.set.rst index 274867870eea..b12d3d5f5e79 100644 --- a/doc/source/reference/routines.set.rst +++ b/doc/source/reference/routines.set.rst @@ -15,7 +15,6 @@ Boolean operations .. autosummary:: :toctree: generated/ - ediff1d in1d intersect1d isin diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index bd97996abde0..316edab11ba7 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -63,13 +63,23 @@ def __getattr__(attr): import math import warnings + def _private_module_msg(module_name: str) -> str: + return ( + f"`np.lib.{module_name}` is now private. Function that you're " + "looking for should be available from the main namespace `np.*`" + ) + if attr == 'math': warnings.warn( "`np.lib.math` is a deprecated alias for the standard library " "`math` module (Deprecated Numpy 1.25). Replace usages of " "`numpy.lib.math` with `math`", DeprecationWarning, stacklevel=2) return math + elif attr in ( + "histograms", "type_check", "nanfunctions", "function_base", + "arraypad", "arraysetops", "ufunclike", "utils" + ): + raise AttributeError(_private_module_msg(attr)) else: raise AttributeError("module {!r} has no attribute " "{!r}".format(__name__, attr)) - From 0379c45e30a524bd51996830283939b9f0c58abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Tue, 29 Aug 2023 10:05:31 +0200 Subject: [PATCH 3/4] Update AttributeError message --- numpy/lib/__init__.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index 316edab11ba7..9e4b41cc217a 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -63,12 +63,6 @@ def __getattr__(attr): import math import warnings - def _private_module_msg(module_name: str) -> str: - return ( - f"`np.lib.{module_name}` is now private. Function that you're " - "looking for should be available from the main namespace `np.*`" - ) - if attr == 'math': warnings.warn( "`np.lib.math` is a deprecated alias for the standard library " @@ -79,7 +73,11 @@ def _private_module_msg(module_name: str) -> str: "histograms", "type_check", "nanfunctions", "function_base", "arraypad", "arraysetops", "ufunclike", "utils" ): - raise AttributeError(_private_module_msg(attr)) + raise AttributeError( + "If you are using a public function, it should be available " + "in the main numpy namespace, otherwise check the NumPy 2.0 " + "migration guide." + ) else: raise AttributeError("module {!r} has no attribute " "{!r}".format(__name__, attr)) From 08e086c3ea8b08cbe0af65546c662fc5499f284d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Tue, 29 Aug 2023 21:13:43 +0200 Subject: [PATCH 4/4] Apply review comment --- numpy/lib/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy/lib/__init__.py b/numpy/lib/__init__.py index 9e4b41cc217a..6f90a6c059ce 100644 --- a/numpy/lib/__init__.py +++ b/numpy/lib/__init__.py @@ -71,12 +71,12 @@ def __getattr__(attr): return math elif attr in ( "histograms", "type_check", "nanfunctions", "function_base", - "arraypad", "arraysetops", "ufunclike", "utils" + "arraypad", "arraysetops", "ufunclike", "utils", "twodim_base" ): raise AttributeError( - "If you are using a public function, it should be available " - "in the main numpy namespace, otherwise check the NumPy 2.0 " - "migration guide." + f"`np.lib.{attr}` is now private. If you are using a public " + "function, it should be available in the main numpy namespace, " + "otherwise check the NumPy 2.0 migration guide." ) else: raise AttributeError("module {!r} has no attribute "