Skip to content

Commit 035581a

Browse files
authored
Use ruff instead of black/isort/flake8/pylint (#749)
1 parent b71c925 commit 035581a

8 files changed

Lines changed: 38 additions & 50 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
repos:
2-
- repo: https://github.com/timothycrosley/isort
3-
rev: 8.0.1
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.15.12
44
hooks:
5-
- id: isort
6-
additional_dependencies: [toml]
7-
- repo: https://github.com/python/black
8-
rev: 26.1.0
9-
hooks:
10-
- id: black
11-
- repo: https://github.com/pycqa/flake8
12-
rev: 7.3.0
13-
hooks:
14-
- id: flake8
15-
- repo: https://github.com/pycqa/pylint
16-
rev: v4.0.5
17-
hooks:
18-
- id: pylint
5+
- id: ruff-check
6+
types_or: [ python, pyi, jupyter, pyproject ]
7+
args: [ --fix ]
8+
- id: ruff-format
199
- repo: https://github.com/codespell-project/codespell
2010
rev: v2.2.4
2111
hooks:

examples/tutorial_lastfm.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@
8585
"source": [
8686
"from implicit.nearest_neighbours import bm25_weight\n",
8787
"\n",
88-
"# weight the matrix, both to reduce impact of users that have played the same artist thousands of times\n",
89-
"# and to reduce the weight given to popular items\n",
88+
"# weight the matrix, both to reduce impact of users that have played the same artist thousands\n",
89+
"# of times and to reduce the weight given to popular items\n",
9090
"artist_user_plays = bm25_weight(artist_user_plays, K1=100, B=0.8)\n",
9191
"\n",
92-
"# get the transpose since the most of the functions in implicit expect (user, item) sparse matrices instead of (item, user)\n",
92+
"# get the transpose since the most of the functions in implicit expect (user, item) sparse\n",
93+
"# matrices instead of (item, user)\n",
9394
"user_plays = artist_user_plays.T.tocsr()"
9495
]
9596
},
@@ -293,7 +294,14 @@
293294
"# Use pandas to display the output in a table, pandas isn't a dependency of implicit otherwise\n",
294295
"import numpy as np\n",
295296
"import pandas as pd\n",
296-
"pd.DataFrame({\"artist\": artists[ids], \"score\": scores, \"already_liked\": np.isin(ids, user_plays[userid].indices)})"
297+
"\n",
298+
"pd.DataFrame(\n",
299+
" {\n",
300+
" \"artist\": artists[ids],\n",
301+
" \"score\": scores,\n",
302+
" \"already_liked\": np.isin(ids, user_plays[userid].indices),\n",
303+
" }\n",
304+
")"
297305
]
298306
},
299307
{
@@ -423,7 +431,7 @@
423431
],
424432
"source": [
425433
"# get related items for the beatles (itemid = 25512)\n",
426-
"ids, scores= model.similar_items(252512)\n",
434+
"ids, scores = model.similar_items(252512)\n",
427435
"\n",
428436
"# display the results using pandas for nicer formatting\n",
429437
"pd.DataFrame({\"artist\": artists[ids], \"score\": scores})"

implicit/ann/annoy.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ def similar_items(
124124

125125
# support recalculate_item if possible. TODO: refactor this
126126
if hasattr(self.model, "_item_factor"):
127-
factor = self.model._item_factor(
128-
itemid, item_users, recalculate_item
129-
) # pylint: disable=protected-access
127+
factor = self.model._item_factor(itemid, item_users, recalculate_item) # pylint: disable=protected-access
130128
elif recalculate_item:
131129
raise NotImplementedError(f"recalculate_item isn't supported with {self.model}")
132130
else:
@@ -191,9 +189,7 @@ def recommend(
191189

192190
# support recalculate_user if possible (TODO: come back to this since its a bit of a hack)
193191
if hasattr(self.model, "+_user_factor"):
194-
user = self.model._user_factor(
195-
userid, user_items, recalculate_user
196-
) # pylint: disable=protected-access
192+
user = self.model._user_factor(userid, user_items, recalculate_user) # pylint: disable=protected-access
197193
elif recalculate_user:
198194
raise NotImplementedError(f"recalculate_user isn't supported with {self.model}")
199195
else:

implicit/ann/faiss.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ def similar_items(
151151

152152
# support recalculate_item if possible. TODO: refactor this
153153
if hasattr(self.model, "_item_factor"):
154-
factors = self.model._item_factor(
155-
itemid, item_users, recalculate_item
156-
) # pylint: disable=protected-access
154+
factors = self.model._item_factor(itemid, item_users, recalculate_item) # pylint: disable=protected-access
157155
elif recalculate_item:
158156
raise NotImplementedError(f"recalculate_item isn't supported with {self.model}")
159157
else:
@@ -223,9 +221,7 @@ def recommend(
223221

224222
# support recalculate_user if possible (TODO: come back to this since its a bit of a hack)
225223
if hasattr(self.model, "+_user_factor"):
226-
user = self.model._user_factor(
227-
userid, user_items, recalculate_user
228-
) # pylint: disable=protected-access
224+
user = self.model._user_factor(userid, user_items, recalculate_user) # pylint: disable=protected-access
229225
elif recalculate_user:
230226
raise NotImplementedError(f"recalculate_user isn't supported with {self.model}")
231227
else:

implicit/ann/nmslib.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ def similar_items(
128128

129129
# support recalculate_item if possible. TODO: refactor this
130130
if hasattr(self.model, "_item_factor"):
131-
factors = self.model._item_factor(
132-
itemid, item_users, recalculate_item
133-
) # pylint: disable=protected-access
131+
factors = self.model._item_factor(itemid, item_users, recalculate_item) # pylint: disable=protected-access
134132
elif recalculate_item:
135133
raise NotImplementedError(f"recalculate_item isn't supported with {self.model}")
136134
else:
@@ -199,9 +197,7 @@ def recommend(
199197

200198
# support recalculate_user if possible (TODO: come back to this since its a bit of a hack)
201199
if hasattr(self.model, "+_user_factor"):
202-
user = self.model._user_factor(
203-
userid, user_items, recalculate_user
204-
) # pylint: disable=protected-access
200+
user = self.model._user_factor(userid, user_items, recalculate_user) # pylint: disable=protected-access
205201
elif recalculate_user:
206202
raise NotImplementedError(f"recalculate_user isn't supported with {self.model}")
207203
else:

implicit/approximate_als.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def NMSLibAlternatingLeastSquares(
1515
index_params=None,
1616
query_params=None,
1717
use_gpu=implicit.gpu.HAS_CUDA,
18-
**kwargs
18+
**kwargs,
1919
):
2020
# delay importing here in case nmslib isn't installed
2121
from implicit.ann.nmslib import NMSLibModel
@@ -39,7 +39,7 @@ def AnnoyAlternatingLeastSquares(
3939
n_trees=50,
4040
search_k=-1,
4141
use_gpu=implicit.gpu.HAS_CUDA,
42-
**kwargs
42+
**kwargs,
4343
):
4444
als_model = implicit.als.AlternatingLeastSquares(*args, use_gpu=use_gpu, **kwargs)
4545
from implicit.ann.annoy import AnnoyModel
@@ -60,7 +60,7 @@ def FaissAlternatingLeastSquares(
6060
nlist=400,
6161
nprobe=20,
6262
use_gpu=implicit.gpu.HAS_CUDA,
63-
**kwargs
63+
**kwargs,
6464
):
6565
# note that we're using the factory function here to instantiate a CPU/GPU model as appropriate
6666
als_model = implicit.als.AlternatingLeastSquares(*args, use_gpu=use_gpu, **kwargs)

pyproject.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
[tool.black]
2-
3-
line-length = 100
4-
51
[build-system]
62
requires = [
73
"setuptools>=42",
@@ -39,3 +35,12 @@ archs = ["x86_64", "universal2", "arm64"]
3935

4036
[tool.pytest.ini_options]
4137
filterwarnings = ['ignore::implicit.utils.ParameterWarning']
38+
39+
[tool.ruff]
40+
line-length = 100
41+
42+
[tool.ruff.lint]
43+
select = ["E", "F", "W", "I"]
44+
45+
[tool.ruff.lint.isort]
46+
known-first-party = ["implicit"]

requirements-dev.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33
scikit-build>=0.13.1
44
pytest
5-
black==22.3.0
6-
flake8
7-
isort
5+
ruff
86
codespell
9-
pylint

0 commit comments

Comments
 (0)