Skip to content

Commit c57510a

Browse files
authored
Add more lints to Ruff for better code quality. (#621)
* Add more lints to Ruff for better code quality. * Add more lints. * Avoid generator instantiation. * Replace all instances of `np.int_` with `np.int64`.
1 parent d6d1b19 commit c57510a

30 files changed

+404
-450
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
args: ["--autofix", "--no-ensure-ascii"]
1818

1919
- repo: https://github.com/astral-sh/ruff-pre-commit
20-
rev: v0.1.9
20+
rev: v0.1.11
2121
hooks:
2222
- id: ruff
2323
args: ["--fix"]

benchmarks/benchmark_coo.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
class MatrixMultiplySuite:
77
def setup(self):
8-
np.random.seed(0)
9-
self.x = sparse.random((100, 100), density=0.01)
10-
self.y = sparse.random((100, 100), density=0.01)
8+
rng = np.random.default_rng(0)
9+
self.x = sparse.random((100, 100), density=0.01, random_state=rng)
10+
self.y = sparse.random((100, 100), density=0.01, random_state=rng)
1111

1212
self.x @ self.y # Numba compilation
1313

@@ -17,9 +17,9 @@ def time_matmul(self):
1717

1818
class ElemwiseSuite:
1919
def setup(self):
20-
np.random.seed(0)
21-
self.x = sparse.random((100, 100, 100), density=0.01)
22-
self.y = sparse.random((100, 100, 100), density=0.01)
20+
rng = np.random.default_rng(0)
21+
self.x = sparse.random((100, 100, 100), density=0.01, random_state=rng)
22+
self.y = sparse.random((100, 100, 100), density=0.01, random_state=rng)
2323

2424
self.x + self.y # Numba compilation
2525

@@ -32,9 +32,9 @@ def time_mul(self):
3232

3333
class ElemwiseBroadcastingSuite:
3434
def setup(self):
35-
np.random.seed(0)
36-
self.x = sparse.random((100, 1, 100), density=0.01)
37-
self.y = sparse.random((100, 100), density=0.01)
35+
rng = np.random.default_rng(0)
36+
self.x = sparse.random((100, 1, 100), density=0.01, random_state=rng)
37+
self.y = sparse.random((100, 100), density=0.01, random_state=rng)
3838

3939
def time_add(self):
4040
self.x + self.y
@@ -45,9 +45,9 @@ def time_mul(self):
4545

4646
class IndexingSuite:
4747
def setup(self):
48-
np.random.seed(0)
49-
self.index = np.random.randint(0, 100, 50)
50-
self.x = sparse.random((100, 100, 100), density=0.01)
48+
rng = np.random.default_rng(0)
49+
self.index = rng.integers(0, 100, 50)
50+
self.x = sparse.random((100, 100, 100), density=0.01, random_state=rng)
5151

5252
# Numba compilation
5353
self.x[5]

benchmarks/benchmark_gcxs.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
class MatrixMultiplySuite:
77
def setup(self):
8-
np.random.seed(0)
9-
self.x = sparse.random((100, 100), density=0.01, format="gcxs")
10-
self.y = sparse.random((100, 100), density=0.01, format="gcxs")
8+
rng = np.random.default_rng(0)
9+
self.x = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)
10+
self.y = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)
1111

1212
self.x @ self.y # Numba compilation
1313

@@ -17,9 +17,9 @@ def time_matmul(self):
1717

1818
class ElemwiseSuite:
1919
def setup(self):
20-
np.random.seed(0)
21-
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs")
22-
self.y = sparse.random((100, 100, 100), density=0.01, format="gcxs")
20+
rng = np.random.default_rng(0)
21+
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)
22+
self.y = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)
2323

2424
self.x + self.y # Numba compilation
2525

@@ -32,9 +32,9 @@ def time_mul(self):
3232

3333
class ElemwiseBroadcastingSuite:
3434
def setup(self):
35-
np.random.seed(0)
36-
self.x = sparse.random((100, 1, 100), density=0.01, format="gcxs")
37-
self.y = sparse.random((100, 100), density=0.01, format="gcxs")
35+
rng = np.random.default_rng(0)
36+
self.x = sparse.random((100, 1, 100), density=0.01, format="gcxs", random_state=rng)
37+
self.y = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)
3838

3939
def time_add(self):
4040
self.x + self.y
@@ -45,9 +45,9 @@ def time_mul(self):
4545

4646
class IndexingSuite:
4747
def setup(self):
48-
np.random.seed(0)
49-
self.index = np.random.randint(0, 100, 50)
50-
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs")
48+
rng = np.random.default_rng(0)
49+
self.index = rng.integers(0, 100, 50)
50+
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)
5151

5252
# Numba compilation
5353
self.x[5]

benchmarks/benchmark_matmul.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class Matmul_Sparse:
77
params = (["coo", "gcxs"], [0, 1, None])
88

99
def setup(self, p, dens_arg):
10-
np.random.seed(0)
11-
self.x = sparse.random((100, 100), density=0.01, format=p)
12-
self.y = sparse.random((100, 100), density=0.01, format=p)
10+
rng = np.random.default_rng(0)
11+
self.x = sparse.random((100, 100), density=0.01, format=p, random_state=rng)
12+
self.y = sparse.random((100, 100), density=0.01, format=p, random_state=rng)
1313

1414
if dens_arg == 0:
1515
self.x = self.x.todense()

benchmarks/benchmark_tensordot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class TensordotSuiteDenseSparse:
1010
"""
1111

1212
def setup(self):
13-
np.random.seed(0)
14-
self.n = np.random.random((100, 100))
15-
self.s = sparse.random((100, 100, 100, 100), density=0.01)
13+
rng = np.random.default_rng(0)
14+
self.n = rng.random((100, 100))
15+
self.s = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)
1616

1717
def time_dense(self):
1818
sparse.tensordot(self.n, self.s, axes=([0, 1], [0, 2]))
@@ -28,9 +28,9 @@ class TensordotSuiteSparseSparse:
2828
"""
2929

3030
def setup(self):
31-
np.random.seed(0)
32-
self.s1 = sparse.random((100, 100), density=0.01)
33-
self.s2 = sparse.random((100, 100, 100, 100), density=0.01)
31+
rng = np.random.default_rng(0)
32+
self.s1 = sparse.random((100, 100), density=0.01, random_state=rng)
33+
self.s2 = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)
3434

3535
def time_dense(self):
3636
sparse.tensordot(self.s1, self.s2, axes=([0, 1], [0, 2]), return_type=np.ndarray)
@@ -46,9 +46,9 @@ class TensordotSuiteSparseDense:
4646
"""
4747

4848
def setup(self):
49-
np.random.seed(0)
50-
self.s = sparse.random((100, 100, 100, 100), density=0.01)
51-
self.n = np.random.random((100, 100))
49+
rng = np.random.default_rng(0)
50+
self.s = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)
51+
self.n = rng.random((100, 100))
5252

5353
def time_dense(self):
5454
sparse.tensordot(self.s, self.n, axes=([0, 1], [0, 1]))

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ include = ["sparse", "sparse.*"]
5555
version_file = "sparse/_version.py"
5656

5757
[tool.ruff]
58-
select = ["F", "E", "W", "I", "B", "UP"]
58+
select = ["F", "E", "W", "I", "B", "UP", "YTT", "BLE", "C4", "T10", "ISC", "ICN", "PIE", "PYI", "RSE", "RET", "SIM", "PGH", "FLY", "NPY", "PERF"]
5959
exclude = ["sparse/_version.py"]
6060
line-length = 120
6161

6262
[tool.ruff.lint.isort.sections]
6363
numpy = ["numpy", "numpy.*", "scipy", "scipy.*"]
6464

65+
[tool.ruff.format]
66+
quote-style = "double"
67+
docstring-code-format = true
68+
6569
[tool.ruff.lint.isort]
6670
section-order = [
6771
"future",

0 commit comments

Comments
 (0)