Skip to content

Commit 4dab17d

Browse files
authored
Merge pull request numpy#30068 from Abhi210/numpygh-11521-core-deprecations
MAINT: remove deprecated `style` argument and deprecations in `fromnumeric`
2 parents 7e52b73 + e535e3c commit 4dab17d

File tree

7 files changed

+23
-136
lines changed

7 files changed

+23
-136
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``numpy.array2string`` and ``numpy.sum`` deprecations finalized
2+
---------------------------------------------------------------
3+
4+
The following long-deprecated APIs have been removed or converted to errors:
5+
6+
* The ``style`` parameter has been removed from ``numpy.array2string``.
7+
This argument had no effect since Numpy 1.14.0.
8+
9+
* Calling ``np.sum(generator)`` directly on a generator object now raises a `TypeError`.
10+
This behavior was deprecated in NumPy 1.15.0. Use ``np.sum(np.fromiter(generator))``
11+
or the python ``sum`` builtin instead.

numpy/_core/arrayprint.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def _array2string(a, options, separator=' ', prefix=""):
619619
def _array2string_dispatcher(
620620
a, max_line_width=None, precision=None,
621621
suppress_small=None, separator=None, prefix=None,
622-
style=None, formatter=None, threshold=None,
622+
formatter=None, threshold=None,
623623
edgeitems=None, sign=None, floatmode=None, suffix=None,
624624
*, legacy=None):
625625
return (a,)
@@ -628,7 +628,7 @@ def _array2string_dispatcher(
628628
@array_function_dispatch(_array2string_dispatcher, module='numpy')
629629
def array2string(a, max_line_width=None, precision=None,
630630
suppress_small=None, separator=' ', prefix="",
631-
style=np._NoValue, formatter=None, threshold=None,
631+
formatter=None, threshold=None,
632632
edgeitems=None, sign=None, floatmode=None, suffix="",
633633
*, legacy=None):
634634
"""
@@ -663,10 +663,6 @@ def array2string(a, max_line_width=None, precision=None,
663663
wrapping is forced at the column ``max_line_width - len(suffix)``.
664664
It should be noted that the content of prefix and suffix strings are
665665
not included in the output.
666-
style : _NoValue, optional
667-
Has no effect, do not use.
668-
669-
.. deprecated:: 1.14.0
670666
formatter : dict of callables, optional
671667
If not None, the keys should indicate the type(s) that the respective
672668
formatting function applies to. Callables should return a string.
@@ -786,16 +782,8 @@ def array2string(a, max_line_width=None, precision=None,
786782
options.update(overrides)
787783

788784
if options['legacy'] <= 113:
789-
if style is np._NoValue:
790-
style = repr
791-
792785
if a.shape == () and a.dtype.names is None:
793-
return style(a.item())
794-
elif style is not np._NoValue:
795-
# Deprecation 11-9-2017 v1.14
796-
warnings.warn("'style' argument is deprecated and no longer functional"
797-
" except in 1.13 'legacy' mode",
798-
DeprecationWarning, stacklevel=2)
786+
return repr(a.item())
799787

800788
if options['legacy'] > 113:
801789
options['linewidth'] -= len(suffix)

numpy/_core/arrayprint.pyi

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ from typing import (
1010
SupportsIndex,
1111
TypeAlias,
1212
TypedDict,
13-
overload,
1413
type_check_only,
1514
)
16-
from typing_extensions import deprecated
1715

1816
import numpy as np
19-
from numpy._globals import _NoValueType
2017
from numpy._typing import NDArray, _CharLike_co, _FloatLike_co
2118

2219
__all__ = [
@@ -94,15 +91,13 @@ def set_printoptions(
9491
def get_printoptions() -> _FormatOptions: ...
9592

9693
# public numpy export
97-
@overload # no style
9894
def array2string(
9995
a: NDArray[Any],
10096
max_line_width: int | None = None,
10197
precision: SupportsIndex | None = None,
10298
suppress_small: bool | None = None,
10399
separator: str = " ",
104100
prefix: str = "",
105-
style: _NoValueType = ...,
106101
formatter: _FormatDict | None = None,
107102
threshold: int | None = None,
108103
edgeitems: int | None = None,
@@ -112,80 +107,6 @@ def array2string(
112107
*,
113108
legacy: _Legacy | None = None,
114109
) -> str: ...
115-
@overload # style=<given> (positional), legacy="1.13"
116-
def array2string(
117-
a: NDArray[Any],
118-
max_line_width: int | None,
119-
precision: SupportsIndex | None,
120-
suppress_small: bool | None,
121-
separator: str,
122-
prefix: str,
123-
style: _ReprFunc,
124-
formatter: _FormatDict | None = None,
125-
threshold: int | None = None,
126-
edgeitems: int | None = None,
127-
sign: _Sign | None = None,
128-
floatmode: _FloatMode | None = None,
129-
suffix: str = "",
130-
*,
131-
legacy: Literal["1.13"],
132-
) -> str: ...
133-
@overload # style=<given> (keyword), legacy="1.13"
134-
def array2string(
135-
a: NDArray[Any],
136-
max_line_width: int | None = None,
137-
precision: SupportsIndex | None = None,
138-
suppress_small: bool | None = None,
139-
separator: str = " ",
140-
prefix: str = "",
141-
*,
142-
style: _ReprFunc,
143-
formatter: _FormatDict | None = None,
144-
threshold: int | None = None,
145-
edgeitems: int | None = None,
146-
sign: _Sign | None = None,
147-
floatmode: _FloatMode | None = None,
148-
suffix: str = "",
149-
legacy: Literal["1.13"],
150-
) -> str: ...
151-
@overload # style=<given> (positional), legacy!="1.13"
152-
@deprecated("'style' argument is deprecated and no longer functional except in 1.13 'legacy' mode")
153-
def array2string(
154-
a: NDArray[Any],
155-
max_line_width: int | None,
156-
precision: SupportsIndex | None,
157-
suppress_small: bool | None,
158-
separator: str,
159-
prefix: str,
160-
style: _ReprFunc,
161-
formatter: _FormatDict | None = None,
162-
threshold: int | None = None,
163-
edgeitems: int | None = None,
164-
sign: _Sign | None = None,
165-
floatmode: _FloatMode | None = None,
166-
suffix: str = "",
167-
*,
168-
legacy: _LegacyNoStyle | None = None,
169-
) -> str: ...
170-
@overload # style=<given> (keyword), legacy="1.13"
171-
@deprecated("'style' argument is deprecated and no longer functional except in 1.13 'legacy' mode")
172-
def array2string(
173-
a: NDArray[Any],
174-
max_line_width: int | None = None,
175-
precision: SupportsIndex | None = None,
176-
suppress_small: bool | None = None,
177-
separator: str = " ",
178-
prefix: str = "",
179-
*,
180-
style: _ReprFunc,
181-
formatter: _FormatDict | None = None,
182-
threshold: int | None = None,
183-
edgeitems: int | None = None,
184-
sign: _Sign | None = None,
185-
floatmode: _FloatMode | None = None,
186-
suffix: str = "",
187-
legacy: _LegacyNoStyle | None = None,
188-
) -> str: ...
189110

190111
def format_float_scientific(
191112
x: _FloatLike_co,

numpy/_core/fromnumeric.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import functools
55
import math
66
import types
7-
import warnings
87

98
import numpy as np
109
from numpy._utils import set_module
@@ -755,8 +754,6 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
755754
provided with a sequence of k-th it will partition all elements
756755
indexed by k-th of them into their sorted position at once.
757756
758-
.. deprecated:: 1.22.0
759-
Passing booleans as index is deprecated.
760757
axis : int or None, optional
761758
Axis along which to sort. If None, the array is flattened before
762759
sorting. The default is -1, which sorts along the last axis.
@@ -868,8 +865,6 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
868865
sequence of k-th it will partition all of them into their sorted
869866
position at once.
870867
871-
.. deprecated:: 1.22.0
872-
Passing booleans as index is deprecated.
873868
axis : int or None, optional
874869
Axis along which to sort. The default is -1 (the last axis). If
875870
None, the flattened array is used.
@@ -2008,15 +2003,6 @@ def nonzero(a):
20082003
To group the indices by element, rather than dimension, use `argwhere`,
20092004
which returns a row for each non-zero element.
20102005
2011-
.. note::
2012-
2013-
When called on a zero-d array or scalar, ``nonzero(a)`` is treated
2014-
as ``nonzero(atleast_1d(a))``.
2015-
2016-
.. deprecated:: 1.17.0
2017-
2018-
Use `atleast_1d` explicitly if this behavior is deliberate.
2019-
20202006
Parameters
20212007
----------
20222008
a : array_like
@@ -2430,19 +2416,12 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
24302416
"""
24312417
if isinstance(a, _gentype):
24322418
# 2018-02-25, 1.15.0
2433-
warnings.warn(
2434-
"Calling np.sum(generator) is deprecated, and in the future will "
2435-
"give a different result. Use np.sum(np.fromiter(generator)) or "
2419+
raise TypeError(
2420+
"Calling np.sum(generator) is deprecated."
2421+
"Use np.sum(np.fromiter(generator)) or "
24362422
"the python sum builtin instead.",
2437-
DeprecationWarning, stacklevel=2
24382423
)
24392424

2440-
res = _sum_(a)
2441-
if out is not None:
2442-
out[...] = res
2443-
return out
2444-
return res
2445-
24462425
return _wrapreduction(
24472426
a, np.add, 'sum', axis, dtype, out,
24482427
keepdims=keepdims, initial=initial, where=where

numpy/_core/tests/test_arrayprint.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,7 @@ def test_0d_arrays(self):
735735
# str is unaffected
736736
assert_equal(str(x), "1")
737737

738-
# check `style` arg raises
739-
pytest.warns(DeprecationWarning, np.array2string,
740-
np.array(1.), style=repr)
741-
# but not in legacy mode
742-
np.array2string(np.array(1.), style=repr, legacy='1.13')
743-
# gh-10934 style was broken in legacy mode, check it works
738+
# check it works
744739
np.array2string(np.array(1.), legacy='1.13')
745740

746741
def test_float_spacing(self):

numpy/_core/tests/test_deprecations.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ def test_bincount_bad_list(self, badlist):
145145
self.assert_deprecated(lambda: np.bincount(badlist))
146146

147147

148-
class TestGeneratorSum(_DeprecationTestCase):
149-
# 2018-02-25, 1.15.0
150-
def test_generator_sum(self):
151-
self.assert_deprecated(np.sum, args=((i for i in range(5)),))
152-
153-
154148
class BuiltInRoundComplexDType(_DeprecationTestCase):
155149
# 2020-03-31 1.19.0
156150
deprecated_types = [np.csingle, np.cdouble, np.clongdouble]

numpy/typing/tests/data/fail/arrayprint.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ AR: npt.NDArray[np.float64]
88
func1: Callable[[Any], str]
99
func2: Callable[[np.integer], str]
1010

11-
np.array2string(AR, style=None) # type: ignore[call-overload]
12-
np.array2string(AR, legacy="1.14") # type: ignore[call-overload]
13-
np.array2string(AR, sign="*") # type: ignore[call-overload]
14-
np.array2string(AR, floatmode="default") # type: ignore[call-overload]
15-
np.array2string(AR, formatter={"A": func1}) # type: ignore[call-overload]
16-
np.array2string(AR, formatter={"float": func2}) # type: ignore[call-overload]
11+
np.array2string(AR, legacy="1.14") # type: ignore[arg-type]
12+
np.array2string(AR, sign="*") # type: ignore[arg-type]
13+
np.array2string(AR, floatmode="default") # type: ignore[arg-type]
14+
np.array2string(AR, formatter={"A": func1}) # type: ignore[arg-type]
15+
np.array2string(AR, formatter={"float": func2}) # type: ignore[arg-type]

0 commit comments

Comments
 (0)