Skip to content

chore(lints): Add many rules for ruff #1213

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 58 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
cf555aa
pycodestyle
schloerke Mar 12, 2024
8ac19b4
isort
schloerke Mar 12, 2024
0fa0840
flake8-bugbear
schloerke Mar 12, 2024
750378c
Fix bugbear lints
schloerke Mar 12, 2024
9142772
flake8-quotes
schloerke Mar 12, 2024
26dec42
commas
schloerke Mar 12, 2024
8de81b2
flake8-comprehensions
schloerke Mar 12, 2024
fd4c9d3
When formatting, fix all lints
schloerke Mar 12, 2024
cdcb7fb
Add unsafe format target for ruff
schloerke Mar 12, 2024
6728a58
Auto fix flake8-comprehensions
schloerke Mar 12, 2024
e55f7f6
flake8-future-annotations
schloerke Mar 12, 2024
33426a1
Only use FA102
schloerke Mar 12, 2024
a6ba322
Auto fix FA102
schloerke Mar 12, 2024
acd766c
flake8-implicit-str-concat
schloerke Mar 12, 2024
d2b3d25
Manually fix flake8-implicit-str-concat errors
schloerke Mar 12, 2024
eeb42c4
flake8-import-conventions
schloerke Mar 12, 2024
3bf4391
Fix flake8-import-conventions errors
schloerke Mar 12, 2024
a8a1e65
Do not use flake8-logging-format
schloerke Mar 12, 2024
e02c851
flake8-pie
schloerke Mar 12, 2024
c789b63
Fix flake8-pie
schloerke Mar 12, 2024
1e2b947
Drop flake8-print
schloerke Mar 12, 2024
a4c0d9c
flake8-pyi
schloerke Mar 12, 2024
07c1729
Auto fix flake8-pyi
schloerke Mar 12, 2024
62e101d
Manually fix flake8-pyi return Self
schloerke Mar 12, 2024
35a5368
flake8-pytest-style
schloerke Mar 12, 2024
56fb78c
Auto fix flake8-pytest-style
schloerke Mar 12, 2024
ca4b7d8
More auto fix flake8-pytest-style
schloerke Mar 12, 2024
6506d7f
Manually fix remaining PT lints
schloerke Mar 12, 2024
23e618b
flake8-simplify Use `key {operator} dict`
schloerke Mar 12, 2024
fd287b0
Auto fix flake8-simplify Use `key {operator} dict`
schloerke Mar 12, 2024
0d8755b
flake8-type-checking
schloerke Mar 12, 2024
8562775
Fix broken tests
schloerke Mar 12, 2024
354dbaa
Auto fix type checking lint
schloerke Mar 12, 2024
c32368f
Drop pandas-vet
schloerke Mar 12, 2024
9b35e7c
flynt
schloerke Mar 12, 2024
d7da495
Auto fix flynt
schloerke Mar 12, 2024
2c298a5
NumPy-specific rules
schloerke Mar 12, 2024
7864da1
Autofix NumPy-specific rule
schloerke Mar 12, 2024
dcbab56
Manually fix many random state lints
schloerke Mar 12, 2024
698e83f
Ruff specific rules; Consider {expression} instead of concatenation &…
schloerke Mar 12, 2024
0a00cb0
Autofix ruff specific rules
schloerke Mar 12, 2024
6a38273
Manually fix ruff specific rules
schloerke Mar 12, 2024
4b0bbd3
Update Makefile
schloerke Mar 12, 2024
7f4eb86
Remove duplicate work
schloerke Mar 12, 2024
d57d8f3
Do not auto update fixtures from return to yield statements
schloerke Mar 12, 2024
356394a
fix some assertions by checking the value
schloerke Mar 12, 2024
f663011
Merge branch 'build_tools' into build_tools_ruff
schloerke Mar 12, 2024
85d3a9d
Merge branch 'build_tools' into build_tools_ruff
schloerke Mar 12, 2024
1cb1fce
Consolidate lines
schloerke Mar 12, 2024
9b140ca
Code feedback
schloerke Mar 12, 2024
174a146
Disable C408 for file
schloerke Mar 12, 2024
062cddc
Disable `B904` / revert changes
schloerke Mar 12, 2024
beaf115
Disable flynt static string lint
schloerke Mar 12, 2024
9fa6aec
Fix bad usage of numpy-legacy-random
schloerke Mar 12, 2024
bfd6566
numpy random s/randn/standard_normal/
schloerke Mar 12, 2024
8e0fda3
More checks for shiny/__init__.py
schloerke Mar 12, 2024
74b01a4
Update comment
schloerke Mar 12, 2024
8fd601f
Format date for py3.12
schloerke Mar 12, 2024
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
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,12 @@ clean-test: FORCE ## remove test and coverage artifacts
# Check lint, test, and format of code
# -----------------
check: check-lint check-types check-tests ## check code, style, types, and test (basic CI)
check-fix: format check-lint check-types check-tests ## check and format code, style, types, and test
check-lint: check-ruff ## check code formatting and style
check-fix: format check-types check-tests ## check and format code, style, types, and test
check-lint: check-ruff ## check code lints and format

check-ruff: $(RUFF) FORCE
@echo "-------- Running ruff lint and formatting checks --------"
@echo "-------- Running ruff lint and format checks --------"
@# Check imports in addition to code
@# Reason for two commands: https://github.com/astral-sh/ruff/issues/8232
# . $(PYBIN)/activate && \
# ruff check --select I --fix .
# Check lints
. $(PYBIN)/activate && \
ruff check .
Expand Down Expand Up @@ -225,13 +222,17 @@ format: format-ruff ## format code
format-ruff: $(RUFF) FORCE
@echo "-------- Formatting code with ruff --------"
@# Reason for two commands: https://github.com/astral-sh/ruff/issues/8232
@# Fix imports
@# Fix lints
. $(PYBIN)/activate && \
ruff check --select I --fix .
ruff check --fix .
@# Fix formatting
. $(PYBIN)/activate && \
ruff format .

format-ruff-unsafe: $(RUFF) FORCE
@echo "-------- Formatting code with ruff (unsafe) --------"
. $(PYBIN)/activate && \
ruff check --fix --unsafe-fixes .
# -----------------
# Documentation
# -----------------
Expand Down
18 changes: 9 additions & 9 deletions examples/brownian/brownian_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# * `brownian_motion()` is a function that generates a random walk
# * `brownian_widget()` uses restructured plotting code given in article

rs = np.random.RandomState()
rng = np.random.default_rng()


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


Expand All @@ -35,12 +35,12 @@ def brownian_widget(width=600, height=600):
x=[],
y=[],
z=[],
marker=dict(
size=4,
color=[],
colorscale="Viridis",
),
line=dict(color="darkblue", width=2),
marker={
"size": 4,
"color": [],
"colorscale": "Viridis",
},
line={"color": "darkblue", "width": 2},
)
],
layout={"showlegend": False, "width": width, "height": height},
Expand Down
14 changes: 7 additions & 7 deletions examples/brownian/mediapipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def rel_hand(start_pos: int, end_pos: int):
normal_unit_vec = normal_vec / np.linalg.norm(normal_vec)

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

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


def xyz_mean(points):
return dict(
x=mean([p["x"] for p in points]),
y=mean([p["y"] for p in points]),
z=mean([p["z"] for p in points]),
)
return {
"x": mean([p["x"] for p in points]),
"y": mean([p["y"] for p in points]),
"z": mean([p["z"] for p in points]),
}
6 changes: 3 additions & 3 deletions examples/cpuinfo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from math import ceil

import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -17,7 +17,7 @@

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

# max number of samples to retain
MAX_SAMPLES = 1000
Expand Down Expand Up @@ -166,7 +166,7 @@ def plot():
ncols=ncols,
squeeze=False,
)
for i in range(0, ncols * nrows):
for i in range(ncols * nrows):
row = i // ncols
col = i % ncols
axes = axeses[row, col]
Expand Down
5 changes: 3 additions & 2 deletions examples/cpuinfo/fakepsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ def cpu_count(logical: bool = True):
return 8 if logical else 4


last_sample = np.random.uniform(0, 100, size=cpu_count(True))
rng = np.random.default_rng()
last_sample = rng.uniform(0, 100, size=cpu_count(True))


def cpu_percent(percpu: bool = False):
global last_sample
delta = np.random.normal(scale=10, size=len(last_sample))
delta = rng.normal(scale=10, size=len(last_sample))
last_sample = (last_sample + delta).clip(0, 100)
if percpu:
return last_sample.tolist()
Expand Down
6 changes: 5 additions & 1 deletion examples/dataframe/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import pandas as pd
from typing import TYPE_CHECKING

import seaborn as sns
from shinyswatch.theme import darkly

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

if TYPE_CHECKING:
import pandas as pd


def app_ui(req):
dark = True if "dark" in req.query_params else None
Expand Down
3 changes: 1 addition & 2 deletions examples/express/accordion_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ def txt():

@render.plot
def histogram():
np.random.seed(19680801)
x = 100 + 15 * np.random.randn(437)
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
plt.hist(x, input.n(), density=True)
6 changes: 2 additions & 4 deletions examples/express/column_wrap_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@

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

with ui.card():

@render.plot
def histogram2():
np.random.seed(19680801)
x = 100 + 15 * np.random.randn(437)
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
plt.hist(x, input.n(), density=True, color="red")
6 changes: 2 additions & 4 deletions examples/express/nav_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

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

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

@render.plot
def histogram2():
np.random.seed(19680801)
x = 100 + 15 * np.random.randn(437)
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
plt.hist(x, input.n2(), density=True)
3 changes: 1 addition & 2 deletions examples/express/plot_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@

@render.plot
def histogram():
np.random.seed(19680801)
x = 100 + 15 * np.random.randn(437)
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
plt.hist(x, input.n(), density=True)
3 changes: 1 addition & 2 deletions examples/express/shared_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

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


Expand Down
3 changes: 1 addition & 2 deletions examples/express/sidebar_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@

@render.plot
def histogram():
np.random.seed(19680801)
x = 100 + 15 * np.random.randn(437)
x = 100 + 15 * np.random.default_rng(seed=19680801).standard_normal(437)
plt.hist(x, input.n(), density=True)
19 changes: 8 additions & 11 deletions examples/model-score/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def df():
params=[150],
)
# Convert timestamp to datetime object, which SQLite doesn't support natively
tbl["timestamp"] = pd.to_datetime(tbl["timestamp"], utc=True)
tbl["timestamp"] = pd.to_datetime(tbl["timestamp"], utc=True, format="ISO8601")
# Create a short label for readability
tbl["time"] = tbl["timestamp"].dt.strftime("%H:%M:%S")
# Reverse order of rows
Expand All @@ -81,10 +81,7 @@ def read_time_period(from_time, to_time):


model_names = ["model_1", "model_2", "model_3", "model_4"]
model_colors = {
name: color
for name, color in zip(model_names, px.colors.qualitative.D3[0 : len(model_names)])
}
model_colors = dict(zip(model_names, px.colors.qualitative.D3[0 : len(model_names)]))


def app_ui(req):
Expand Down Expand Up @@ -224,7 +221,7 @@ def plot_timeseries():
filtered_df(),
x="time",
y="score",
labels=dict(score="accuracy"),
labels={"score": "accuracy"},
color="model",
color_discrete_map=model_colors,
# The default for render_mode is "auto", which switches between
Expand All @@ -238,13 +235,13 @@ def plot_timeseries():
fig.add_hline(
THRESHOLD_LOW,
line_dash="dash",
line=dict(color=THRESHOLD_LOW_COLOR, width=2),
line={"color": THRESHOLD_LOW_COLOR, "width": 2},
opacity=0.3,
)
fig.add_hline(
THRESHOLD_MID,
line_dash="dash",
line=dict(color=THRESHOLD_MID_COLOR, width=2),
line={"color": THRESHOLD_MID_COLOR, "width": 2},
opacity=0.3,
)

Expand All @@ -260,7 +257,7 @@ def plot_dist():
facet_row="model",
nbins=20,
x="score",
labels=dict(score="accuracy"),
labels={"score": "accuracy"},
color="model",
color_discrete_map=model_colors,
template="simple_white",
Expand All @@ -269,13 +266,13 @@ def plot_dist():
fig.add_vline(
THRESHOLD_LOW,
line_dash="dash",
line=dict(color=THRESHOLD_LOW_COLOR, width=2),
line={"color": THRESHOLD_LOW_COLOR, "width": 2},
opacity=0.3,
)
fig.add_vline(
THRESHOLD_MID,
line_dash="dash",
line=dict(color=THRESHOLD_MID_COLOR, width=2),
line={"color": THRESHOLD_MID_COLOR, "width": 2},
opacity=0.3,
)

Expand Down
8 changes: 5 additions & 3 deletions examples/static_plots/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@


def server(input: Inputs, output: Outputs, session: Session):
rng = np.random.default_rng()

@reactive.calc
def fake_data():
n = 5000
mean = [0, 0]
rng = np.random.RandomState(0)
cov = [(input.var(), input.cov()), (input.cov(), 1 / input.var())]
return rng.multivariate_normal(mean, cov, n).T
return np.random.default_rng(seed=0).multivariate_normal(mean, cov, n).T

@render.plot
def seaborn():
Expand Down Expand Up @@ -98,7 +99,8 @@ def plotnine():
@render.plot
def pandas():
ts = pd.Series(
np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000)
rng.standard_normal(1000),
index=pd.date_range("1/1/2000", periods=1000),
)
ts = ts.cumsum()
return ts.plot()
Expand Down
Loading