8000 DEP: Deprecate calling the `out` argument `y` · numpy/numpy@628f7b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 628f7b6

Browse files
committed
DEP: Deprecate calling the out argument y
Fixes gh-8995
1 parent 29e8883 commit 628f7b6

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

doc/release/1.13.0-notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Dropped Support
2020
Deprecations
2121
============
2222

23+
* Calling ``np.fix``, ``np.isposinf``, and ``np.isneginf`` with ``f(x, y=out)``
24+
is deprecated - the argument should be passed as ``f(x, out=out)``, which
25+
matches other ufunc-like interfaces.
26+
2327

2428
Build System Changes
2529
====================

numpy/lib/tests/test_ufunclike.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import numpy.core as nx
44
import numpy.lib.ufunclike as ufl
55
from numpy.testing import (
6-
run_module_suite, TestCase, assert_, assert_equal, assert_array_equal
6+
run_module_suite, TestCase, assert_, assert_equal, assert_array_equal,
7+
assert_warns
78
)
89

910

@@ -61,5 +62,11 @@ def __array_wrap__(self, obj, context=None):
6162
assert_(isinstance(f, MyArray))
6263
assert_equal(f.metadata, 'foo')
6364

65+
def test_deprecated(self):
66+
# NumPy 1.13.0, 2017-04-26
67+
assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
68+
assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
69+
assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2))
70+
6471
if __name__ == "__main__":
6572
run_module_suite()

numpy/lib/ufunclike.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,36 @@
88
__all__ = ['fix', 'isneginf', 'isposinf']
99

1010
import numpy.core.numeric as nx
11+
import warnings
12+
import functools
1113

12-
def fix(x, y=None):
14+
def _deprecate_out_named_y(f):
15+
"""
16+
Allow the out argument to be passed as the name `y` (deprecated)
17+
18+
In future, this decorator should be removed.
19+
"""
20+
@functools.wraps(f)
21+
def func(x, out=None, **kwargs):
22+
if 'y' in kwargs:
23+
if 'out' in kwargs:
24+
raise TypeError(
25+
"{} got multiple values for argument 'out'/'y'"
26+
.format(f.__name__)
27+
)
28+
out = kwargs.pop('y')
29+
# NumPy 1.13.0, 2017-04-26
30+
warnings.warn(
31+
"The name of the out argument to {} has changed from `y` to "
32+
"`out`, to match other ufuncs.".format(f.__name__),
33+
DeprecationWarning, stacklevel=3)
34+
return f(x, out=out, **kwargs)
35+
36+
return func
E7EE
37+
38+
39+
@_deprecate_out_named_y
40+
def fix(x, out=None):
1341
"""
1442
Round to nearest integer towards zero.
1543
@@ -46,12 +74,14 @@ def fix(x, y=None):
4674
x = nx.asanyarray(x)
4775
y1 = nx.floor(x)
4876
y2 = nx.ceil(x)
49-
if y is None:
50-
y = nx.asanyarray(y1)
51-
y[...] = nx.where(x >= 0, y1, y2)
52-
return y
77+
if out is None:
78+
out = nx.asanyarray(y1)
79+
out[...] = nx.where(x >= 0, y1, y2)
80+
return out
5381

54-
def isposinf(x, y=None):
82+
83+
@_deprecate_out_named_y
84+
def isposinf(x, out=None):
5585
"""
5686
Test element-wise for positive infinity, return result as bool array.
5787
@@ -64,7 +94,7 @@ def isposinf(x, y=None):
6494
6595
Returns
6696
-------
67-
y : ndarray
97+
out : ndarray
6898
A boolean array with the same dimensions as the input.
6999
If second argument is not supplied then a boolean array is returned
70100
with values True where the corresponding element of the input is
@@ -74,7 +104,7 @@ def isposinf(x, y=None):
74104
If a second argument is supplied the result is stored there. If the
75105
type of that array is a numeric type the result is represented as zeros
76106
and ones, if the type is boolean then as False and True.
77-
The return value `y` is then a refe 23DA rence to that array.
107+
The return value `out` is then a reference to that array.
78108
79109
See Also
80110
--------
@@ -109,25 +139,27 @@ def isposinf(x, y=None):
109139
"""
110140
if y is None:
111141
x = nx.asarray(x)
112-
y = nx.empty(x.shape, dtype=nx.bool_)
113-
nx.logical_and(nx.isinf(x), ~nx.signbit(x), y)
114-
return y
142+
out = nx.empty(x.shape, dtype=nx.bool_)
143+
nx.logical_and(nx.isinf(x), ~nx.signbit(x), out)
144+
return out
145+
115146

116-
def isneginf(x, y=None):
147+
@_deprecate_out_named_y
148+
def isneginf(x, out=None):
117149
"""
118150
Test element-wise for negative infinity, return result as bool array.
119151
120152
Parameters
121153
----------
122154
x : array_like
123155
The input array.
124-
y : array_like, optional
156+
out : array_like, optional
125157
A boolean array with the same shape and type as `x` to store the
126158
result.
127159
128160
Returns
129161
-------
130-
y : ndarray
162+
out : ndarray
131163
A boolean array with the same dimensions as the input.
132164
If second argument is not supplied then a numpy boolean array is
133165
returned with values True where the corresponding element of the
@@ -137,7 +169,7 @@ def isneginf(x, y=None):
137169
If a second argument is supplied the result is stored there. If the
138170
type of that array is a numeric type the result is represented as
139171
zeros and ones, if the type is boolean then as False and True. The
140-
return value `y` is then a reference to that array.
172+
return value `out` is then a reference to that array.
141173
142174
See Also
143175
--------
@@ -170,8 +202,8 @@ def isneginf(x, y=None):
170202
array([1, 0, 0])
171203
172204
"""
173-
if y is None:
205+
if out is None:
174206
x = nx.asarray(x)
175-
y = nx.empty(x.shape, dtype=nx.bool_)
176-
nx.logical_and(nx.isinf(x), nx.signbit(x), y)
177-
return y
207+
out = nx.empty(x.shape, dtype=nx.bool_)
208+
nx.logical_and(nx.isinf(x), nx.signbit(x), out)
209+
return out

0 commit comments

Comments
 (0)
0