Skip to content

Commit

Permalink
CI: pycodestyle → ruff (numpy#27300)
Browse files Browse the repository at this point in the history
* CI: pycodestyle → ruff check

* MAINT: Apply ruff/pycodestyle rule E101

E101 Indentation contains mixed spaces and tabs

* MAINT: Apply ruff/pycodestyle rule E111

E111 Indentation is not a multiple of 4

* MAINT: Apply ruff/pycodestyle rule E116

E116 Unexpected indentation (comment)

* MAINT: Apply ruff/pycodestyle rule E117

E117 Over-indented

* MAINT: Apply ruff/pycodestyle rule E201

E201 Whitespace after bracket

* MAINT: Apply ruff/pycodestyle rule E202

E202 Whitespace before bracket

* MAINT: Apply ruff/pycodestyle rule E203

E203 Whitespace before punctuation

* MAINT: Apply ruff/pycodestyle rule E211

E211 Whitespace before '('

* MAINT: Apply ruff/pycodestyle rule E221

E221 Multiple spaces before operator

* MAINT: Apply ruff/pycodestyle rule E222

E222 Multiple spaces after operator

* MAINT: Apply ruff/pycodestyle rule E225

E225 Missing whitespace around operator

* MAINT: Apply ruff/pycodestyle rule E226

E226 Missing whitespace around arithmetic operator

* MAINT: Apply ruff/pycodestyle rule E227

E227 Missing whitespace around bitwise or shift operator

* MAINT: Apply ruff/pycodestyle rule E228

E228 Missing whitespace around modulo operator

* MAINT: Apply ruff/pycodestyle rule E252

E252 Missing whitespace around parameter equals

* MAINT: Apply ruff/pycodestyle rule E261

E261 Insert at least two spaces before an inline comment

* MAINT: Apply ruff/pycodestyle rule E271

E271 Multiple spaces after keyword

* MAINT: Apply ruff/pycodestyle rule E272

E272 Multiple spaces before keyword

* MAINT: Apply ruff/pycodestyle rule E275

E275 Missing whitespace after keyword

* MAINT: Apply ruff/pycodestyle rule E301

E301 Expected 1 blank line between methods

* MAINT: Apply ruff/pycodestyle rule E303

E303 Too many blank lines

* MAINT: Apply ruff/pycodestyle rule E304

E304 Blank lines found after function decorator

* MAINT: Apply ruff/pycodestyle rule E305

E305 Expected 2 blank lines after class or function definition

* MAINT: Apply ruff/pycodestyle rule E306

E306 Expected 1 blank line before a nested definition

* MAINT: Apply ruff/pycodestyle rule E501

E501 Line too long

* MAINT: Disable ruff/pycodestyle rule E714 in tests

E714 Test for object identity should be `is not`

* MAINT: Apply ruff/pycodestyle rule E743

E743 Ambiguous function name

* MAINT: Disable E501 for now

Ruff has found 1080 occurrences of this error. Fixing them sounds excessive.
  • Loading branch information
DimitriPapadopoulos authored Dec 16, 2024
1 parent d9c8ed5 commit c31e599
Show file tree
Hide file tree
Showing 276 changed files with 4,044 additions and 3,892 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ jobs:
- name: Install linter requirements
run:
python -m pip install -r requirements/linter_requirements.txt
- name: Run linter on PR diff
- name: Run linter on PR
env:
BASE_REF: ${{ github.base_ref }}
run:
python tools/linter.py --branch origin/"$BASE_REF"
python tools/linter.py

smoke_test:
# To enable this job on a fork, comment out:
Expand Down
2 changes: 1 addition & 1 deletion .spin/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _set_mem_rlimit(max_mem=None):
def _commit_to_sha(commit):
p = spin.util.run(['git', 'rev-parse', commit], output=False, echo=False)
if p.returncode != 0:
raise(
raise (
click.ClickException(
f'Could not find SHA matching commit `{commit}`'
)
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ stages:
# pip 21.1 emits a pile of garbage messages to annoy users :)
# failOnStderr: true
- script: |
python tools/linter.py --branch origin/$(System.PullRequest.TargetBranch)
python tools/linter.py
displayName: 'Run Lint Checks'
failOnStderr: true
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/benchmarks/bench_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CorrConv(Benchmark):

def setup(self, size1, size2, mode):
self.x1 = np.linspace(0, 1, num=size1)
self.x2 = np.cos(np.linspace(0, 2*np.pi, num=size2))
self.x2 = np.cos(np.linspace(0, 2 * np.pi, num=size2))

def time_correlate(self, size1, size2, mode):
np.correlate(self.x1, self.x2, mode=mode)
Expand Down Expand Up @@ -276,10 +276,10 @@ def time_sum(self, dtype, size):

class NumPyChar(Benchmark):
def setup(self):
self.A = np.array([100*'x', 100*'y'])
self.A = np.array([100 * 'x', 100 * 'y'])
self.B = np.array(1000 * ['aa'])

self.C = np.array([100*'x' + 'z', 100*'y' + 'z' + 'y', 100*'x'])
self.C = np.array([100 * 'x' + 'z', 100 * 'y' + 'z' + 'y', 100 * 'x'])
self.D = np.array(1000 * ['ab'] + 1000 * ['ac'])

def time_isalpha_small_list_big_string(self):
Expand Down
9 changes: 5 additions & 4 deletions benchmarks/benchmarks/bench_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def time_fine_binning(self):

class Histogram2D(Benchmark):
def setup(self):
self.d = np.linspace(0, 100, 200000).reshape((-1,2))
self.d = np.linspace(0, 100, 200000).reshape((-1, 2))

def time_full_coverage(self):
np.histogramdd(self.d, (200, 200), ((0, 100), (0, 100)))
Expand Down Expand Up @@ -64,7 +64,7 @@ class Mean(Benchmark):
params = [[1, 10, 100_000]]

def setup(self, size):
self.array = np.arange(2*size).reshape(2, size)
self.array = np.arange(2 * size).reshape(2, size)

def time_mean(self, size):
np.mean(self.array)
Expand Down Expand Up @@ -136,6 +136,7 @@ def time_select_larger(self):

def memoize(f):
_memoized = {}

def wrapped(*args):
if args not in _memoized:
_memoized[args] = f(*args)
Expand Down Expand Up @@ -181,11 +182,11 @@ def reversed(size, dtype, rnd):
dtype = np.dtype(dtype)
try:
with np.errstate(over="raise"):
res = dtype.type(size-1)
res = dtype.type(size - 1)
except (OverflowError, FloatingPointError):
raise SkipNotImplemented("Cannot construct arange for this size.")

return np.arange(size-1, -1, -1, dtype=dtype)
return np.arange(size - 1, -1, -1, dtype=dtype)

@staticmethod
@memoize
Expand Down
9 changes: 5 additions & 4 deletions benchmarks/benchmarks/bench_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ def time_numpy_linalg_lstsq_a__b_float64(self):
class Einsum(Benchmark):
param_names = ['dtype']
params = [[np.float32, np.float64]]

def setup(self, dtype):
self.one_dim_small = np.arange(600, dtype=dtype)
self.one_dim = np.arange(3000, dtype=dtype)
self.one_dim_big = np.arange(480000, dtype=dtype)
self.two_dim_small = np.arange(1200, dtype=dtype).reshape(30, 40)
self.two_dim = np.arange(240000, dtype=dtype).reshape(400, 600)
self.three_dim_small = np.arange(10000, dtype=dtype).reshape(10,100,10)
self.three_dim_small = np.arange(10000, dtype=dtype).reshape(10, 100, 10)
self.three_dim = np.arange(24000, dtype=dtype).reshape(20, 30, 40)
# non_contiguous arrays
self.non_contiguous_dim1_small = np.arange(1, 80, 2, dtype=dtype)
Expand All @@ -143,7 +144,7 @@ def time_einsum_outer(self, dtype):

# multiply(a, b):trigger sum_of_products_contig_two
def time_einsum_multiply(self, dtype):
np.einsum("..., ...", self.two_dim_small, self.three_dim , optimize=True)
np.einsum("..., ...", self.two_dim_small, self.three_dim, optimize=True)

# sum and multiply:trigger sum_of_products_contig_stride0_outstride0_two
def time_einsum_sum_mul(self, dtype):
Expand Down Expand Up @@ -208,8 +209,8 @@ def setup(self, shape, npdtypes):
self.x2arg = np.random.uniform(-1, 1, np.dot(*shape)).reshape(shape)
self.x2arg = self.x2arg.astype(npdtypes)
if npdtypes.startswith('complex'):
self.xarg += self.xarg.T*1j
self.x2arg += self.x2arg.T*1j
self.xarg += self.xarg.T * 1j
self.x2arg += self.x2arg.T * 1j

def time_transpose(self, shape, npdtypes):
np.transpose(self.xarg)
Expand Down
29 changes: 15 additions & 14 deletions benchmarks/benchmarks/bench_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ class Indexing(Benchmark):
params = [[True, False],
[1, 2],
[10, 100, 1000]]

def setup(self, masked, ndim, size):
x = np.arange(size**ndim).reshape(ndim * (size,))

if masked:
self.m = np.ma.array(x, mask=x%2 == 0)
self.m = np.ma.array(x, mask=x % 2 == 0)
else:
self.m = np.ma.array(x)

self.idx_scalar = (size//2,) * ndim
self.idx_0d = (size//2,) * ndim + (Ellipsis,)
self.idx_1d = (size//2,) * (ndim - 1)
self.idx_scalar = (size // 2,) * ndim
self.idx_0d = (size // 2,) * ndim + (Ellipsis,)
self.idx_1d = (size // 2,) * (ndim - 1)

def time_scalar(self, masked, ndim, size):
self.m[self.idx_scalar]
Expand All @@ -65,8 +66,8 @@ def setup(self, a_masked, b_masked, size):
self.a_scalar = np.ma.masked if a_masked else 5
self.b_scalar = np.ma.masked if b_masked else 3

self.a_1d = np.ma.array(x, mask=x%2 == 0 if a_masked else np.ma.nomask)
self.b_1d = np.ma.array(x, mask=x%3 == 0 if b_masked else np.ma.nomask)
self.a_1d = np.ma.array(x, mask=x % 2 == 0 if a_masked else np.ma.nomask)
self.b_1d = np.ma.array(x, mask=x % 3 == 0 if b_masked else np.ma.nomask)

self.a_2d = self.a_1d.reshape(1, -1)
self.b_2d = self.a_1d.reshape(-1, 1)
Expand Down Expand Up @@ -130,7 +131,7 @@ class MAFunctions1v(Benchmark):
def setup(self, mtype, func, msize):
xs = 2.0 + np.random.uniform(-1, 1, 6).reshape(2, 3)
m1 = [[True, False, False], [False, False, True]]
xl = 2.0 + np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = 2.0 + np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 2.8
self.nmxs = np.ma.array(xs, mask=m1)
self.nmxl = np.ma.array(xl, mask=maskx)
Expand All @@ -152,7 +153,7 @@ class MAMethod0v(Benchmark):
def setup(self, method, msize):
xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
m1 = [[True, False, False], [False, False, True]]
xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 0.8
self.nmxs = np.ma.array(xs, mask=m1)
self.nmxl = np.ma.array(xl, mask=maskx)
Expand Down Expand Up @@ -180,8 +181,8 @@ def setup(self, mtype, func, msize):
self.nmxs = np.ma.array(xs, mask=m1)
self.nmys = np.ma.array(ys, mask=m2)
# Big arrays
xl = 2.0 + np.random.uniform(-1, 1, 100*100).reshape(100, 100)
yl = 2.0 + np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = 2.0 + np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
yl = 2.0 + np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 2.8
masky = yl < 1.8
self.nmxl = np.ma.array(xl, mask=maskx)
Expand All @@ -203,7 +204,7 @@ class MAMethodGetItem(Benchmark):
def setup(self, margs, msize):
xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
m1 = [[True, False, False], [False, False, True]]
xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 0.8
self.nmxs = np.ma.array(xs, mask=m1)
self.nmxl = np.ma.array(xl, mask=maskx)
Expand All @@ -225,7 +226,7 @@ class MAMethodSetItem(Benchmark):
def setup(self, margs, mset, msize):
xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
m1 = [[True, False, False], [False, False, True]]
xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 0.8
self.nmxs = np.ma.array(xs, mask=m1)
self.nmxl = np.ma.array(xl, mask=maskx)
Expand All @@ -252,8 +253,8 @@ def setup(self, mtype, msize):
self.nmxs = np.ma.array(xs, mask=m1)
self.nmys = np.ma.array(ys, mask=m2)
# Big arrays
xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
yl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
xl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
yl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)
maskx = xl > 0.8
masky = yl < -0.8
self.nmxl = np.ma.array(xl, mask=maskx)
Expand Down
10 changes: 5 additions & 5 deletions benchmarks/benchmarks/bench_manipulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class BroadcastArrays(Benchmark):
timeout = 10

def setup(self, shape, ndtype):
self.xarg = np.random.ranf(shape[0]*shape[1]).reshape(shape)
self.xarg = np.random.ranf(shape[0] * shape[1]).reshape(shape)
self.xarg = self.xarg.astype(ndtype)
if ndtype.startswith('complex'):
self.xarg += np.random.ranf(1)*1j
self.xarg += np.random.ranf(1) * 1j

def time_broadcast_arrays(self, shape, ndtype):
np.broadcast_arrays(self.xarg, np.ones(1))
Expand All @@ -30,7 +30,7 @@ def setup(self, size, ndtype):
self.xarg = self.rng.random(size)
self.xarg = self.xarg.astype(ndtype)
if ndtype.startswith('complex'):
self.xarg += self.rng.random(1)*1j
self.xarg += self.rng.random(1) * 1j

def time_broadcast_to(self, size, ndtype):
np.broadcast_to(self.xarg, (size, size))
Expand All @@ -44,11 +44,11 @@ class ConcatenateStackArrays(Benchmark):
timeout = 10

def setup(self, shape, narrays, ndtype):
self.xarg = [np.random.ranf(shape[0]*shape[1]).reshape(shape)
self.xarg = [np.random.ranf(shape[0] * shape[1]).reshape(shape)
for x in range(narrays)]
self.xarg = [x.astype(ndtype) for x in self.xarg]
if ndtype.startswith('complex'):
[x + np.random.ranf(1)*1j for x in self.xarg]
[x + np.random.ranf(1) * 1j for x in self.xarg]

def time_concatenate_ax0(self, size, narrays, ndtype):
np.concatenate(self.xarg, axis=0)
Expand Down
31 changes: 16 additions & 15 deletions benchmarks/benchmarks/bench_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def time_permutation_2d(self):
def time_permutation_int(self):
np.random.permutation(self.n)


nom_size = 100000

class RNG(Benchmark):
Expand Down Expand Up @@ -155,21 +156,21 @@ def setup(self, bitgen, args):
self.rg.random()

def time_bounded(self, bitgen, args):
"""
Timer for 8-bit bounded values.
Parameters (packed as args)
----------
dt : {uint8, uint16, uint32, unit64}
output dtype
max : int
Upper bound for range. Lower is always 0. Must be <= 2**bits.
"""
dt, max = args
if bitgen == 'numpy':
self.rg.randint(0, max + 1, nom_size, dtype=dt)
else:
self.rg.integers(0, max + 1, nom_size, dtype=dt)
"""
Timer for 8-bit bounded values.
Parameters (packed as args)
----------
dt : {uint8, uint16, uint32, unit64}
output dtype
max : int
Upper bound for range. Lower is always 0. Must be <= 2**bits.
"""
dt, max = args
if bitgen == 'numpy':
self.rg.randint(0, max + 1, nom_size, dtype=dt)
else:
self.rg.integers(0, max + 1, nom_size, dtype=dt)

class Choice(Benchmark):
params = [1e3, 1e6, 1e8]
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/bench_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class StatsReductions(Benchmark):
def setup(self, dtype):
self.data = np.ones(200, dtype=dtype)
if dtype.startswith('complex'):
self.data = self.data * self.data.T*1j
self.data = self.data * self.data.T * 1j

def time_min(self, dtype):
np.min(self.data)
Expand Down
1 change: 1 addition & 0 deletions benchmarks/benchmarks/bench_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ScalarMath(Benchmark):
# the function call overhead to some degree.
params = [TYPES1]
param_names = ["type"]

def setup(self, typename):
self.num = np.dtype(typename).type(2)
self.int32 = np.int32(2)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/bench_shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Block2D(Benchmark):
def setup(self, shape, dtype, n_chunks):

self.block_list = [
[np.full(shape=[s//n_chunk for s, n_chunk in zip(shape, n_chunks)],
[np.full(shape=[s // n_chunk for s, n_chunk in zip(shape, n_chunks)],
fill_value=1, dtype=dtype) for _ in range(n_chunks[1])]
for _ in range(n_chunks[0])
]
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/benchmarks/bench_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class NDArrayGetItem(Benchmark):

def setup(self, margs, msize):
self.xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
self.xl = np.random.uniform(-1, 1, 50*50).reshape(50, 50)
self.xl = np.random.uniform(-1, 1, 50 * 50).reshape(50, 50)

def time_methods_getitem(self, margs, msize):
if msize == 'small':
Expand All @@ -268,7 +268,7 @@ class NDArraySetItem(Benchmark):

def setup(self, margs, msize):
self.xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
self.xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
self.xl = np.random.uniform(-1, 1, 100 * 100).reshape(100, 100)

def time_methods_setitem(self, margs, msize):
if msize == 'small':
Expand Down Expand Up @@ -512,9 +512,11 @@ def time_add_scalar_conv_complex(self):

class ArgPack:
__slots__ = ['args', 'kwargs']

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def __repr__(self):
return '({})'.format(', '.join(
[repr(a) for a in self.args] +
Expand Down
Loading

0 comments on commit c31e599

Please sign in to comment.