8000 Merge pull request #29068 from eendebakpt/ruff_e501_tests · numpy/numpy@3f1b457 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f1b457

Browse files
authored
Merge pull request #29068 from eendebakpt/ruff_e501_tests
MAINT: Enforce ruff E501
2 parents e107f24 + f9baafb commit 3f1b457

15 files changed

+63
-32
lines changed

benchmarks/benchmarks/bench_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class CountNonzero(Benchmark):
151151
params = [
152152
[1, 2, 3],
153153
[100, 10000, 1000000],
154-
[bool, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64, str, object]
154+
[bool, np.int8, np.int16, np.int32, np.int64, np.float32,
155+
np.float64, str, object]
155156
]
156157

157158
def setup(self, numaxes, size, dtype):

benchmarks/benchmarks/bench_function_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class Sort(Benchmark):
241241
def setup(self, kind, dtype, array_type):
242242
rnd = np.random.RandomState(507582308)
243243
array_class = array_type[0]
244-
self.arr = getattr(SortGenerator, array_class)(self.ARRAY_SIZE, dtype, *array_type[1:], rnd)
244+
generate_array_method = getattr(SortGenerator, array_class)
245+
self.arr = generate_array_method(self.ARRAY_SIZE, dtype, *array_type[1:], rnd)
245246

246247
def time_sort(self, kind, dtype, array_type):
247248
# Using np.sort(...) instead of arr.sort(...) because it makes a copy.

benchmarks/benchmarks/bench_ufunc_strides.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ def train(self, max_epoch):
208208
for epoch in range(max_epoch):
209209
z = np.matmul(self.X_train, self.W)
210210
A = 1 / (1 + np.exp(-z)) # sigmoid(z)
211-
loss = -np.mean(self.Y_train * np.log(A) + (1 - self.Y_train) * np.log(1 - A))
212-
dz = A - self.Y_train
211+
Y_train = self.Y_train
212+
loss = -np.mean(Y_train * np.log(A) + (1 - Y_train) * np.log(1 - A))
213+
dz = A - Y_train
213214
dw = (1 / self.size) * np.matmul(self.X_train.T, dz)
214215
self.W = self.W - self.alpha * dw
215216

numpy/_core/tests/test_cython.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def test_npystring_allocators_other_dtype(install_temp):
345345
assert checks.npystr 8000 ing_allocators_other_types(arr1, arr2) == 0
346346

347347

348-
@pytest.mark.skipif(sysconfig.get_platform() == 'win-arm64', reason='no checks module on win-arm64')
348+
@pytest.mark.skipif(sysconfig.get_platform() == 'win-arm64',
349+
reason='no checks module on win-arm64')
349350
def test_npy_uintp_type_enum():
350351
import checks
351352
assert checks.check_npy_uintp_type_enum()

numpy/_core/tests/test_function_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
def _is_armhf():
3434
# Check if the current platform is ARMHF (32-bit ARM architecture)
35-
return platform.machine().startswith('arm') and platform.architecture()[0] == '32bit'
35+
architecture = platform.architecture()
36+
return platform.machine().startswith('arm') and architecture[0] == '32bit'
3637

3738
class PhysicalQuantity(float):
3839
def __new__(cls, value):

numpy/_core/tests/test_half.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def test_half_fpe(self):
531531
assert_raises_fpe('overflow', lambda a, b: a - b,
532532
float16(-65504), float16(17))
533533
assert_raises_fpe('overflow', np.nextafter, float16(65504), float16(np.inf))
534-
assert_raises_fpe('overflow', np.nextafter, float16(-65504), float16(-np.inf))
534+
assert_raises_fpe('overflow', np.nextafter, float16(-65504), float16(-np.inf)) # noqa: E501
535535
assert_raises_fpe('overflow', np.spacing, float16(65504))
536536

537537
# Invalid value errors

numpy/_core/tests/test_indexing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,8 @@ def test_boolean_indexing_fast_path(self):
13521352
"size of axis is 3 but size of corresponding boolean axis is 1",
13531353
lambda: a[idx1])
13541354

1355-
# This used to incorrectly give a ValueError: operands could not be broadcast together
1355+
# This used to incorrectly give a ValueError: operands could not be
1356+
# broadcast together
13561357
idx2 = np.array([[False] * 8 + [True]])
13571358
assert_raises_regex(IndexError,
13581359
"boolean index did not match indexed array along axis 0; "

numpy/_core/tests/test_mem_overlap.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ def check_may_share_memory_exact(a, b):
165165

166166
err_msg = ""
167167
if got != exact:
168+
base_delta = a.__array_interface__['data'][0] - b.__array_interface__['data'][0]
168169
err_msg = " " + "\n ".join([
169-
f"base_a - base_b = {a.__array_interface__['data'][0] - b.__array_interface__['data'][0]!r}",
170+
f"base_a - base_b = {base_delta!r}",
170171
f"shape_a = {a.shape!r}",
171172
f"shape_b = {b.shape!r}",
172173
f"strides_a = {a.strides!r}",
@@ -402,7 +403,9 @@ def check(A, U, exists=None):
402403
exists = (X is not None)
403404

404405
if X is not None:
405-
assert_(sum(a * x for a, x in zip(A, X)) == sum(a * u // 2 for a, u in zip(A, U)))
406+
sum_ax = sum(a * x for a, x in zip(A, X))
407+
sum_au_half = sum(a * u // 2 for a, u in zip(A, U))
408+
assert_(sum_ax == sum_au_half)
406409
assert_(all(0 <= x <= u for x, u in zip(X, U)))
407410
assert_(any(x != u // 2 for x, u in zip(X, U)))
408411

numpy/_core/tests/test_scalarbuffer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_str_ucs4(self, s):
128128
s = np.str_(s) # only our subclass implements the buffer protocol
129129

130130
# all the same, characters always encode as ucs4
131-
expected = {'strides': (), 'itemsize': 8, 'ndim': 0, 'shape': (), 'format': '2w',
132-
'readonly': True}
131+
expected = {'strides': (), 'itemsize': 8, 'ndim': 0, 'shape': (),
132+
'format': '2w', 'readonly': True}
133133

134134
v = memoryview(s)
135135
assert self._as_dict(v) == expected

numpy/_core/tests/test_unicode.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def test_valuesSD(self):
134134

135135
def test_valuesMD(self):
136136
# Check creation of multi-dimensional objects with values
137-
ua = np.array([[[self.ucs_value * self.ulen] * 2] * 3] * 4, dtype=f'U{self.ulen}')
137+
data = [[[self.ucs_value * self.ulen] * 2] * 3] * 4
138+
ua = np.array(data, dtype=f'U{self.ulen}')
138139
self.content_check(ua, ua[0, 0, 0], 4 * self.ulen * 2 * 3 * 4)
139140
self.content_check(ua, ua[-1, -1, -1], 4 * self.ulen * 2 * 3 * 4)
140141

numpy/lib/tests/test_format.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,6 @@
384384
('z', 'u1')]
385385

386386
NbufferT = [
387-
# x Info color info y z
388-
# value y2 Info2 name z2 Name Value
389-
# name value y3 z3
390387
([3, 2], (6j, 6., ('nn', [6j, 4j], [6., 4.], [1, 2]), 'NN', True),
391388
'cc', ('NN', 6j), [[6., 4.], [6., 4.]], 8),
392389
([4, 3], (7j, 7., ('oo', [7j, 5j], [7., 5.], [2, 1]), 'OO', False),

numpy/lib/tests/test_histograms.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,18 +554,20 @@ def test_outlier(self):
554554
assert_equal(len(a), numbins)
555555

556556
def test_scott_vs_stone(self):
557-
"""Verify that Scott's rule and Stone's rule converges for normally distributed data"""
557+
# Verify that Scott's rule and Stone's rule converges for normally
558+
# distributed data
558559

559560
def nbins_ratio(seed, size):
560561
rng = np.random.RandomState(seed)
561562
x = rng.normal(loc=0, scale=2, size=size)
562563
a, b = len(np.histogram(x, 'stone')[0]), len(np.histogram(x, 'scott')[0])
563564
return a / (a + b)
564565

565-
ll = [[nbins_ratio(seed, size) for size in np.geomspace(start=10, stop=100, num=4).round().astype(int)]
566-
for seed in range(10)]
566+
geom_space = np.geomspace(start=10, stop=100, num=4).round().astype(int)
567+
ll = [[nbins_ratio(seed, size) for size in geom_space] for seed in range(10)]
567568

568-
# the average difference between the two methods decreases as the dataset size increases.
569+
# the average difference between the two methods decreases as the dataset
570+
# size increases.
569571
avg = abs(np.mean(ll, axis=0) - 0.5)
570572
assert_almost_equal(avg, [0.15, 0.09, 0.08, 0.03], decimal=2)
571573

numpy/lib/tests/test_recfunctions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,8 @@ def test_flatten_wflexible(self):
524524
assert_equal(test, control)
525525

526526
test = merge_arrays((x, w), flatten=False)
527-
controldtype = [('f0', int),
528-
('f1', [('a', int),
529-
('b', [('ba', float), ('bb', int), ('bc', [])])])]
527+
f1_descr = [('a', int), ('b', [('ba', float), ('bb', int), ('bc', [])])]
528+
controldtype = [('f0', int), ('f1', f1_descr)]
530529
control = np.array([(1., (1, (2, 3.0, ()))), (2, (4, (5, 6.0, ())))],
531530
dtype=controldtype)
532531
assert_equal(test, control)

numpy/tests/test_public_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def test___qualname___and___module___attribute():
780780
inspect.ismodule(member) and # it's a module
781781
"numpy" in member.__name__ and # inside NumPy
782782
not member_name.startswith("_") and # not private
783-
member_name not in {"tests", "typing"} and # 2024-12: type names don't match
783+
member_name not in {"tests", "typing"} and # type names don't match
784784
"numpy._core" not in member.__name__ and # outside _core
785785
member not in visited_modules # not visited yet
786786
):

ruff.toml

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,41 @@ ignore = [
6868
"_tempita.py" = ["B909"]
6969
"bench_*.py" = ["B015", "B018"]
7070
"test*.py" = ["B015", "B018", "E201", "E714"]
71-
"benchmarks/*py" = ["E501"]
72-
"numpy/_core/tests/**" = ["E501"]
71+
72+
"benchmarks/benchmarks/bench_linalg.py" = ["E501"]
73+
"numpy/_core/tests/test_api.py" = ["E501"]
74+
"numpy/_core/tests/test_arrayprint.py" = ["E501"]
75+
"numpy/_core/tests/test_cpu_dispatcher.py" = ["E501"]
76+
"numpy/_core/tests/test_cpu_features.py" = ["E501"]
77+
"numpy/_core/tests/test_datetime.py" = ["E501"]
78+
"numpy/_core/tests/test_dtype.py" = ["E501"]
79+
"numpy/_core/tests/test_defchararray.py" = ["E501"]
80+
"numpy/_core/tests/test_einsum.py" = ["E501"]
81+
"numpy/_core/tests/test_multiarray.py" = ["E501"]
82+
"numpy/_core/tests/test_multithreading.py" = ["E501"]
83+
"numpy/_core/tests/test_nditer*py" = ["E501"]
84+
"numpy/_core/tests/test_ufunc*py" = ["E501"]
85+
"numpy/_core/tests/test_umath*py" = ["E501"]
86+
"numpy/_core/tests/test_numeric*.py" = ["E501"]
87+
"numpy/_core/tests/test_regression.py" = ["E501"]
88+
"numpy/_core/tests/test_shape_base.py" = ["E501"]
89+
"numpy/_core/tests/test_simd*.py" = ["E501"]
90+
"numpy/_core/tests/test_strings.py" = ["E501"]
7391
"numpy/_core/_add_newdocs.py" = ["E501"]
7492
"numpy/_core/_add_newdocs_scalars.py" = ["E501"]
7593
"numpy/_core/code_generators/generate_umath.py" = ["E501"]
76-
"numpy/_typing/*py" = ["E501"]
77-
"numpy/lib/tests/*py" = ["E501"]
78-
"numpy/linalg/tests/*py" = ["E501"]
79-
"numpy/ma/tests/*py" = ["E501"]
80-
"numpy/tests/*py" = ["E501"]
81-
"numpy*pyi" = ["E501"]
94+
"numpy/lib/tests/test_function_base.py" = ["E501"]
95+
"numpy/lib/tests/test_format.py" = ["E501"]
96+
"numpy/lib/tests/test_io.py" = ["E501"]
97+
"numpy/lib/tests/test_polynomial.py" = ["E501"]
98+
"numpy/linalg/tests/test_linalg.py" = ["E501"]
99+
"numpy/tests/test_configtool.py" = ["E501"]
82100
"numpy/f2py/*py" = ["E501"]
101+
# for typing related files we follow https://typing.python.org/en/latest/guides/writing_stubs.html#maximum-line-length
102+
"numpy/_typing/_array_like.py" = ["E501"]
103+
"numpy/_typing/_dtype_like.py" = ["E501"]
104+
"numpy*pyi" = ["E501"]
105+
83106
"__init__.py" = ["F401", "F403", "F405"]
84107
"__init__.pyi" = ["F401"]
85108
"numpy/_core/defchararray.py" = ["F403", "F405"]

0 commit comments

Comments
 (0)
0