Skip to content

Commit a2903c5

Browse files
Backport PR #3795 on branch 1.11.x (ci: update Ruff) (#3796)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 70f0077 commit a2903c5

37 files changed

Lines changed: 212 additions & 219 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.12.7
3+
rev: v0.13.0
44
hooks:
55
- id: ruff-check
66
args: ["--fix"]

benchmarks/benchmarks/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ParamSkipper(Protocol):
2828
def __call__(self, **skipped: AbstractSet) -> Callable[[C], C]: ...
2929

3030
Dataset = Literal["pbmc68k_reduced", "pbmc3k", "bmmc", "lung93k"]
31-
KeyX = Literal[None, "off-axis"]
31+
KeyX = Literal["off-axis"] | None
3232
KeyCount = Literal["counts", "counts-off-axis"]
3333

3434

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ exclude_also = [
215215
src = [ "src" ]
216216

217217
[tool.ruff.format]
218+
preview = true
218219
docstring-code-format = true
219220

220221
[tool.ruff.lint]

src/scanpy/_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def old_positionals(*old_positionals: str):
117117

118118
def add_note(exc: BaseException, note: str) -> None:
119119
exc.add_note(note)
120+
120121
else:
121122

122123
def add_note(exc: BaseException, note: str) -> None:

src/scanpy/_settings/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class SettingsMeta(SingletonMeta, type):
8383
_cachedir: Path
8484
_datasetdir: Path
8585
_figdir: Path
86-
_cache_compression: Literal["lzf", "gzip", None]
86+
_cache_compression: Literal["lzf", "gzip"] | None
8787
_max_memory: float
8888
_n_jobs: int
8989
_categories_to_ignore: list[str]
@@ -238,12 +238,14 @@ def figdir(cls, figdir: Path | str) -> None:
238238
cls._figdir = Path(figdir)
239239

240240
@property
241-
def cache_compression(cls) -> Literal["lzf", "gzip", None]:
241+
def cache_compression(cls) -> Literal["lzf", "gzip"] | None:
242242
"""Compression for `sc.read(..., cache=True)` (default `'lzf'`)."""
243243
return cls._cache_compression
244244

245245
@cache_compression.setter
246-
def cache_compression(cls, cache_compression: Literal["lzf", "gzip", None]) -> None:
246+
def cache_compression(
247+
cls, cache_compression: Literal["lzf", "gzip"] | None
248+
) -> None:
247249
if cache_compression not in {"lzf", "gzip", None}:
248250
msg = (
249251
f"`cache_compression` ({cache_compression}) "

src/scanpy/external/exporting.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -440,24 +440,24 @@ def _export_paga_to_spring(adata, paga_coords, outpath) -> None:
440440
for i, name, xy, color, size, cells in zip(
441441
range(len(names)), names, coords, colors, sizes, cell_groups, strict=True
442442
):
443-
nodes.append(
444-
{
445-
"index": i,
446-
"size": int(size),
447-
"color": color,
448-
"coordinates": xy,
449-
"cells": cells,
450-
"name": name,
451-
}
452-
)
443+
nodes.append({
444+
"index": i,
445+
"size": int(size),
446+
"color": color,
447+
"coordinates": xy,
448+
"cells": cells,
449+
"name": name,
450+
})
453451

454452
# make link list, avoid redundant encoding (graph is undirected)
455453
links = []
456454
for source, target, weight in zip(sources, targets, weights, strict=True):
457455
if source < target and weight > min_edge_weight_save:
458-
links.append(
459-
{"source": int(source), "target": int(target), "weight": float(weight)}
460-
)
456+
links.append({
457+
"source": int(source),
458+
"target": int(target),
459+
"weight": float(weight),
460+
})
461461

462462
# save data about edge weights
463463
edge_weight_meta = {

src/scanpy/get/_aggregated.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ def _combine_categories(
393393

394394
# It's like np.concatenate([x for x in product(*[range(n) for n in n_categories])])
395395
code_combinations = np.indices(n_categories).reshape(len(n_categories), -1)
396-
result_categories = pd.Index(
397-
["_".join(map(str, x)) for x in product(*[df[c].cat.categories for c in cols])]
398-
)
396+
result_categories = pd.Index([
397+
"_".join(map(str, x)) for x in product(*[df[c].cat.categories for c in cols])
398+
])
399399

400400
# Dataframe with unique combination of categories for each row
401401
new_label_df = pd.DataFrame(

src/scanpy/plotting/_anndata.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,14 +1344,10 @@ def heatmap( # noqa: PLR0912, PLR0913, PLR0915
13441344

13451345
if categorical:
13461346
groupby_ax = fig.add_subplot(axs[1, 0])
1347-
(
1348-
label2code,
1349-
ticks,
1350-
labels,
1351-
groupby_cmap,
1352-
norm,
1353-
) = _plot_categories_as_colorblocks(
1354-
groupby_ax, obs_tidy, colors=groupby_colors, orientation="left"
1347+
label2code, ticks, _labels, groupby_cmap, norm = (
1348+
_plot_categories_as_colorblocks(
1349+
groupby_ax, obs_tidy, colors=groupby_colors, orientation="left"
1350+
)
13551351
)
13561352

13571353
# add lines to main heatmap
@@ -1437,14 +1433,10 @@ def heatmap( # noqa: PLR0912, PLR0913, PLR0915
14371433

14381434
if categorical:
14391435
groupby_ax = fig.add_subplot(axs[2, 0])
1440-
(
1441-
label2code,
1442-
ticks,
1443-
labels,
1444-
groupby_cmap,
1445-
norm,
1446-
) = _plot_categories_as_colorblocks(
1447-
groupby_ax, obs_tidy, colors=groupby_colors, orientation="bottom"
1436+
label2code, ticks, _labels, groupby_cmap, norm = (
1437+
_plot_categories_as_colorblocks(
1438+
groupby_ax, obs_tidy, colors=groupby_colors, orientation="bottom"
1439+
)
14481440
)
14491441
# add lines to main heatmap
14501442
line_positions = (
@@ -1701,7 +1693,7 @@ def tracksplot( # noqa: PLR0912, PLR0913, PLR0915
17011693
ax.spines["top"].set_visible(False)
17021694
ax.spines["bottom"].set_visible(False)
17031695
ax.grid(visible=False)
1704-
ymin, ymax = ax.get_ylim()
1696+
_ymin, ymax = ax.get_ylim()
17051697
ymax = int(ymax)
17061698
ax.set_yticks([ymax])
17071699
ax.set_yticklabels([str(ymax)], ha="left", va="top")
@@ -1730,7 +1722,7 @@ def tracksplot( # noqa: PLR0912, PLR0913, PLR0915
17301722

17311723
groupby_ax = fig.add_subplot(axs2[1])
17321724

1733-
label2code, ticks, labels, groupby_cmap, norm = _plot_categories_as_colorblocks(
1725+
_label2code, ticks, _labels, groupby_cmap, norm = _plot_categories_as_colorblocks(
17341726
groupby_ax, obs_tidy.T, colors=groupby_colors, orientation="bottom"
17351727
)
17361728
# add lines to plot
@@ -2326,9 +2318,10 @@ def _reorder_categories_after_dendrogram(
23262318
position = var_groups.positions[idx]
23272319
_var_names = var_names[position[0] : position[1] + 1]
23282320
var_names_idx_ordered.extend(range(position[0], position[1] + 1))
2329-
positions_ordered.append(
2330-
(position_start, position_start + len(_var_names) - 1)
2331-
)
2321+
positions_ordered.append((
2322+
position_start,
2323+
position_start + len(_var_names) - 1,
2324+
))
23322325
position_start += len(_var_names)
23332326
labels_ordered.append(var_groups.labels[idx])
23342327
var_groups = VarGroups(labels_ordered, positions_ordered)
@@ -2662,7 +2655,7 @@ def _plot_colorbar(mappable, fig, subplot_spec, max_cbar_height: float = 4.0):
26622655
color bar ax
26632656
26642657
"""
2665-
width, height = fig.get_size_inches()
2658+
_width, height = fig.get_size_inches()
26662659
if height > max_cbar_height:
26672660
# to make the colorbar shorter, the
26682661
# ax is split and the lower portion is used.

src/scanpy/plotting/_qc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def highest_expr_genes(
9898
# figsize is hardcoded to produce a tall image. To change the fig size,
9999
# a matplotlib.axes.Axes object needs to be passed.
100100
height = (n_top * 0.2) + 1.5
101-
fig, ax = plt.subplots(figsize=(5, height))
101+
_fig, ax = plt.subplots(figsize=(5, height))
102102
sns.boxplot(data=counts_top_genes, orient="h", ax=ax, fliersize=1, **kwds)
103103
ax.set_xlabel("% of total counts")
104104
if log:

src/scanpy/plotting/_tools/paga.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,10 @@ def _compute_pos( # noqa: PLR0912
258258
# I don't know why this is necessary
259259
# np.random.seed(random_state)
260260
if init_pos is None:
261-
init_coords = random_state.random_sample(
262-
(adjacency_solid.shape[0], 2)
263-
).tolist()
261+
init_coords = random_state.random_sample((
262+
adjacency_solid.shape[0],
263+
2,
264+
)).tolist()
264265
else:
265266
init_pos = init_pos.copy()
266267
# this is a super-weird hack that is necessary as igraph’s
@@ -582,7 +583,7 @@ def is_flat(x):
582583
)
583584

584585
if plot:
585-
axs, panel_pos, draw_region_width, figure_width = _utils.setup_axes(
586+
axs, panel_pos, draw_region_width, _figure_width = _utils.setup_axes(
586587
ax, panels=colors, colorbars=colorbars
587588
)
588589

@@ -797,7 +798,7 @@ def _paga_graph( # noqa: PLR0912, PLR0913, PLR0915
797798
and colors in adata.obs
798799
and isinstance(adata.obs[colors].dtype, CategoricalDtype)
799800
):
800-
asso_names, asso_matrix = _sc_utils.compute_association_matrix_of_groups(
801+
_asso_names, asso_matrix = _sc_utils.compute_association_matrix_of_groups(
801802
adata,
802803
prediction=groups_key,
803804
reference=colors,
@@ -1268,14 +1269,12 @@ def moving_average(a):
12681269

12691270
# groups bar
12701271
ax_bounds = ax.get_position().bounds
1271-
groups_axis = plt.axes(
1272-
(
1273-
ax_bounds[0],
1274-
ax_bounds[1] - ax_bounds[3] / len(keys),
1275-
ax_bounds[2],
1276-
ax_bounds[3] / len(keys),
1277-
)
1278-
)
1272+
groups_axis = plt.axes((
1273+
ax_bounds[0],
1274+
ax_bounds[1] - ax_bounds[3] / len(keys),
1275+
ax_bounds[2],
1276+
ax_bounds[3] / len(keys),
1277+
))
12791278
groups = np.array(groups)[None, :]
12801279
groups_axis.imshow(
12811280
groups,
@@ -1307,14 +1306,12 @@ def moving_average(a):
13071306
for ianno, anno in enumerate(annotations):
13081307
if ianno > 0:
13091308
y_shift = ax_bounds[3] / len(keys) / 2
1310-
anno_axis = plt.axes(
1311-
(
1312-
ax_bounds[0],
1313-
ax_bounds[1] - (ianno + 2) * y_shift,
1314-
ax_bounds[2],
1315-
y_shift,
1316-
)
1317-
)
1309+
anno_axis = plt.axes((
1310+
ax_bounds[0],
1311+
ax_bounds[1] - (ianno + 2) * y_shift,
1312+
ax_bounds[2],
1313+
y_shift,
1314+
))
13181315
arr = np.array(anno_dict[anno])[None, :]
13191316
if anno not in color_maps_annotations:
13201317
color_map_anno = (

0 commit comments

Comments
 (0)