Skip to content

Commit 5052807

Browse files
authored
Merge pull request numpy#26882 from DimitriPapadopoulos/UP
MAINT: start applying ruff/pyupgrade rules (UP)
2 parents b8b0d88 + 5e05384 commit 5052807

24 files changed

+63
-64
lines changed

benchmarks/benchmarks/bench_ufunc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
all_ufuncs = (getattr(np, name, None) for name in dir(np))
3333
all_ufuncs = set(filter(lambda f: isinstance(f, np.ufunc), all_ufuncs))
34-
bench_ufuncs = set((getattr(np, name, None) for name in ufuncs))
34+
bench_ufuncs = set(getattr(np, name, None) for name in ufuncs)
3535

3636
missing_ufuncs = all_ufuncs - bench_ufuncs
3737
if len(missing_ufuncs) > 0:
@@ -497,7 +497,7 @@ def time_floor_divide_int(self, dtype, size):
497497
class Scalar(Benchmark):
498498
def setup(self):
499499
self.x = np.asarray(1.0)
500-
self.y = np.asarray((1.0 + 1j))
500+
self.y = np.asarray(1.0 + 1j)
501501
self.z = complex(1.0, 1.0)
502502

503503
def time_add_scalar(self):

numpy/_core/tests/test_array_coercion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def subclass(a):
3838

3939
yield subclass
4040

41-
class _SequenceLike():
41+
class _SequenceLike:
4242
# Older NumPy versions, sometimes cared whether a protocol array was
4343
# also _SequenceLike. This shouldn't matter, but keep it for now
4444
# for __array__ and not the others.

numpy/_core/tests/test_indexing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ def _get_multi_index(self, arr, indices):
983983
elif indx is None:
984984
# this is like taking a slice with one element from a new axis:
985985
indices.append(['n', np.array([0], dtype=np.intp)])
986-
arr = arr.reshape((arr.shape[:ax] + (1,) + arr.shape[ax:]))
986+
arr = arr.reshape(arr.shape[:ax] + (1,) + arr.shape[ax:])
987987
continue
988988
if isinstance(indx, np.ndarray) and indx.dtype == bool:
989989
if indx.shape != arr.shape[ax:ax+indx.ndim]:
@@ -998,9 +998,9 @@ def _get_multi_index(self, arr, indices):
998998
flat_indx = np.array([0]*indx.sum(), dtype=np.intp)
999999
# concatenate axis into a single one:
10001000
if indx.ndim != 0:
1001-
arr = arr.reshape((arr.shape[:ax]
1001+
arr = arr.reshape(arr.shape[:ax]
10021002
+ (np.prod(arr.shape[ax:ax+indx.ndim]),)
1003-
+ arr.shape[ax+indx.ndim:]))
1003+
+ arr.shape[ax+indx.ndim:])
10041004
indx = flat_indx
10051005
else:
10061006
# This could be changed, a 0-d boolean index can
@@ -1067,9 +1067,9 @@ def _get_multi_index(self, arr, indices):
10671067
# First of all, reshape arr to combine fancy axes into one:
10681068
orig_shape = arr.shape
10691069
orig_slice = orig_shape[ax:ax + len(indx[1:])]
1070-
arr = arr.reshape((arr.shape[:ax]
1070+
arr = arr.reshape(arr.shape[:ax]
10711071
+ (np.prod(orig_slice).astype(int),)
1072-
+ arr.shape[ax + len(indx[1:]):]))
1072+
+ arr.shape[ax + len(indx[1:]):])
10731073

10741074
# Check if broadcasting works
10751075
res = np.broadcast(*indx[1:])
@@ -1103,9 +1103,9 @@ def _get_multi_index(self, arr, indices):
11031103
raise ValueError
11041104
arr = arr.take(mi.ravel(), axis=ax)
11051105
try:
1106-
arr = arr.reshape((arr.shape[:ax]
1106+
arr = arr.reshape(arr.shape[:ax]
11071107
+ mi.shape
1108-
+ arr.shape[ax+1:]))
1108+
+ arr.shape[ax+1:])
11091109
except ValueError:
11101110
# too many dimensions, probably
11111111
raise IndexError

numpy/_core/tests/test_multiarray.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def test_readonly_flag_protocols(self, flag, flag_value, writeable):
234234
a = np.arange(10)
235235
setattr(a.flags, flag, flag_value)
236236

237-
class MyArr():
237+
class MyArr:
238238
__array_struct__ = a.__array_struct__
239239

240240
assert memoryview(a).readonly is not writeable
@@ -6480,11 +6480,11 @@ def test_std_where(self):
64806480
[True],
64816481
[False]])
64826482
_cases = [
6483-
(0, True, 7.07106781*np.ones((5))),
6484-
(1, True, 1.41421356*np.ones((5))),
6483+
(0, True, 7.07106781*np.ones(5)),
6484+
(1, True, 1.41421356*np.ones(5)),
64856485
(0, whf,
64866486
np.array([4.0824829 , 8.16496581, 5., 7.39509973, 8.49836586])),
6487-
(0, whp, 2.5*np.ones((5)))
6487+
(0, whp, 2.5*np.ones(5))
64886488
]
64896489
for _ax, _wh, _res in _cases:
64906490
assert_allclose(a.std(axis=_ax, where=_wh), _res)
@@ -6886,7 +6886,7 @@ def test_huge_vectordot(self, dtype):
68866886

68876887
def test_dtype_discovery_fails(self):
68886888
# See gh-14247, error checking was missing for failed dtype discovery
6889-
class BadObject(object):
6889+
class BadObject:
68906890
def __array__(self, dtype=None, copy=None):
68916891
raise TypeError("just this tiny mint leaf")
68926892

@@ -7247,7 +7247,7 @@ def test_matmul_empty(self):
72477247

72487248
def test_matmul_exception_multiply(self):
72497249
# test that matmul fails if `__mul__` is missing
7250-
class add_not_multiply():
7250+
class add_not_multiply:
72517251
def __add__(self, other):
72527252
return self
72537253
a = np.full((3,3), add_not_multiply())
@@ -7256,7 +7256,7 @@ def __add__(self, other):
72567256

72577257
def test_matmul_exception_add(self):
72587258
# test that matmul fails if `__add__` is missing
7259-
class multiply_not_add():
7259+
class multiply_not_add:
72607260
def __mul__(self, other):
72617261
return self
72627262
a = np.full((3,3), multiply_not_add())
@@ -8356,7 +8356,7 @@ def test_no_suboffsets(self):
83568356
np.frombuffer(buffer)
83578357

83588358

8359-
class TestArrayCreationCopyArgument(object):
8359+
class TestArrayCreationCopyArgument:
83608360

83618361
class RaiseOnBool:
83628362

@@ -8677,7 +8677,7 @@ def test_multiarray_flags_not_writable_attribute_deletion(self):
86778677
assert_raises(AttributeError, delattr, a, s)
86788678

86798679

8680-
class TestArrayInterface():
8680+
class TestArrayInterface:
86818681
class Foo:
86828682
def __init__(self, value):
86838683
self.value = value

numpy/_core/tests/test_overrides.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def __array_function__(self, func, types, args, kwargs):
540540

541541
class TestArrayLike:
542542
def setup_method(self):
543-
class MyArray():
543+
class MyArray:
544544
def __init__(self, function=None):
545545
self.function = function
546546

@@ -554,7 +554,7 @@ def __array_function__(self, func, types, args, kwargs):
554554

555555
self.MyArray = MyArray
556556

557-
class MyNoArrayFunctionArray():
557+
class MyNoArrayFunctionArray:
558558
def __init__(self, function=None):
559559
self.function = function
560560

numpy/_core/tests/test_records.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ def test_recarray_from_obj(self):
114114

115115
mine = np.rec.fromarrays([a, b, c], names='date,data1,data2')
116116
for i in range(len(a)):
117-
assert_((mine.date[i] == list(range(1, 10))))
118-
assert_((mine.data1[i] == 0.0))
119-
assert_((mine.data2[i] == 0.0))
117+
assert_(mine.date[i] == list(range(1, 10)))
118+
assert_(mine.data1[i] == 0.0)
119+
assert_(mine.data2[i] == 0.0)
120120

121121
def test_recarray_repr(self):
122122
a = np.array([(1, 0.1), (2, 0.2)],

numpy/_core/tests/test_regression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ def test_astype_copy(self):
10711071
with open(filename, 'rb') as f:
10721072
xp = pickle.load(f, encoding='latin1')
10731073
xpd = xp.astype(np.float64)
1074-
assert_((xp.__array_interface__['data'][0] !=
1075-
xpd.__array_interface__['data'][0]))
1074+
assert_(xp.__array_interface__['data'][0] !=
1075+
xpd.__array_interface__['data'][0])
10761076

10771077
def test_compress_small_type(self):
10781078
# Ticket #789, changeset 5217.

numpy/_core/tests/test_shape_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_2D_array(self):
154154

155155
def test_generator(self):
156156
with pytest.raises(TypeError, match="arrays to stack must be"):
157-
hstack((np.arange(3) for _ in range(2)))
157+
hstack(np.arange(3) for _ in range(2))
158158
with pytest.raises(TypeError, match="arrays to stack must be"):
159159
hstack(map(lambda x: x, np.ones((3, 2))))
160160

@@ -209,7 +209,7 @@ def test_2D_array2(self):
209209

210210
def test_generator(self):
211211
with pytest.raises(TypeError, match="arrays to stack must be"):
212-
vstack((np.arange(3) for _ in range(2)))
212+
vstack(np.arange(3) for _ in range(2))
213213

214214
def test_casting_and_dtype(self):
215215
a = np.array([1, 2, 3])
@@ -477,7 +477,7 @@ def test_stack():
477477

478478
# do not accept generators
479479
with pytest.raises(TypeError, match="arrays to stack must be"):
480-
stack((x for x in range(3)))
480+
stack(x for x in range(3))
481481

482482
#casting and dtype test
483483
a = np.array([1, 2, 3])

numpy/_core/tests/test_stringdtype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ def test_self_casts(dtype, dtype2, strings):
232232
if hasattr(dtype, "na_object") and hasattr(dtype2, "na_object"):
233233
na1 = dtype.na_object
234234
na2 = dtype2.na_object
235-
if ((na1 is not na2 and
235+
if (na1 is not na2 and
236236
# check for pd_NA first because bool(pd_NA) is an error
237237
((na1 is pd_NA or na2 is pd_NA) or
238238
# the second check is a NaN check, spelled this way
239239
# to avoid errors from math.isnan and np.isnan
240-
(na1 != na2 and not (na1 != na1 and na2 != na2))))):
240+
(na1 != na2 and not (na1 != na1 and na2 != na2)))):
241241
with pytest.raises(TypeError):
242242
arr[:-1] == newarr[:-1]
243243
return

numpy/f2py/auxfuncs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,9 @@ def getcallprotoargument(rout, cb_map={}):
701701
else:
702702
if not isattr_value(var):
703703
ctype = ctype + '*'
704-
if ((isstring(var)
704+
if (isstring(var)
705705
or isarrayofstrings(var) # obsolete?
706-
or isstringarray(var))):
706+
or isstringarray(var)):
707707
arg_types2.append('size_t')
708708
arg_types.append(ctype)
709709

0 commit comments

Comments
 (0)