Thanks to visit codestin.com
Credit goes to github.com

Skip to content

API: deprecate undocumented functions #24154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions doc/source/user/basics.io.genfromtxt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,3 @@ output array will then be a :class:`~numpy.ma.MaskedArray`.


.. unpack=None, loose=True, invalid_raise=True)


Shortcut functions
==================

In addition to :func:`~numpy.genfromtxt`, the ``numpy.lib.npyio`` module
provides several convenience functions derived from
:func:`~numpy.genfromtxt`. These functions work the same way as the
original, but they have different default values.

``numpy.lib.npyio.recfromtxt``
Returns a standard :class:`numpy.recarray` (if ``usemask=False``) or a
``numpy.ma.mrecords.MaskedRecords`` array (if ``usemaske=True``). The
default dtype is ``dtype=None``, meaning that the types of each column
will be automatically determined.
``numpy.lib.npyio.recfromcsv``
Like ``numpy.lib.npyio.recfromtxt``, but with a default ``delimiter=","``.
2 changes: 2 additions & 0 deletions numpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
in ``numpy.core``.
"""

from ._convertions import asunicode, asbytes


def set_module(module):
"""Private decorator for overriding __module__ on a function or class.
Expand Down
18 changes: 16 additions & 2 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,

See Also
--------
get_printoptions, printoptions, set_string_function, array2string
get_printoptions, printoptions, array2string

Notes
-----
Expand Down Expand Up @@ -320,7 +320,7 @@ def get_printoptions():

See Also
--------
set_printoptions, printoptions, set_string_function
set_printoptions, printoptions

"""
opts = _format_options.copy()
Expand Down Expand Up @@ -1691,6 +1691,10 @@ def set_string_function(f, repr=True):
"""
Set a Python function to be used when pretty printing arrays.

.. deprecated:: 2.0
Use `np.set_printoptions` instead with a formatter for custom
printing of NumPy objects.

Parameters
----------
f : function or None
Expand Down Expand Up @@ -1738,6 +1742,16 @@ def set_string_function(f, repr=True):
'array([0, 1, 2, 3])'

"""

# Deprecated in NumPy 2.0, 2023-07-11
warnings.warn(
"`set_string_function` is deprecated. Use `np.set_printoptions` "
"with a formatter for custom printing NumPy objects. "
"(deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

if f is None:
if repr:
return multiarray.set_string_function(_default_array_repr, 1)
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/defchararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .numeric import array as narray
from numpy.core.multiarray import _vec_string
from numpy.core import overrides
from numpy._utils._convertions import asbytes
from numpy._utils import asbytes
import numpy

__all__ = [
Expand Down
14 changes: 13 additions & 1 deletion numpy/core/numerictypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def maximum_sctype(t):
"""
Return the scalar type of highest precision of the same kind as the input.

.. deprecated:: 2.0
Use an explicit dtype like int64 or float64 instead.

Parameters
----------
t : dtype or dtype specifier
Expand Down Expand Up @@ -168,6 +171,15 @@ def maximum_sctype(t):
<class 'numpy.float128'> # may vary

"""

# Deprecated in NumPy 2.0, 2023-07-11
warnings.warn(
"`maximum_sctype` is deprecated. Use an explicit dtype like int64 "
"or float64 instead. (deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

g = obj2sctype(t)
if g is None:
return t
Expand Down Expand Up @@ -245,7 +257,7 @@ def obj2sctype(rep, default=None):

See Also
--------
sctype2char, issctype, issubsctype, issubdtype, maximum_sctype
sctype2char, issctype, issubsctype, issubdtype

Examples
--------
Expand Down
17 changes: 15 additions & 2 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,20 @@ 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 import byte_bounds, safe_eval, who
self.assert_deprecated(lambda: byte_bounds(np.array([1])))
from numpy import (
byte_bounds, safe_eval, who, recfromcsv, recfromtxt,
disp, get_array_wrap, maximum_sctype
)
from numpy.lib.tests.test_io import TextIO

self.assert_deprecated(lambda: safe_eval("None"))
self.assert_deprecated(lambda: who())

data_gen = lambda: TextIO('A,B\n0,1\n2,3')
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
self.assert_deprecated(lambda: recfromcsv(data_gen()))
self.assert_deprecated(lambda: recfromtxt(data_gen(), **kwargs))

self.assert_deprecated(lambda: disp("test"))
self.assert_deprecated(lambda: get_array_wrap())
self.assert_deprecated(lambda: maximum_sctype(int))
3 changes: 3 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ def assign(v):
assert_raises((AttributeError, TypeError), assign, C())
assert_raises(ValueError, assign, [1])

@pytest.mark.filterwarnings(
"ignore:.*set_string_function.*:DeprecationWarning"
)
def test_unicode_assignment(self):
# gh-5049
from numpy.core.numeric import set_string_function
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3354,6 +3354,9 @@ def test_list(self):
assert_equal(np.argwhere([4, 0, 2, 1, 3]), [[0], [2], [3], [4]])


@pytest.mark.filterwarnings(
"ignore:.*set_string_function.*:DeprecationWarning"
)
class TestStringFunction:

def test_set_string_function(self):
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/tests/test_numerictypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,13 @@ def test_ulong(self):
# We will probably allow this in the future:
assert not hasattr(np, 'ulong')


class TestBitName:
def test_abstract(self):
assert_raises(ValueError, np.core.numerictypes.bitname, np.floating)


@pytest.mark.filterwarnings("ignore:.*maximum_sctype.*:DeprecationWarning")
class TestMaximumSctype:

# note that parametrizing with sctype['int'] and similar would skip types
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_assert_valid_refcount, HAS_REFCOUNT, IS_PYSTON, IS_WASM
)
from numpy.testing._private.utils import _no_tracing, requires_memory
from numpy._utils._convertions import asbytes, asunicode
from numpy._utils import asbytes, asunicode


class TestRegression:
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy

from pathlib import Path
from numpy._utils._convertions import asunicode
from numpy._utils import asunicode
from numpy.testing import temppath, IS_WASM
from importlib import import_module

Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/_iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np
import numpy.core.numeric as nx
from numpy._utils._convertions import asbytes, asunicode
from numpy._utils import asbytes, asunicode


def _decode_line(line, encoding=None):
Expand Down
18 changes: 15 additions & 3 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import builtins
import collections.abc
import functools
import re
import sys
import warnings

from .._utils import set_module
import numpy as np
import numpy.core.numeric as _nx
from numpy.core import transpose
Expand All @@ -28,8 +28,7 @@
interp as compiled_interp, interp_complex as compiled_interp_complex
)
from numpy.core.umath import _add_newdoc_ufunc as add_newdoc_ufunc

import builtins
from numpy._utils import set_module

# needed in this module for compatibility
from numpy.lib.histograms import histogram, histogramdd # noqa: F401
Expand Down Expand Up @@ -1955,6 +1954,9 @@ def disp(mesg, device=None, linefeed=True):
"""
Display a message on a device.

.. deprecated:: 2.0
Use your own printing function instead.

Parameters
----------
mesg : str
Expand Down Expand Up @@ -1983,6 +1985,16 @@ def disp(mesg, device=None, linefeed=True):
'"Display" in a file\\n'

"""

# Deprecated in NumPy 2.0, 2023-07-11
warnings.warn(
"`disp` is deprecated, "
"use your own printing function instead. "
"(deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

if device is None:
device = sys.stdout
if linefeed:
Expand Down
28 changes: 27 additions & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
ConverterLockError, ConversionWarning, _is_string_like,
has_nested_fields, flatten_dtype, easy_dtype, _decode_line
)
from numpy._utils._convertions import asunicode, asbytes
from numpy._utils import asunicode, asbytes


__all__ = [
Expand Down Expand Up @@ -2453,6 +2453,9 @@ def recfromtxt(fname, **kwargs):
If ``usemask=False`` a standard `recarray` is returned,
if ``usemask=True`` a MaskedRecords array is returned.

.. deprecated:: 2.0
Use `numpy.genfromtxt` instead.

Parameters
----------
fname, kwargs : For a description of input parameters, see `genfromtxt`.
Expand All @@ -2467,6 +2470,16 @@ def recfromtxt(fname, **kwargs):
array will be determined from the data.

"""

# Deprecated in NumPy 2.0, 2023-07-11
warnings.warn(
"`recfromtxt` is deprecated, "
"use `numpy.genfromtxt` instead."
"(deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

kwargs.setdefault("dtype", None)
usemask = kwargs.get('usemask', False)
output = genfromtxt(fname, **kwargs)
Expand All @@ -2486,6 +2499,9 @@ def recfromcsv(fname, **kwargs):
`recarray`) or a masked record array (if ``usemask=True``,
see `ma.mrecords.MaskedRecords`).

.. deprecated:: 2.0
Use `numpy.genfromtxt` with comma as `delimiter` instead.

Parameters
----------
fname, kwargs : For a description of input parameters, see `genfromtxt`.
Expand All @@ -2500,6 +2516,16 @@ def recfromcsv(fname, **kwargs):
array will be determined from the data.

"""

# Deprecated in NumPy 2.0, 2023-07-11
warnings.warn(
"`recfromcsv` is deprecated, "
"use `numpy.genfromtxt` with comma as `delimiter` instead. "
"(deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

# Set default kwargs for genfromtxt as relevant to csv import.
kwargs.setdefault("case_sensitive", "lower")
kwargs.setdefault("names", True)
Expand Down
14 changes: 13 additions & 1 deletion numpy/lib/shape_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import warnings

import numpy.core.numeric as _nx
from numpy.core.numeric import asarray, zeros, array, asanyarray
Expand Down Expand Up @@ -1051,8 +1052,19 @@ def get_array_prepare(*args):
def get_array_wrap(*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-07-11
warnings.warn(
"`get_array_wrap` is deprecated. "
"(deprecated in NumPy 2.0)",
DeprecationWarning,
stacklevel=2
)

wrappers = sorted((getattr(x, '__array_priority__', 0), -i,
x.__array_wrap__) for i, x in enumerate(args)
if hasattr(x, '__array_wrap__'))
Expand Down
7 changes: 6 additions & 1 deletion numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
break_cycles, IS_WASM
)
from numpy.testing._private.utils import requires_memory
from numpy._utils._convertions import asbytes
from numpy._utils import asbytes


class TextIO(BytesIO):
Expand Down Expand Up @@ -1650,6 +1650,7 @@ def test_dtype_with_converters(self):
control = np.array([2009., 23., 46],)
assert_equal(test, control)

@pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
def test_dtype_with_converters_and_usecols(self):
dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n"
dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3}
Expand Down Expand Up @@ -2290,6 +2291,7 @@ def test_utf8_file_nodtype_unicode(self):
dtype=np.str_)
assert_array_equal(test, ctl)

@pytest.mark.filterwarnings("ignore:.*recfromtxt.*:DeprecationWarning")
def test_recfromtxt(self):
#
data = TextIO('A,B\n0,1\n2,3')
Expand All @@ -2309,6 +2311,7 @@ def test_recfromtxt(self):
assert_equal(test.mask, control.mask)
assert_equal(test.A, [0, 2])

@pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
def test_recfromcsv(self):
#
data = TextIO('A,B\n0,1\n2,3')
Expand Down Expand Up @@ -2609,6 +2612,7 @@ def test_genfromtxt(self, filename_type):
assert_array_equal(a, data)

@pytest.mark.parametrize("filename_type", [Path, str])
@pytest.mark.filterwarnings("ignore:.*recfromtxt.*:DeprecationWarning")
def test_recfromtxt(self, filename_type):
with temppath(suffix='.txt') as path:
path = filename_type(path)
Expand All @@ -2623,6 +2627,7 @@ def test_recfromtxt(self, filename_type):
assert_equal(test, control)

@pytest.mark.parametrize("filename_type", [Path, str])
@pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
def test_recfromcsv(self, filename_type):
with temppath(suffix='.txt') as path:
path = filename_type(path)
Expand Down
Loading