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

Skip to content

Commit 86d912b

Browse files
authored
Merge pull request #15053 from charris/revert-object-deprecation
REV: Revert "Merge pull request #14794 from mattip/nep-0034-impl"
2 parents fdd8395 + 6866cc8 commit 86d912b

15 files changed

+51
-124
lines changed

doc/release/upcoming_changes/14794.deprecation.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

doc/release/upcoming_changes/template.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
{% if definitions[category]['showcontent'] %}
1818
{% for text, values in sections[section][category].items() %}
1919
{{ text }}
20-
2120
{{ get_indent(text) }}({{values|join(', ') }})
2221

2322
{% endfor %}

numpy/core/src/multiarray/ctors.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -688,20 +688,14 @@ discover_itemsize(PyObject *s, int nd, int *itemsize, int string_type)
688688
return 0;
689689
}
690690

691-
typedef enum {
692-
DISCOVERED_OK = 0,
693-
DISCOVERED_RAGGED = 1,
694-
DISCOVERED_OBJECT = 2
695-
} discovered_t;
696-
697691
/*
698692
* Take an arbitrary object and discover how many dimensions it
699693
* has, filling in the dimensions as we go.
700694
*/
701695
static int
702696
discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
703697
int stop_at_string, int stop_at_tuple,
704-
discovered_t *out_is_object)
698+
int *out_is_object)
705699
{
706700
PyObject *e;
707701
npy_intp n, i;
@@ -887,7 +881,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
887881
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
888882
PyErr_Clear();
889883
*maxndim = 0;
890-
*out_is_object = DISCOVERED_OBJECT;
884+
*out_is_object = 1;
891885
return 0;
892886
}
893887
else {
@@ -946,7 +940,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
946940
*maxndim = all_elems_maxndim + 1;
947941
if (!all_dimensions_match) {
948942
/* typically results in an array containing variable-length lists */
949-
*out_is_object = DISCOVERED_RAGGED;
943+
*out_is_object = 1;
950944
}
951945
}
952946

@@ -1755,7 +1749,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
17551749

17561750
/* Try to treat op as a list of lists */
17571751
if (!writeable && PySequence_Check(op)) {
1758-
int check_it, stop_at_string, stop_at_tuple;
1752+
int check_it, stop_at_string, stop_at_tuple, is_object;
17591753
int type_num, type;
17601754

17611755
/*
@@ -1805,7 +1799,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
18051799
((*out_dtype)->names || (*out_dtype)->subarray));
18061800

18071801
*out_ndim = NPY_MAXDIMS;
1808-
discovered_t is_object = DISCOVERED_OK;
1802+
is_object = 0;
18091803
if (discover_dimensions(
18101804
op, out_ndim, out_dims, check_it,
18111805
stop_at_string, stop_at_tuple, &is_object) < 0) {
@@ -1822,17 +1816,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
18221816
return 0;
18231817
}
18241818
/* If object arrays are forced */
1825-
if (is_object != DISCOVERED_OK) {
1826-
if (is_object == DISCOVERED_RAGGED && requested_dtype == NULL) {
1827-
/* NumPy 1.18, 2019-11-01 */
1828-
if (DEPRECATE("Creating an ndarray with automatic object "
1829-
"dtype is deprecated, use dtype=object if you intended "
1830-
"it, otherwise specify an exact dtype") < 0)
1831-
{
1832-
return -1;
1833-
}
1834-
}
1835-
/* either DISCOVERED_OBJECT or there is a requested_dtype */
1819+
if (is_object) {
18361820
Py_DECREF(*out_dtype);
18371821
*out_dtype = PyArray_DescrFromType(NPY_OBJECT);
18381822
if (*out_dtype == NULL) {

numpy/core/tests/test_deprecations.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,3 @@ class TestNonZero(_DeprecationTestCase):
568568
def test_zerod(self):
569569
self.assert_deprecated(lambda: np.nonzero(np.array(0)))
570570
self.assert_deprecated(lambda: np.nonzero(np.array(1)))
571-
572-
573-
class TestRaggedArray(_DeprecationTestCase):
574-
# 2019-11-29 1.18.0
575-
def test_deprecate_ragged_arrays(self):
576-
# NEP 34 deprecated automatic object dtype when creating ragged
577-
# arrays. Also see the "ragged" tests in `test_multiarray`
578-
arg = [1, [2, 3]]
579-
self.assert_deprecated(np.array, args=(arg,))
580-

numpy/core/tests/test_multiarray.py

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def test_array(self):
448448
assert_equal(r, np.ones((2, 6, 6)))
449449

450450
d = np.ones((6, ))
451-
r = np.array([[d, d + 1], d + 2], dtype=object)
451+
r = np.array([[d, d + 1], d + 2])
452452
assert_equal(len(r), 2)
453453
assert_equal(r[0], [d, d + 1])
454454
assert_equal(r[1], d + 2)
@@ -1051,60 +1051,34 @@ def test_array_too_big(self):
10511051
assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
10521052
shape=(max_bytes//itemsize + 1,), dtype=dtype)
10531053

1054-
def _ragged_creation(self, seq):
1055-
# without dtype=object, the ragged object should raise
1056-
with assert_warns(DeprecationWarning):
1057-
a = np.array(seq)
1058-
b = np.array(seq, dtype=object)
1059-
assert_equal(a, b)
1060-
return b
1061-
1062-
def test_ragged_ndim_object(self):
1054+
def test_jagged_ndim_object(self):
10631055
# Lists of mismatching depths are treated as object arrays
1064-
a = self._ragged_creation([[1], 2, 3])
1056+
a = np.array([[1], 2, 3])
10651057
assert_equal(a.shape, (3,))
10661058
assert_equal(a.dtype, object)
10671059

1068-
a = self._ragged_creation([1, [2], 3])
1060+
a = np.array([1, [2], 3])
10691061
assert_equal(a.shape, (3,))
10701062
assert_equal(a.dtype, object)
10711063

1072-
a = self._ragged_creation([1, 2, [3]])
1064+
a = np.array([1, 2, [3]])
10731065
assert_equal(a.shape, (3,))
10741066
assert_equal(a.dtype, object)
10751067

1076-
def test_ragged_shape_object(self):
1068+
def test_jagged_shape_object(self):
10771069
# The jagged dimension of a list is turned into an object array
1078-
a = self._ragged_creation([[1, 1], [2], [3]])
1070+
a = np.array([[1, 1], [2], [3]])
1071+
assert_equal(a.shape, (3,))
1072+
assert_equal(a.dtype, object)
1073+
1074+
a = np.array([[1], [2, 2], [3]])
10791075
assert_equal(a.shape, (3,))
10801076
assert_equal(a.dtype, object)
10811077

1082-
a = self._ragged_creation([[1], [2, 2], [3]])
1078+
a = np.array([[1], [2], [3, 3]])
10831079
assert_equal(a.shape, (3,))
10841080
assert_equal(a.dtype, object)
10851081

1086-
a = self._ragged_creation([[1], [2], [3, 3]])
1087-
assert a.shape == (3,)
1088-
assert a.dtype == object
1089-
1090-
def test_array_of_ragged_array(self):
1091-
outer = np.array([None, None])
1092-
outer[0] = outer[1] = np.array([1, 2, 3])
1093-
assert np.array(outer).shape == (2,)
1094-
assert np.array([outer]).shape == (1, 2)
1095-
1096-
outer_ragged = np.array([None, None])
1097-
outer_ragged[0] = np.array([1, 2, 3])
1098-
outer_ragged[1] = np.array([1, 2, 3, 4])
1099-
# should both of these emit deprecation warnings?
1100-
assert np.array(outer_ragged).shape == (2,)
1101-
assert np.array([outer_ragged]).shape == (1, 2,)
1102-
1103-
def test_deep_nonragged_object(self):
1104-
# None of these should raise, even though they are missing dtype=object
1105-
a = np.array([[[Decimal(1)]]])
1106-
a = np.array([1, Decimal(1)])
1107-
a = np.array([[1], [Decimal(1)]])
11081082

11091083
class TestStructured(object):
11101084
def test_subarray_field_access(self):

numpy/core/tests/test_numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ def test_array_method(self):
12111211

12121212
def test_nonzero_invalid_object(self):
12131213
# gh-9295
1214-
a = np.array([np.array([1, 2]), 3], dtype=object)
1214+
a = np.array([np.array([1, 2]), 3])
12151215
assert_raises(ValueError, np.nonzero, a)
12161216

12171217
class BoolErrors:

numpy/core/tests/test_regression.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,21 +1367,21 @@ def test_fromiter_bytes(self):
13671367
def test_array_from_sequence_scalar_array(self):
13681368
# Ticket #1078: segfaults when creating an array with a sequence of
13691369
# 0d arrays.
1370-
a = np.array((np.ones(2), np.array(2)), dtype=object)
1370+
a = np.array((np.ones(2), np.array(2)))
13711371
assert_equal(a.shape, (2,))
13721372
assert_equal(a.dtype, np.dtype(object))
13731373
assert_equal(a[0], np.ones(2))
13741374
assert_equal(a[1], np.array(2))
13751375

1376-
a = np.array(((1,), np.array(1)), dtype=object)
1376+
a = np.array(((1,), np.array(1)))
13771377
assert_equal(a.shape, (2,))
13781378
assert_equal(a.dtype, np.dtype(object))
13791379
assert_equal(a[0], (1,))
13801380
assert_equal(a[1], np.array(1))
13811381

13821382
def test_array_from_sequence_scalar_array2(self):
13831383
# Ticket #1081: weird array with strange input...
1384-
t = np.array([np.array([]), np.array(0, object)], dtype=object)
1384+
t = np.array([np.array([]), np.array(0, object)])
13851385
assert_equal(t.shape, (2,))
13861386
assert_equal(t.dtype, np.dtype(object))
13871387

@@ -2290,10 +2290,9 @@ def f(x):
22902290
x[0], x[-1] = x[-1], x[0]
22912291

22922292
uf = np.frompyfunc(f, 1, 0)
2293-
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]], dtype=object)
2293+
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
22942294
assert_equal(uf(a), ())
2295-
expected = np.array([[3, 2, 1], [5, 4], [9, 7, 8, 6]], dtype=object)
2296-
assert_array_equal(a, expected)
2295+
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
22972296

22982297
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
22992298
def test_leak_in_structured_dtype_comparison(self):

numpy/core/tests/test_ufunc.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,18 +1124,14 @@ def test_object_array_accumulate_inplace(self):
11241124
# Twice reproduced also for tuples:
11251125
np.add.accumulate(arr, out=arr)
11261126
np.add.accumulate(arr, out=arr)
1127-
assert_array_equal(arr,
1128-
np.array([[1]*i for i in [1, 3, 6, 10]], dtype=object),
1129-
)
1127+
assert_array_equal(arr, np.array([[1]*i for i in [1, 3, 6, 10]]))
11301128

11311129
# And the same if the axis argument is used
11321130
arr = np.ones((2, 4), dtype=object)
11331131
arr[0, :] = [[2] for i in range(4)]
11341132
np.add.accumulate(arr, out=arr, axis=-1)
11351133
np.add.accumulate(arr, out=arr, axis=-1)
1136-
assert_array_equal(arr[0, :],
1137-
np.array([[2]*i for i in [1, 3, 6, 10]], dtype=object),
1138-
)
1134+
assert_array_equal(arr[0, :], np.array([[2]*i for i in [1, 3, 6, 10]]))
11391135

11401136
def test_object_array_reduceat_inplace(self):
11411137
# Checks that in-place reduceats work, see also gh-7465

numpy/lib/tests/test_arraypad.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,29 +1262,24 @@ def test_negative_pad_width(self, pad_width, mode):
12621262
with pytest.raises(ValueError, match=match):
12631263
np.pad(arr, pad_width, mode)
12641264

1265-
@pytest.mark.parametrize("pad_width, dtype", [
1266-
("3", None),
1267-
("word", None),
1268-
(None, None),
1269-
(object(), None),
1270-
(3.4, None),
1271-
(((2, 3, 4), (3, 2)), object),
1272-
(complex(1, -1), None),
1273-
(((-2.1, 3), (3, 2)), None),
1265+
@pytest.mark.parametrize("pad_width", [
1266+
"3",
1267+
"word",
1268+
None,
1269+
object(),
1270+
3.4,
1271+
((2, 3, 4), (3, 2)), # dtype=object (tuple)
1272+
complex(1, -1),
1273+
((-2.1, 3), (3, 2)),
12741274
])
12751275
@pytest.mark.parametrize("mode", _all_modes.keys())
1276-
def test_bad_type(self, pad_width, dtype, mode):
1276+
def test_bad_type(self, pad_width, mode):
12771277
arr = np.arange(30).reshape((6, 5))
12781278
match = "`pad_width` must be of integral type."
1279-
if dtype is not None:
1280-
# avoid DeprecationWarning when not specifying dtype
1281-
with pytest.raises(TypeError, match=match):
1282-
np.pad(arr, np.array(pad_width, dtype=dtype), mode)
1283-
else:
1284-
with pytest.raises(TypeError, match=match):
1285-
np.pad(arr, pad_width, mode)
1286-
with pytest.raises(TypeError, match=match):
1287-
np.pad(arr, np.array(pad_width), mode)
1279+
with pytest.raises(TypeError, match=match):
1280+
np.pad(arr, pad_width, mode)
1281+
with pytest.raises(TypeError, match=match):
1282+
np.pad(arr, np.array(pad_width), mode)
12881283

12891284
def test_pad_width_as_ndarray(self):
12901285
a = np.arange(12)

numpy/lib/tests/test_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def test_unicode_and_bytes_fmt(self, fmt, iotype):
580580
def test_large_zip(self):
581581
# The test takes at least 6GB of memory, writes a file larger than 4GB
582582
test_data = np.asarray([np.random.rand(np.random.randint(50,100),4)
583-
for i in range(800000)], dtype=object)
583+
for i in range(800000)])
584584
with tempdir() as tmpdir:
585585
np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data)
586586

numpy/ma/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,8 @@ def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
28282828
elif isinstance(data, (tuple, list)):
28292829
try:
28302830
# If data is a sequence of masked array
2831-
mask = np.array([getmaskarray(np.asanyarray(m, dtype=mdtype))
2832-
for m in data], dtype=mdtype)
2831+
mask = np.array([getmaskarray(m) for m in data],
2832+
dtype=mdtype)
28332833
except ValueError:
28342834
# If data is nested
28352835
mask = nomask

numpy/ma/tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ def test_mvoid_multidim_print(self):
936936
def test_object_with_array(self):
937937
mx1 = masked_array([1.], mask=[True])
938938
mx2 = masked_array([1., 2.])
939-
mx = masked_array([mx1, mx2], mask=[False, True], dtype=object)
939+
mx = masked_array([mx1, mx2], mask=[False, True])
940940
assert_(mx[0] is mx1)
941941
assert_(mx[1] is not mx2)
942942
assert_(np.all(mx[1].data == mx2.data))

numpy/random/tests/test_generator_mt19937_regressions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ def test_shuffle_mixed_dimension(self):
5656
[1, (2, 2), (3, 3), None],
5757
[(1, 1), 2, 3, None]]:
5858
mt19937 = Generator(MT19937(12345))
59-
shuffled = np.array(t, dtype=object)
59+
shuffled = list(t)
6060
mt19937.shuffle(shuffled)
61-
expected = np.array([t[2], t[0], t[3], t[1]], dtype=object)
62-
assert_array_equal(np.array(shuffled, dtype=object), expected)
61+
assert_array_equal(shuffled, [t[2], t[0], t[3], t[1]])
6362

6463
def test_call_within_randomstate(self):
6564
# Check that custom BitGenerator does not call into global state
@@ -119,7 +118,7 @@ def test_shuffle_of_array_of_objects(self):
119118
# a segfault on garbage collection.
120119
# See gh-7719
121120
mt19937 = Generator(MT19937(1234))
122-
a = np.array([np.arange(1), np.arange(4)], dtype=object)
121+
a = np.array([np.arange(1), np.arange(4)])
123122

124123
for _ in range(1000):
125124
mt19937.shuffle(a)

numpy/random/tests/test_randomstate_regression.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def test_shuffle_mixed_dimension(self):
6868
random.seed(12345)
6969
shuffled = list(t)
7070
random.shuffle(shuffled)
71-
expected = np.array([t[0], t[3], t[1], t[2]], dtype=object)
72-
assert_array_equal(np.array(shuffled, dtype=object), expected)
71+
assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])
7372

7473
def test_call_within_randomstate(self):
7574
# Check that custom RandomState does not call into global state
@@ -129,7 +128,7 @@ def test_shuffle_of_array_of_objects(self):
129128
# a segfault on garbage collection.
130129
# See gh-7719
131130
random.seed(1234)
132-
a = np.array([np.arange(1), np.arange(4)], dtype=object)
131+
a = np.array([np.arange(1), np.arange(4)])
133132

134133
for _ in range(1000):
135134
random.shuffle(a)

numpy/random/tests/test_regression.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def test_shuffle_mixed_dimension(self):
6666
np.random.seed(12345)
6767
shuffled = list(t)
6868
random.shuffle(shuffled)
69-
expected = np.array([t[0], t[3], t[1], t[2]], dtype=object)
70-
assert_array_equal(np.array(shuffled, dtype=object), expected)
69+
assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])
7170

7271
def test_call_within_randomstate(self):
7372
# Check that custom RandomState does not call into global state
@@ -127,7 +126,7 @@ def test_shuffle_of_array_of_objects(self):
127126
# a segfault on garbage collection.
128127
# See gh-7719
129128
np.random.seed(1234)
130-
a = np.array([np.arange(1), np.arange(4)], dtype=object)
129+
a = np.array([np.arange(1), np.arange(4)])
131130

132131
for _ in range(1000):
133132
np.random.shuffle(a)

0 commit comments

Comments
 (0)