8000 MAINT: Enable linting with ruff E501 by eendebakpt · Pull Request #29250 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Enable linting with ruff E501 #29250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions numpy/_core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,12 @@ def check_contig(a, ccontig, fcontig):

def test_broadcast_arrays():
# Test user defined dtypes
a = np.array([(1, 2, 3)], dtype='u4,u4,u4')
b = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4')
dtype = 'u4,u4,u4'
a = np.array([(1, 2, 3)], dtype=dtype)
b = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=dtype)
result = np.broadcast_arrays(a, b)
assert_equal(result[0], np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)], dtype='u4,u4,u4'))
assert_equal(result[1], np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype='u4,u4,u4'))
assert_equal(result[0], np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)], dtype=dtype))
assert_equal(result[1], np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=dtype))

@pytest.mark.parametrize(["shape", "fill_value", "expected_output"],
[((2, 2), [5.0, 6.0], np.array([[5.0, 6.0], [5.0, 6.0]])),
Expand Down
13 changes: 10 additions & 3 deletions numpy/_core/tests/test_shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,17 @@ def test_exceptions(self):
assert_raises(ValueError, concatenate, ())

@pytest.mark.slow
@pytest.mark.skipif(sys.maxsize < 2**32, reason="only problematic on 64bit platforms")
@pytest.mark.skipif(
sys.maxsize < 2**32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32 bit platforms have sys.maxsize == 2**31 - 1, but I guess this also works :P

reason="only problematic on 64bit platforms"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also out of scope, but shouldn't this be "32bit platforms" 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the condition we want is to have np.iinfo(np.intc).max smaller than sys.maxsize. We can then create a list of size max_int + 1, but this cannot be concatenated by numpy.

On my system (64-bit windows) the np.intc is 32 bit, and sys.maxsize is 2**63-1. The test fails though:

In [222]: a = np.array([1])
     ...: max_int = np.iinfo(np.intc).max

In [223]: arrs = (a,) * (max_int + 1)
     ...: 

In [224]: np.concatenate(arrs)
     ...: 
---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
Cell In[224], line 1
----> 1 np.concatenate(arrs)

SystemError: <built-in function concatenate> returned NULL without setting an exception

Maybe because the amount of memory I have is not enough.

I'll happily leave this to another PR though!

Copy link
Member
@jorenham jorenham Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.maxsize corresponds to np.iinfo(np.intp).max and ctypes.sizeof(ctypes.c_ssize_t) I believe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll happily leave this to another PR though!

yea, probably for the best

)
@requires_memory(2 * np.iinfo(np.intc).max)
def test_huge_list_error(self):
a = np.array([1])
max_int = np.iinfo(np.intc).max
arrs = (a,) * (max_int + 1)
msg = fr"concatenate\(\) only supports up to {max_int} arrays but got {max_int + 1}."
msg = (fr"concatenate\(\) only supports up to {max_int} arrays"
f" but got {max_int + 1}.")
with pytest.raises(ValueError, match=msg):
np.concatenate(arrs)

Expand Down Expand Up @@ -379,7 +383,10 @@ def test_concatenate(self):
assert_(out is rout)
assert_equal(res, rout)

@pytest.mark.skipif(IS_PYPY, reason="PYPY handles sq_concat, nb_add differently than cpython")
@pytest.mark.skipif(
IS_PYPY,
reason="PYPY handles sq_concat, nb_add differently than cpython"
)
def test_operator_concat(self):
import operator
a = array([1, 2])
Expand Down
36 changes: 21 additions & 15 deletions numpy/lib/tests/test_function_base.py
EDBE
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
suppress_warnings,
)

np_floats = [np.half, np.single, np.double, np.longdouble]

def get_mat(n):
data = np.arange(n)
Expand Down Expand Up @@ -309,7 +310,7 @@ def test_basic(self):
def test_order(self):
# It turns out that people rely on np.copy() preserving order by
# default; changing this broke scikit-learn:
# github.com/scikit-learn/scikit-learn/commit/7842748cf777412c506a8c0ed28090711d3a3783
# github.com/scikit-learn/scikit-learn/commit/7842748
a = np.array([[1, 2], [3, 4]])
assert_(a.flags.c_contiguous)
assert_(not a.flags.f_contiguous)
Expand Down Expand Up @@ -2520,7 +2521,7 @@ def test_extreme(self):
assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
assert_(np.all(np.abs(c) <= 1.0))

@pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
@pytest.mark.parametrize("test_type", np_floats)
def test_corrcoef_dtype(self, test_type):
cast_A = self.A.astype(test_type)
res = corrcoef(cast_A, dtype=test_type)
Expand Down Expand Up @@ -2626,7 +2627,7 @@ def test_unit_fweights_and_aweights(self):
aweights=self.unit_weights),
self.res1)

@pytest.mark.parametrize("test_type", [np.half, np.single, np.double, np.longdouble])
@pytest.mark.parametrize("test_type", np_floats)
def test_cov_dtype(self, test_type):
cast_x1 = self.x1.astype(test_type)
res = cov(cast_x1, dtype=test_type)
Expand All @@ -2648,7 +2649,8 @@ def test_simple(self):

# need at least one test above 8, as the implementation is piecewise
A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549, 10.0])
expected = np.array([1.06307822, 1.12518299, 1.01214991, 1.00006049, 2815.71662847])
expected = np.array([1.06307822, 1.12518299, 1.01214991,
1.00006049, 2815.71662847])
assert_almost_equal(i0(A), expected)
assert_almost_equal(i0(-A), expected)

Expand Down Expand Up @@ -3141,23 +3143,27 @@ def test_non_finite_any_nan(self, sc):

def test_non_finite_inf(self, sc):
""" Test that interp between opposite infs gives nan """
assert_equal(np.interp(0.5, [-np.inf, +np.inf], sc([ 0, 10])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, 1], sc([-np.inf, +np.inf])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, 1], sc([+np.inf, -np.inf])), sc(np.nan))
inf = np.inf
nan = np.nan
assert_equal(np.interp(0.5, [-inf, +inf], sc([ 0, 10])), sc(nan))
assert_equal(np.interp(0.5, [ 0, 1], sc([-inf, +inf])), sc(nan))
assert_equal(np.interp(0.5, [ 0, 1], sc([+inf, -inf])), sc(nan))

# unless the y values are equal
assert_equal(np.interp(0.5, [-np.inf, +np.inf], sc([ 10, 10])), sc(10))

def test_non_finite_half_inf_xf(self, sc):
""" Test that interp where both axes have a bound at inf gives nan """
assert_equal(np.interp(0.5, [-np.inf, 1], sc([-np.inf, 10])), sc(np.nan))
assert_equal(np.interp(0.5, [-np.inf, 1], sc([+np.inf, 10])), sc(np.nan))
assert_equal(np.interp(0.5, [-np.inf, 1], sc([ 0, -np.inf])), sc(np.nan))
assert_equal(np.interp(0.5, [-np.inf, 1], sc([ 0, +np.inf])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, +np.inf], sc([-np.inf, 10])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, +np.inf], sc([+np.inf, 10])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, +np.inf], sc([ 0, -np.inf])), sc(np.nan))
assert_equal(np.interp(0.5, [ 0, +np.inf], sc([ 0, +np.inf])), sc(np.nan))
inf = np.inf
nan = np.nan
assert_equal(np.interp(0.5, [-inf, 1], sc([-inf, 10])), sc(nan))
assert_equal(np.interp(0.5, [-inf, 1], sc([+inf, 10])), sc(nan))
assert_equal(np.interp(0.5, [-inf, 1], sc([ 0, -inf])), sc(nan))
assert_equal(np.interp(0.5, [-inf, 1], sc([ 0, +inf])), sc(nan))
assert_equal(np.interp(0.5, [ 0, +inf], sc([-inf, 10])), sc(nan))
assert_equal(np.interp(0.5, [ 0, +inf], sc([+inf, 10])), sc(nan))
assert_equal(np.interp(0.5, [ 0, +inf], sc([ 0, -inf])), sc(nan))
assert_equal(np.interp(0.5, [ 0, +inf], sc([ 0, +inf])), sc(nan))

def test_non_finite_half_inf_x(self, sc):
""" Test interp where the x axis has a bound at inf """
Expand Down
9 changes: 6 additions & 3 deletions numpy/lib/tests/test_io.py
Original file line numb F438 er Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ def check_large_zip(memoryerror_raised):
raise MemoryError("Child process raised a MemoryError exception")
# -9 indicates a SIGKILL, probably an OOM.
if p.exitcode == -9:
pytest.xfail("subprocess got a SIGKILL, apparently free memory was not sufficient")
msg = "subprocess got a SIGKILL, apparently free memory was not sufficient"
pytest.xfail(msg)
assert p.exitcode == 0

class LoadTxtBase:
Expand Down Expand Up @@ -1673,7 +1674,8 @@ def test_dtype_with_converters_and_usecols(self):
conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
names=None, converters=conv, encoding="bytes")
control = np.rec.array([(1, 5, -1, 0), (2, 8, -1, 1), (3, 3, -2, 3)], dtype=dtyp)
control = np.rec.array([(1, 5, -1, 0), (2, 8, -1, 1), (3, 3, -2, 3)],
dtype=dtyp)
assert_equal(test, control)
dtyp = [('e1', 'i4'), ('e2', 'i4'), ('n', 'i1')]
test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
Expand Down Expand Up @@ -1886,7 +1888,8 @@ def test_user_missing_values(self):
#
basekwargs['dtype'] = mdtype
test = np.genfromtxt(TextIO(data),
missing_values={0: -9, 1: -99, 2: -999j}, usemask=True, **basekwargs)
missing_values={0: -9, 1: -99, 2: -999j},
usemask=True, **basekwargs)
control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
(-9, 2.2, -999j), (3, -99, 3j)],
mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
Expand Down
15 changes: 10 additions & 5 deletions numpy/lib/tests/test_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def test_poly1d_math(self):
assert_equal(p * q, np.poly1d([3., 8., 14., 8., 3.]))
assert_equal(p + q, np.poly1d([4., 4., 4.]))
assert_equal(p - q, np.poly1d([-2., 0., 2.]))
assert_equal(p ** 4, np.poly1d([1., 8., 36., 104., 214., 312., 324., 216., 81.]))
assert_equal(p ** 4, np.poly1d([1., 8., 36., 104., 214.,
312., 324., 216., 81.]))
assert_equal(p(q), np.poly1d([9., 12., 16., 8., 6.]))
assert_equal(q(p), np.poly1d([3., 12., 32., 40., 34.]))
assert_equal(p.deriv(), np.poly1d([2., 2.]))
Expand Down Expand Up @@ -131,12 +132,16 @@ def test_roots(self):
for i in np.logspace(10, 25, num=1000, base=10):
tgt = np.array([-1, 1, i])
res = np.sort(np.roots(poly.polyfromroots(tgt)[::-1]))
assert_almost_equal(res, tgt, 14 - int(np.log10(i))) # Adapting the expected precision according to the root value, to take into account numerical calculation error
# Adapting the expected precision according to the root value,
# to take into account numerical calculation error
assert_almost_equal(res, tgt, 14 - int(np.log10(i)))

for i in np.logspace(10, 25, num=1000, base=10):
tgt = np.array([-1, 1.01, i])
res = np.sort(np.roots(poly.polyfromroots(tgt)[::-1]))
assert_almost_equal(res, tgt, 14 - int(np.log10(i))) # Adapting the expected precision according to the root value, to take into account numerical calculation error
# Adapting the expected precision according to the root value,
# to take into account numerical calculation error
assert_almost_equal(res, tgt, 14 - int(np.log10(i)))

def test_str_leading_zeros(self):
p = np.poly1d([4, 3, 2, 1])
Expand Down Expand Up @@ -249,8 +254,8 @@ def test_complex(self):
def test_integ_coeffs(self):
p = np.poly1d([3, 2, 1])
p2 = p.integ(3, k=[9, 7, 6])
assert_(
(p2.coeffs == [1 / 4. / 5., 1 / 3. / 4., 1 / 2. / 3., 9 / 1. / 2., 7, 6]).all())
expected = [1 / 4 / 5, 1 / 3 / 4, 1 / 2 / 3, 9 / 1 / 2, 7, 6]
assert_((p2.coeffs == expected).all())

def test_zero_dims(self):
try:
Expand Down
5 changes: 0 additions & 5 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ ignore = [
"test*.py" = ["B015", "B018", "E201", "E714"]

"benchmarks/benchmarks/bench_linalg.py" = ["E501"]
"numpy/_core/tests/test_api.py" = ["E501"]
"numpy/_core/tests/test_arrayprint.py" = ["E501"]
"numpy/_core/tests/test_cpu_dispatcher.py" = ["E501"]
"numpy/_core/tests/test_cpu_features.py" = ["E501"]
Expand All @@ -88,16 +87,12 @@ ignore = [
"numpy/_core/tests/test_umath*py" = ["E501"]
"numpy/_core/tests/test_numeric*.py" = ["E501"]
"numpy/_core/tests/test_regression.py" = ["E501"]
"numpy/_core/tests/test_shape_base.py" = ["E501"]
"numpy/_core/tests/test_simd*.py" = ["E501"]
"numpy/_core/tests/test_strings.py" = ["E501"]
"numpy/_core/_add_newdocs.py" = ["E501"]
"numpy/_core/_add_newdocs_scalars.py" = ["E501"]
"numpy/_core/code_generators/generate_umath.py" = ["E501"]
"numpy/lib/tests/test_function_base.py" = ["E501"]
"numpy/lib/tests/test_format.py" = ["E501"]
"numpy/lib/tests/test_io.py" = ["E501"]
"numpy/lib/tests/test_polynomial.py" = ["E501"]
"numpy/linalg/tests/test_linalg.py" = ["E501"]
"numpy/tests/test_configtool.py" = ["E501"]
"numpy/f2py/*py" = ["E501"]
Expand Down
Loading
0