Skip to content

Commit dc5419f

Browse files
authored
Merge pull request numpy#27296 from DimitriPapadopoulos/C4
MAINT: Start applying ruff/flake8-comprehensions rules (C4)
2 parents 6fbd06b + 486ad9b commit dc5419f

File tree

22 files changed

+77
-82
lines changed

22 files changed

+77
-82
lines changed

benchmarks/benchmarks/bench_linalg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def time_tensordot_a_b_axes_1_0_0_1(self):
7272

7373

7474
class Linalg(Benchmark):
75-
params = sorted(list(set(TYPES1) - set(['float16'])))
75+
params = sorted(set(TYPES1) - set(['float16']))
7676
param_names = ['dtype']
7777

7878
def setup(self, typename):

doc/source/reference/random/performance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
table = table.T
6060
table = table.reindex(columns)
6161
table = table.T
62-
table = table.reindex([k for k in funcs], axis=0)
62+
table = table.reindex(list(funcs), axis=0)
6363
print(table.to_csv(float_format='%0.1f'))
6464

6565

numpy/_core/einsumfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ def einsum_path(*operands, optimize='greedy', einsum_call=False):
964964
# Build contraction tuple (positions, gemm, einsum_str, remaining)
965965
for cnum, contract_inds in enumerate(path):
966966
# Make sure we remove inds from right to left
967-
contract_inds = tuple(sorted(list(contract_inds), reverse=True))
967+
contract_inds = tuple(sorted(contract_inds, reverse=True))
968968

969969
contract = _find_contraction(contract_inds, input_sets, output_set)
970970
out_inds, input_sets, idx_removed, idx_contract = contract

numpy/_core/tests/test_cython.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,8 @@ def test_multiiter_fields(install_temp, arrays):
215215
assert bcast.shape == checks.get_multiiter_shape(bcast)
216216
assert bcast.index == checks.get_multiiter_current_index(bcast)
217217
assert all(
218-
[
219-
x.base is y.base
220-
for x, y in zip(bcast.iters, checks.get_multiiter_iters(bcast))
221-
]
218+
x.base is y.base
219+
for x, y in zip(bcast.iters, checks.get_multiiter_iters(bcast))
222220
)
223221

224222

@@ -278,10 +276,8 @@ def test_npyiter_api(install_temp):
278276
x is y for x, y in zip(checks.get_npyiter_operands(it), it.operands)
279277
)
280278
assert all(
281-
[
282-
np.allclose(x, y)
283-
for x, y in zip(checks.get_npyiter_itviews(it), it.itviews)
284-
]
279+
np.allclose(x, y)
280+
for x, y in zip(checks.get_npyiter_itviews(it), it.itviews)
285281
)
286282

287283

numpy/_core/tests/test_multiarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5478,7 +5478,7 @@ def test_roundtrip_str(self, x):
54785478

54795479
def test_roundtrip_repr(self, x):
54805480
x = x.real.ravel()
5481-
s = "@".join(map(lambda x: repr(x)[11:-1], x))
5481+
s = "@".join((repr(x)[11:-1] for x in x))
54825482
y = np.fromstring(s, sep="@")
54835483
assert_array_equal(x, y)
54845484

numpy/_core/tests/test_nditer.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ def test_iter_best_order():
9797
aview = a.reshape(shape)[dirs_index]
9898
# C-order
9999
i = nditer(aview, [], [['readonly']])
100-
assert_equal([x for x in i], a)
100+
assert_equal(list(i), a)
101101
# Fortran-order
102102
i = nditer(aview.T, [], [['readonly']])
103-
assert_equal([x for x in i], a)
103+
assert_equal(list(i), a)
104104
# Other order
105105
if len(shape) > 2:
106106
i = nditer(aview.swapaxes(0, 1), [], [['readonly']])
107-
assert_equal([x for x in i], a)
107+
assert_equal(list(i), a)
108108

109109
def test_iter_c_order():
110110
# Test forcing C order
@@ -123,14 +123,14 @@ def test_iter_c_order():
123123
aview = a.reshape(shape)[dirs_index]
124124
# C-order
125125
i = nditer(aview, order='C')
126-
assert_equal([x for x in i], aview.ravel(order='C'))
126+
assert_equal(list(i), aview.ravel(order='C'))
127127
# Fortran-order
128128
i = nditer(aview.T, order='C')
129-
assert_equal([x for x in i], aview.T.ravel(order='C'))
129+
assert_equal(list(i), aview.T.ravel(order='C'))
130130
# Other order
131131
if len(shape) > 2:
132132
i = nditer(aview.swapaxes(0, 1), order='C')
133-
assert_equal([x for x in i],
133+
assert_equal(list(i),
134134
aview.swapaxes(0, 1).ravel(order='C'))
135135

136136
def test_iter_f_order():
@@ -150,14 +150,14 @@ def test_iter_f_order():
150150
aview = a.reshape(shape)[dirs_index]
151151
# C-order
152152
i = nditer(aview, order='F')
153-
assert_equal([x for x in i], aview.ravel(order='F'))
153+
assert_equal(list(i), aview.ravel(order='F'))
154154
# Fortran-order
155155
i = nditer(aview.T, order='F')
156-
assert_equal([x for x in i], aview.T.ravel(order='F'))
156+
assert_equal(list(i), aview.T.ravel(order='F'))
157157
# Other order
158158
if len(shape) > 2:
159159
i = nditer(aview.swapaxes(0, 1), order='F')
160-
assert_equal([x for x in i],
160+
assert_equal(list(i),
161161
aview.swapaxes(0, 1).ravel(order='F'))
162162

163163
def test_iter_c_or_f_order():
@@ -177,14 +177,14 @@ def test_iter_c_or_f_order():
177177
aview = a.reshape(shape)[dirs_index]
178178
# C-order
179179
i = nditer(aview, order='A')
180-
assert_equal([x for x in i], aview.ravel(order='A'))
180+
assert_equal(list(i), aview.ravel(order='A'))
181181
# Fortran-order
182182
i = nditer(aview.T, order='A')
183-
assert_equal([x for x in i], aview.T.ravel(order='A'))
183+
assert_equal(list(i), aview.T.ravel(order='A'))
184184
# Other order
185185
if len(shape) > 2:
186186
i = nditer(aview.swapaxes(0, 1), order='A')
187-
assert_equal([x for x in i],
187+
assert_equal(list(i),
188188
aview.swapaxes(0, 1).ravel(order='A'))
189189

190190
def test_nditer_multi_index_set():
@@ -195,7 +195,7 @@ def test_nditer_multi_index_set():
195195
# Removes the iteration on two first elements of a[0]
196196
it.multi_index = (0, 2,)
197197

198-
assert_equal([i for i in it], [2, 3, 4, 5])
198+
assert_equal(list(it), [2, 3, 4, 5])
199199

200200
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
201201
def test_nditer_multi_index_set_refcount():
@@ -271,7 +271,7 @@ def test_iter_best_order_multi_index_3d():
271271
assert_equal(iter_multi_index(i),
272272
[(0, 2, 0), (0, 2, 1), (0, 1, 0), (0, 1, 1), (0, 0, 0), (0, 0, 1),
273273
(1, 2, 0), (1, 2, 1), (1, 1, 0), (1, 1, 1), (1, 0, 0), (1, 0, 1)])
274-
i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['multi_index'], [['readonly']])
274+
i = nditer(a.reshape(2, 3, 2)[:, :, ::-1], ['multi_index'], [['readonly']])
275275
assert_equal(iter_multi_index(i),
276276
[(0, 0, 1), (0, 0, 0), (0, 1, 1), (0, 1, 0), (0, 2, 1), (0, 2, 0),
277277
(1, 0, 1), (1, 0, 0), (1, 1, 1), (1, 1, 0), (1, 2, 1), (1, 2, 0)])
@@ -286,7 +286,7 @@ def test_iter_best_order_multi_index_3d():
286286
assert_equal(iter_multi_index(i),
287287
[(0, 2, 0), (1, 2, 0), (0, 1, 0), (1, 1, 0), (0, 0, 0), (1, 0, 0),
288288
(0, 2, 1), (1, 2, 1), (0, 1, 1), (1, 1, 1), (0, 0, 1), (1, 0, 1)])
289-
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1],
289+
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, :, ::-1],
290290
['multi_index'], [['readonly']])
291291
assert_equal(iter_multi_index(i),
292292
[(0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1), (0, 2, 1), (1, 2, 1),
@@ -352,7 +352,7 @@ def test_iter_best_order_c_index_3d():
352352
i = nditer(a.reshape(2, 3, 2)[:, ::-1], ['c_index'], [['readonly']])
353353
assert_equal(iter_indices(i),
354354
[4, 5, 2, 3, 0, 1, 10, 11, 8, 9, 6, 7])
355-
i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['c_index'], [['readonly']])
355+
i = nditer(a.reshape(2, 3, 2)[:, :, ::-1], ['c_index'], [['readonly']])
356356
assert_equal(iter_indices(i),
357357
[1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10])
358358
# 3D reversed Fortran-order
@@ -364,7 +364,7 @@ def test_iter_best_order_c_index_3d():
364364
['c_index'], [['readonly']])
365365
assert_equal(iter_indices(i),
366366
[4, 10, 2, 8, 0, 6, 5, 11, 3, 9, 1, 7])
367-
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1],
367+
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, :, ::-1],
368368
['c_index'], [['readonly']])
369369
assert_equal(iter_indices(i),
370370
[1, 7, 3, 9, 5, 11, 0, 6, 2, 8, 4, 10])
@@ -429,7 +429,7 @@ def test_iter_best_order_f_index_3d():
429429
i = nditer(a.reshape(2, 3, 2)[:, ::-1], ['f_index'], [['readonly']])
430430
assert_equal(iter_indices(i),
431431
[4, 10, 2, 8, 0, 6, 5, 11, 3, 9, 1, 7])
432-
i = nditer(a.reshape(2, 3, 2)[:,:, ::-1], ['f_index'], [['readonly']])
432+
i = nditer(a.reshape(2, 3, 2)[:, :, ::-1], ['f_index'], [['readonly']])
433433
assert_equal(iter_indices(i),
434434
[6, 0, 8, 2, 10, 4, 7, 1, 9, 3, 11, 5])
435435
# 3D reversed Fortran-order
@@ -441,7 +441,7 @@ def test_iter_best_order_f_index_3d():
441441
['f_index'], [['readonly']])
442442
assert_equal(iter_indices(i),
443443
[4, 5, 2, 3, 0, 1, 10, 11, 8, 9, 6, 7])
444-
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:,:, ::-1],
444+
i = nditer(a.reshape(2, 3, 2).copy(order='F')[:, :, ::-1],
445445
['f_index'], [['readonly']])
446446
assert_equal(iter_indices(i),
447447
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5])
@@ -481,15 +481,15 @@ def test_iter_no_inner_dim_coalescing():
481481

482482
# Skipping the last element in a dimension prevents coalescing
483483
# with the next-bigger dimension
484-
a = arange(24).reshape(2, 3, 4)[:,:, :-1]
484+
a = arange(24).reshape(2, 3, 4)[:, :, :-1]
485485
i = nditer(a, ['external_loop'], [['readonly']])
486486
assert_equal(i.ndim, 2)
487487
assert_equal(i[0].shape, (3,))
488-
a = arange(24).reshape(2, 3, 4)[:, :-1,:]
488+
a = arange(24).reshape(2, 3, 4)[:, :-1, :]
489489
i = nditer(a, ['external_loop'], [['readonly']])
490490
assert_equal(i.ndim, 2)
491491
assert_equal(i[0].shape, (8,))
492-
a = arange(24).reshape(2, 3, 4)[:-1,:,:]
492+
a = arange(24).reshape(2, 3, 4)[:-1, :, :]
493493
i = nditer(a, ['external_loop'], [['readonly']])
494494
assert_equal(i.ndim, 1)
495495
assert_equal(i[0].shape, (12,))
@@ -761,9 +761,9 @@ def test_iter_flags_errors():
761761
a.flags.writeable = True
762762
# Multi-indices available only with the multi_index flag
763763
i = nditer(arange(6), [], [['readonly']])
764-
assert_raises(ValueError, lambda i:i.multi_index, i)
764+
assert_raises(ValueError, lambda i: i.multi_index, i)
765765
# Index available only with an index flag
766-
assert_raises(ValueError, lambda i:i.index, i)
766+
assert_raises(ValueError, lambda i: i.index, i)
767767
# GotoCoords and GotoIndex incompatible with buffering or no_inner
768768

769769
def assign_multi_index(i):
@@ -911,7 +911,7 @@ def test_iter_array_cast():
911911
# The memory layout of the temporary should match a (a is (48,4,16))
912912
# except negative strides get flipped to positive strides.
913913
assert_equal(i.operands[0].strides, (96, 8, 32))
914-
a = a[::-1,:, ::-1]
914+
a = a[::-1, :, ::-1]
915915
i = nditer(a, [], [['readonly', 'copy']],
916916
casting='safe',
917917
op_dtypes=[np.dtype('f8')])
@@ -1049,7 +1049,7 @@ def test_iter_scalar_cast_errors():
10491049
def test_iter_object_arrays_basic():
10501050
# Check that object arrays work
10511051

1052-
obj = {'a':3,'b':'d'}
1052+
obj = {'a': 3, 'b': 'd'}
10531053
a = np.array([[1, 2, 3], None, obj, None], dtype='O')
10541054
if HAS_REFCOUNT:
10551055
rc = sys.getrefcount(obj)
@@ -1677,12 +1677,12 @@ def test_iter_remove_axis():
16771677

16781678
i = nditer(a, ['multi_index'])
16791679
i.remove_axis(1)
1680-
assert_equal([x for x in i], a[:, 0,:].ravel())
1680+
assert_equal(list(i), a[:, 0, :].ravel())
16811681

1682-
a = a[::-1,:,:]
1682+
a = a[::-1, :, :]
16831683
i = nditer(a, ['multi_index'])
16841684
i.remove_axis(0)
1685-
assert_equal([x for x in i], a[0,:,:].ravel())
1685+
assert_equal(list(i), a[0, :, :].ravel())
16861686

16871687
def test_iter_remove_multi_index_inner_loop():
16881688
# Check that removing multi-index support works
@@ -1695,13 +1695,13 @@ def test_iter_remove_multi_index_inner_loop():
16951695
assert_equal(i.itviews[0].shape, (2, 3, 4))
16961696

16971697
# Removing the multi-index tracking causes all dimensions to coalesce
1698-
before = [x for x in i]
1698+
before = list(i)
16991699
i.remove_multi_index()
1700-
after = [x for x in i]
1700+
after = list(i)
17011701

17021702
assert_equal(before, after)
17031703
assert_equal(i.ndim, 1)
1704-
assert_raises(ValueError, lambda i:i.shape, i)
1704+
assert_raises(ValueError, lambda i: i.shape, i)
17051705
assert_equal(i.itviews[0].shape, (24,))
17061706

17071707
# Removing the inner loop means there's just one iteration
@@ -1847,9 +1847,9 @@ def test_iter_buffering_delayed_alloc():
18471847
casting='unsafe',
18481848
op_dtypes='f4')
18491849
assert_(i.has_delayed_bufalloc)
1850-
assert_raises(ValueError, lambda i:i.multi_index, i)
1851-
assert_raises(ValueError, lambda i:i[0], i)
1852-
assert_raises(ValueError, lambda i:i[0:2], i)
1850+
assert_raises(ValueError, lambda i: i.multi_index, i)
1851+
assert_raises(ValueError, lambda i: i[0], i)
1852+
assert_raises(ValueError, lambda i: i[0:2], i)
18531853

18541854
def assign_iter(i):
18551855
i[0] = 0
@@ -2240,7 +2240,7 @@ def test_iter_buffered_cast_subarray():
22402240
for x in i:
22412241
assert_equal(x['a'][:2, 0], a[count]['a'][:, 0])
22422242
assert_equal(x['a'][:2, 1], a[count]['a'][:, 0])
2243-
assert_equal(x['a'][2,:], [0, 0])
2243+
assert_equal(x['a'][2, :], [0, 0])
22442244
count += 1
22452245

22462246
# matrix -> matrix (truncates and zero-pads)
@@ -2256,7 +2256,7 @@ def test_iter_buffered_cast_subarray():
22562256
for x in i:
22572257
assert_equal(x['a'][:2, 0], a[count]['a'][:, 0])
22582258
assert_equal(x['a'][:2, 1], a[count]['a'][:, 1])
2259-
assert_equal(x['a'][2,:], [0, 0])
2259+
assert_equal(x['a'][2, :], [0, 0])
22602260
count += 1
22612261

22622262
def test_iter_buffering_badwriteback():
@@ -2549,7 +2549,7 @@ def test_0d(self):
25492549
vals = []
25502550
for x in i:
25512551
for y in j:
2552-
vals.append([z for z in k])
2552+
vals.append(list(k))
25532553
assert_equal(vals, [[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]])
25542554

25552555
def test_iter_nested_iters_dtype_buffered(self):
@@ -2689,11 +2689,11 @@ def test_iter_buffering_reduction():
26892689
assert_equal(it[0], [1, 2, 1, 2])
26902690

26912691
# Iterator inner loop should take argument contiguity into account
2692-
x = np.ones((7, 13, 8), np.int8)[4:6,1:11:6,1:5].transpose(1, 2, 0)
2692+
x = np.ones((7, 13, 8), np.int8)[4:6, 1:11:6, 1:5].transpose(1, 2, 0)
26932693
x[...] = np.arange(x.size).reshape(x.shape)
26942694
y_base = np.arange(4*4, dtype=np.int8).reshape(4, 4)
26952695
y_base_copy = y_base.copy()
2696-
y = y_base[::2,:,None]
2696+
y = y_base[::2, :, None]
26972697

26982698
it = np.nditer([y, x],
26992699
['buffered', 'external_loop', 'reduce_ok'],
@@ -3174,7 +3174,7 @@ def test_close_equivalent():
31743174
def add_close(x, y, out=None):
31753175
addop = np.add
31763176
it = np.nditer([x, y, out], [],
3177-
[['readonly'], ['readonly'], ['writeonly','allocate']])
3177+
[['readonly'], ['readonly'], ['writeonly', 'allocate']])
31783178
for (a, b, c) in it:
31793179
addop(a, b, out=c)
31803180
ret = it.operands[2]
@@ -3184,7 +3184,7 @@ def add_close(x, y, out=None):
31843184
def add_context(x, y, out=None):
31853185
addop = np.add
31863186
it = np.nditer([x, y, out], [],
3187-
[['readonly'], ['readonly'], ['writeonly','allocate']])
3187+
[['readonly'], ['readonly'], ['writeonly', 'allocate']])
31883188
with it:
31893189
for (a, b, c) in it:
31903190
addop(a, b, out=c)

numpy/_core/tests/test_regression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def test_rec_iterate(self):
290290
x = np.rec.array([(1, 1.1, '1.0'),
291291
(2, 2.2, '2.0')], dtype=descr)
292292
x[0].tolist()
293-
[i for i in x[0]]
293+
list(x[0])
294294

295295
def test_unicode_string_comparison(self):
296296
# Ticket #190
@@ -1028,7 +1028,7 @@ def __del__(self):
10281028
def test_mem_fromiter_invalid_dtype_string(self):
10291029
x = [1, 2, 3]
10301030
assert_raises(ValueError,
1031-
np.fromiter, [xi for xi in x], dtype='S')
1031+
np.fromiter, list(x), dtype='S')
10321032

10331033
def test_reduce_big_object_array(self):
10341034
# Ticket #713

numpy/_core/tests/test_shape_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_generator(self):
156156
with pytest.raises(TypeError, match="arrays to stack must be"):
157157
hstack(np.arange(3) for _ in range(2))
158158
with pytest.raises(TypeError, match="arrays to stack must be"):
159-
hstack(map(lambda x: x, np.ones((3, 2))))
159+
hstack((x for x in np.ones((3, 2))))
160160

161161
def test_casting_and_dtype(self):
162162
a = np.array([1, 2, 3])
@@ -438,7 +438,7 @@ def test_stack():
438438
assert_array_equal(np.stack((a, b)), r1)
439439
assert_array_equal(np.stack((a, b), axis=1), r1.T)
440440
# all input types
441-
assert_array_equal(np.stack(list([a, b])), r1)
441+
assert_array_equal(np.stack([a, b]), r1)
442442
assert_array_equal(np.stack(array([a, b])), r1)
443443
# all shapes for 1d input
444444
arrays = [np.random.randn(3) for _ in range(10)]

numpy/f2py/_backends/_meson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def include_substitution(self) -> None:
117117
def fortran_args_substitution(self) -> None:
118118
if self.fortran_args:
119119
self.substitutions["fortran_args"] = (
120-
f"{self.indent}fortran_args: [{', '.join([arg for arg in self.fortran_args])}],"
120+
f"{self.indent}fortran_args: [{', '.join(list(self.fortran_args))}],"
121121
)
122122
else:
123123
self.substitutions["fortran_args"] = ""

0 commit comments

Comments
 (0)