Skip to content

Commit 3d616fb

Browse files
authored
chore(lints): Add many rules for ruff (#1213)
1 parent 6f84637 commit 3d616fb

File tree

136 files changed

+644
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+644
-589
lines changed

Makefile

+9-8
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,12 @@ clean-test: FORCE ## remove test and coverage artifacts
186186
# Check lint, test, and format of code
187187
# -----------------
188188
check: check-lint check-types check-tests ## check code, style, types, and test (basic CI)
189-
check-fix: format check-lint check-types check-tests ## check and format code, style, types, and test
190-
check-lint: check-ruff ## check code formatting and style
189+
check-fix: format check-types check-tests ## check and format code, style, types, and test
190+
check-lint: check-ruff ## check code lints and format
191191

192192
check-ruff: $(RUFF) FORCE
193-
@echo "-------- Running ruff lint and formatting checks --------"
193+
@echo "-------- Running ruff lint and format checks --------"
194194
@# Check imports in addition to code
195-
@# Reason for two commands: https://github.com/astral-sh/ruff/issues/8232
196-
# . $(PYBIN)/activate && \
197-
# ruff check --select I --fix .
198195
# Check lints
199196
. $(PYBIN)/activate && \
200197
ruff check .
@@ -225,13 +222,17 @@ format: format-ruff ## format code
225222
format-ruff: $(RUFF) FORCE
226223
@echo "-------- Formatting code with ruff --------"
227224
@# Reason for two commands: https://github.com/astral-sh/ruff/issues/8232
228-
@# Fix imports
225+
@# Fix lints
229226
. $(PYBIN)/activate && \
230-
ruff check --select I --fix .
227+
ruff check --fix .
231228
@# Fix formatting
232229
. $(PYBIN)/activate && \
233230
ruff format .
234231

232+
format-ruff-unsafe: $(RUFF) FORCE
233+
@echo "-------- Formatting code with ruff (unsafe) --------"
234+
. $(PYBIN)/activate && \
235+
ruff check --fix --unsafe-fixes .
235236
# -----------------
236237
# Documentation
237238
# -----------------

examples/brownian/brownian_motion.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
# * `brownian_motion()` is a function that generates a random walk
66
# * `brownian_widget()` uses restructured plotting code given in article
77

8-
rs = np.random.RandomState()
8+
rng = np.random.default_rng()
99

1010

1111
# https://plotly.com/python/3d-line-plots/
1212
def brownian_motion(T=1, N=100, mu=0.1, sigma=0.01, S0=20):
1313
dt = float(T) / N
1414
t = np.linspace(0, T, N)
15-
W = rs.standard_normal(size=N)
15+
W = rng.standard_normal(size=N)
1616
W = np.cumsum(W) * np.sqrt(dt) # standard brownian motion
1717
X = (mu - 0.5 * sigma**2) * t + sigma * W
1818
S = S0 * np.exp(X) # geometric brownian motion
@@ -24,7 +24,7 @@ def brownian_data(n=100, mu=(0.0, 0.00), sigma=(0.1, 0.1), S0=(1.0, 1.0)):
2424
"x": brownian_motion(T=1, N=n, mu=mu[0], sigma=sigma[0], S0=S0[0]),
2525
"y": brownian_motion(T=1, N=n, mu=mu[1], sigma=sigma[1], S0=S0[1]),
2626
# "y": [i for i in range(n)],
27-
"z": [i for i in range(n)],
27+
"z": list(range(n)),
2828
}
2929

3030

@@ -35,12 +35,12 @@ def brownian_widget(width=600, height=600):
3535
x=[],
3636
y=[],
3737
z=[],
38-
marker=dict(
39-
size=4,
40-
color=[],
41-
colorscale="Viridis",
42-
),
43-
line=dict(color="darkblue", width=2),
38+
marker={
39+
"size": 4,
40+
"color": [],
41+
"colorscale": "Viridis",
42+
},
43+
line={"color": "darkblue", "width": 2},
4444
)
4545
],
4646
layout={"showlegend": False, "width": width, "height": height},

examples/brownian/mediapipe.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def rel_hand(start_pos: int, end_pos: int):
6060
normal_unit_vec = normal_vec / np.linalg.norm(normal_vec)
6161

6262
def list_to_xyz(x):
63-
x = list(map(lambda y: round(y, 2), x))
64-
return dict(x=x[0], y=x[1], z=x[2])
63+
x = [round(y, 2) for y in x]
64+
return {"x": x[0], "y": x[1], "z": x[2]}
6565

6666
# Invert, for some reason
6767
normal_unit_vec = normal_unit_vec * -1.0
@@ -82,8 +82,8 @@ def list_to_xyz(x):
8282

8383

8484
def xyz_mean(points):
85-
return dict(
86-
x=mean([p["x"] for p in points]),
87-
y=mean([p["y"] for p in points]),
88-
z=mean([p["z"] for p in points]),
89-
)
85+
return {
86+
"x": mean([p["x"] for p in points]),
87+
"y": mean([p["y"] for p in points]),
88+
"z": mean([p["z"] for p in points]),
89+
}

examples/cpuinfo/app.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from math import ceil
1010

11-
import matplotlib
11+
import matplotlib as mpl
1212
import matplotlib.pyplot as plt
1313
import numpy as np
1414
import pandas as pd
@@ -17,7 +17,7 @@
1717

1818
# The agg matplotlib backend seems to be a little more efficient than the default when
1919
# running on macOS, and also gives more consistent results across operating systems
20-
matplotlib.use("agg")
20+
mpl.use("agg")
2121

2222
# max number of samples to retain
2323
MAX_SAMPLES = 1000
@@ -166,7 +166,7 @@ def plot():
166166
ncols=ncols,
167167
squeeze=False,
168168
)
169-
for i in range(0, ncols * nrows):
169+
for i in range(ncols * nrows):
170170
row = i // ncols
171171
col = i % ncols
172172
axes = axeses[row, col]

examples/cpuinfo/fakepsutil.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ def cpu_count(logical: bool = True):
77
return 8 if logical else 4
88

99

10-
last_sample = np.random.uniform(0, 100, size=cpu_count(True))
10+
rng = np.random.default_rng()
11+
last_sample = rng.uniform(0, 100, size=cpu_count(True))
1112

1213

1314
def cpu_percent(percpu: bool = False):
1415
global last_sample
15-
delta = np.random.normal(scale=10, size=len(last_sample))
16+
delta = rng.normal(scale=10, size=len(last_sample))
1617
last_sample = (last_sample + delta).clip(0, 100)
1718
if percpu:
1819
return last_sample.tolist()

examples/dataframe/app.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import pandas as pd
1+
from typing import TYPE_CHECKING
2+
23
import seaborn as sns
34
from shinyswatch.theme import darkly
45

56
from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
67

8+
if TYPE_CHECKING:
9+
import pandas as pd
10+
711

812
def app_ui(req):
913
dark = True if "dark" in req.query_params else None

examples/express/accordion_app.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ def txt():
1717

1818
@render.plot
1919
def histogram():
20-
np.random.seed(19680801)
21-
x = 100 + 15 * np.random.randn(437)
20+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
2221
plt.hist(x, input.n(), density=True)

examples/express/column_wrap_app.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212

1313
@render.plot
1414
def histogram():
15-
np.random.seed(19680801)
16-
x = 100 + 15 * np.random.randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
1716
plt.hist(x, input.n(), density=True)
1817

1918
with ui.card():
2019

2120
@render.plot
2221
def histogram2():
23-
np.random.seed(19680801)
24-
x = 100 + 15 * np.random.randn(437)
22+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
2523
plt.hist(x, input.n(), density=True, color="red")

examples/express/nav_app.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
@render.plot
1515
def histogram():
16-
np.random.seed(19680801)
17-
x = 100 + 15 * np.random.randn(437)
16+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
1817
plt.hist(x, input.n(), density=True)
1918

2019
with ui.navset_card_underline():
@@ -25,6 +24,5 @@ def histogram():
2524

2625
@render.plot
2726
def histogram2():
28-
np.random.seed(19680801)
29-
x = 100 + 15 * np.random.randn(437)
27+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
3028
plt.hist(x, input.n2(), density=True)

examples/express/plot_app.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99

1010
@render.plot
1111
def histogram():
12-
np.random.seed(19680801)
13-
x = 100 + 15 * np.random.randn(437)
12+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
1413
plt.hist(x, input.n(), density=True)

examples/express/shared_app.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
@render.plot
1414
def histogram():
15-
np.random.seed(19680801)
16-
x = 100 + 15 * np.random.randn(437)
15+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
1716
plt.hist(x, shared.rv(), density=True)
1817

1918

examples/express/sidebar_app.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111
@render.plot
1212
def histogram():
13-
np.random.seed(19680801)
14-
x = 100 + 15 * np.random.randn(437)
13+
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
1514
plt.hist(x, input.n(), density=True)

examples/model-score/app.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def df():
5858
params=[150],
5959
)
6060
# Convert timestamp to datetime object, which SQLite doesn't support natively
61-
tbl["timestamp"] = pd.to_datetime(tbl["timestamp"], utc=True)
61+
tbl["timestamp"] = pd.to_datetime(tbl["timestamp"], utc=True, format="ISO8601")
6262
# Create a short label for readability
6363
tbl["time"] = tbl["timestamp"].dt.strftime("%H:%M:%S")
6464
# Reverse order of rows
@@ -81,10 +81,7 @@ def read_time_period(from_time, to_time):
8181

8282

8383
model_names = ["model_1", "model_2", "model_3", "model_4"]
84-
model_colors = {
85-
name: color
86-
for name, color in zip(model_names, px.colors.qualitative.D3[0 : len(model_names)])
87-
}
84+
model_colors = dict(zip(model_names, px.colors.qualitative.D3[0 : len(model_names)]))
8885

8986

9087
def app_ui(req):
@@ -224,7 +221,7 @@ def plot_timeseries():
224221
filtered_df(),
225222
x="time",
226223
y="score",
227-
labels=dict(score="accuracy"),
224+
labels={"score": "accuracy"},
228225
color="model",
229226
color_discrete_map=model_colors,
230227
# The default for render_mode is "auto", which switches between
@@ -238,13 +235,13 @@ def plot_timeseries():
238235
fig.add_hline(
239236
THRESHOLD_LOW,
240237
line_dash="dash",
241-
line=dict(color=THRESHOLD_LOW_COLOR, width=2),
238+
line={"color": THRESHOLD_LOW_COLOR, "width": 2},
242239
opacity=0.3,
243240
)
244241
fig.add_hline(
245242
THRESHOLD_MID,
246243
line_dash="dash",
247-
line=dict(color=THRESHOLD_MID_COLOR, width=2),
244+
line={"color": THRESHOLD_MID_COLOR, "width": 2},
248245
opacity=0.3,
249246
)
250247

@@ -260,7 +257,7 @@ def plot_dist():
260257
facet_row="model",
261258
nbins=20,
262259
x="score",
263-
labels=dict(score="accuracy"),
260+
labels={"score": "accuracy"},
264261
color="model",
265262
color_discrete_map=model_colors,
266263
template="simple_white",
@@ -269,13 +266,13 @@ def plot_dist():
269266
fig.add_vline(
270267
THRESHOLD_LOW,
271268
line_dash="dash",
272-
line=dict(color=THRESHOLD_LOW_COLOR, width=2),
269+
line={"color": THRESHOLD_LOW_COLOR, "width": 2},
273270
opacity=0.3,
274271
)
275272
fig.add_vline(
276273
THRESHOLD_MID,
277274
line_dash="dash",
278-
line=dict(color=THRESHOLD_MID_COLOR, width=2),
275+
line={"color": THRESHOLD_MID_COLOR, "width": 2},
279276
opacity=0.3,
280277
)
281278

examples/static_plots/app.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@
5454

5555

5656
def server(input: Inputs, output: Outputs, session: Session):
57+
rng = np.random.default_rng()
58+
5759
@reactive.calc
5860
def fake_data():
5961
n = 5000
6062
mean = [0, 0]
61-
rng = np.random.RandomState(0)
6263
cov = [(input.var(), input.cov()), (input.cov(), 1 / input.var())]
63-
return rng.multivariate_normal(mean, cov, n).T
64+
return np.random.default_rng(seed=0).multivariate_normal(mean, cov, n).T
6465

6566
@render.plot
6667
def seaborn():
@@ -98,7 +99,8 @@ def plotnine():
9899
@render.plot
99100
def pandas():
100101
ts = pd.Series(
101-
np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000)
102+
rng.standard_normal(1000),
103+
index=pd.date_range("1/1/2000", periods=1000),
102104
)
103105
ts = ts.cumsum()
104106
return ts.plot()

0 commit comments

Comments
 (0)