Skip to content

Commit bdcef41

Browse files
authored
Add RUF lints (#3582)
1 parent 1c5432b commit bdcef41

File tree

27 files changed

+131
-136
lines changed

27 files changed

+131
-136
lines changed

benchmarks/benchmarks/preprocessing_counts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class FastSuite:
8181
["pbmc3k", "pbmc68k_reduced", "bmmc", "lung93k"],
8282
["counts", "counts-off-axis"],
8383
)
84-
param_names = ["dataset", "layer"]
84+
param_names = ("dataset", "layer")
8585

8686
def time_calculate_qc_metrics(self, *_):
8787
sc.pp.calculate_qc_metrics(

benchmarks/benchmarks/preprocessing_log.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class FastSuite:
8484
["pbmc3k", "pbmc68k_reduced", "bmmc", "lung93k"],
8585
[None, "off-axis"],
8686
)
87-
param_names = ["dataset", "layer"]
87+
param_names = ("dataset", "layer")
8888

8989
def time_mean_var(self, *_):
9090
_get_mean_var(adata.X)

pyproject.toml

+12-21
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ select = [
243243
"PT", # Pytest style
244244
"PTH", # Pathlib instead of os.path
245245
"PYI", # Typing
246-
"RUF100", # Unused noqa
246+
"RUF", # Miscellaneous Ruff-only lints
247247
"SIM", # Simplify control flow
248248
"TC", # Manage type checking blocks
249249
"UP", # Update legacy syntax
@@ -252,27 +252,18 @@ select = [
252252
]
253253
external = [ "PLR0917" ] # preview lint that we use
254254
ignore = [
255-
# dict() syntax is preferrable when creating dicts for kwargs
256-
"C408",
257-
# E266 too many leading '#' for block comment -> Scanpy allows them for comments into sections
258-
"E262",
259-
# module level import not at top of file -> required to circumvent circular imports for Scanpys API
260-
"E402",
261-
# line too long -> we accept long comment lines; black gets rid of long code lines
262-
"E501",
263-
# allow I, O, l as variable names -> I is the identity matrix, i, j, k, l is reasonable indexing notation
264-
"E741",
265-
# We ban blank lines before docstrings instead of the opposite
266-
"D203",
267-
# We want multiline summaries to start on the first line, not the second
268-
"D213",
269-
# TODO: replace our current param docs reuse with this and remove it here:
270-
"D417",
271-
# Numbers like “2” aren’t that “magic”.
272-
"PLR2004",
273-
# `Literal["..."] | str` is useful for autocompletion
274-
"PYI051",
255+
"C408", # dict() syntax is preferrable when creating dicts for kwargs
256+
"E262", # E266 too many leading '#' for block comment -> Scanpy allows them for comments into sections
257+
"E402", # module level import not at top of file -> required to circumvent circular imports for Scanpys API
258+
"E501", # line too long -> we accept long comment lines; black gets rid of long code lines
259+
"E741", # allow I, O, l as variable names -> I is the identity matrix, i, j, k, l is reasonable indexing notation
260+
"D203", # We ban blank lines before docstrings instead of the opposite
261+
"D213", # We want multiline summaries to start on the first line, not the second
262+
"D417", # TODO: replace our current param docs reuse with this and remove it here:
263+
"PLR2004", # Numbers like “2” aren’t that “magic”.
264+
"PYI051", # `Literal["..."] | str` is useful for autocompletion
275265
]
266+
allowed-confusables = [ "×", "", "", "α" ]
276267
[tool.ruff.lint.per-file-ignores]
277268
# Do not assign a lambda expression, use a def
278269
"src/scanpy/tools/_rank_genes_groups.py" = [ "E731" ]

src/scanpy/__init__.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,23 @@
6060
del sys, annotate_doc_types
6161

6262
__all__ = [
63-
"__version__",
6463
"AnnData",
64+
"Neighbors",
65+
"Verbosity",
66+
"__version__",
6567
"concat",
68+
"datasets",
69+
"experimental",
70+
"external",
71+
"get",
72+
"logging",
73+
"metrics",
74+
"pl",
75+
"pp",
76+
"queries",
77+
"read",
78+
"read_10x_h5",
79+
"read_10x_mtx",
6680
"read_csv",
6781
"read_excel",
6882
"read_h5ad",
@@ -71,23 +85,9 @@
7185
"read_mtx",
7286
"read_text",
7387
"read_umi_tools",
74-
"read",
75-
"read_10x_h5",
76-
"read_10x_mtx",
7788
"read_visium",
78-
"write",
79-
"datasets",
80-
"experimental",
81-
"external",
82-
"get",
83-
"logging",
84-
"metrics",
85-
"queries",
86-
"pl",
87-
"pp",
88-
"tl",
89-
"Verbosity",
90-
"settings",
91-
"Neighbors",
9289
"set_figure_params",
90+
"settings",
91+
"tl",
92+
"write",
9393
]

src/scanpy/_compat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949
__all__ = [
5050
"DaskArray",
5151
"ZappyArray",
52+
"_numba_threading_layer",
53+
"deprecated",
5254
"fullname",
55+
"njit",
56+
"old_positionals",
5357
"pkg_metadata",
5458
"pkg_version",
55-
"old_positionals",
56-
"deprecated",
57-
"njit",
58-
"_numba_threading_layer",
5959
]
6060

6161

src/scanpy/datasets/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
__all__ = [
2020
"blobs",
2121
"burczynski06",
22+
"ebi_expression_atlas",
2223
"krumsiek11",
2324
"moignard15",
2425
"paul15",
@@ -27,5 +28,4 @@
2728
"pbmc68k_reduced",
2829
"toggleswitch",
2930
"visium_sge",
30-
"ebi_expression_atlas",
3131
]

src/scanpy/external/exporting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from anndata import AnnData
2424

25-
__all__ = ["spring_project", "cellbrowser"]
25+
__all__ = ["cellbrowser", "spring_project"]
2626

2727

2828
@old_positionals(

src/scanpy/external/pl.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import matplotlib.pyplot as plt
99
import numpy as np
10-
from anndata import AnnData # noqa: TCH002
11-
from matplotlib.axes import Axes # noqa: TCH002
10+
from anndata import AnnData # noqa: TC002
11+
from matplotlib.axes import Axes # noqa: TC002
1212
from sklearn.utils import deprecated
1313

1414
from .._compat import old_positionals
@@ -30,10 +30,10 @@
3030

3131

3232
__all__ = [
33-
"phate",
34-
"trimap",
3533
"harmony_timeseries",
34+
"phate",
3635
"sam",
36+
"trimap",
3737
"wishbone_marker_trajectory",
3838
]
3939

src/scanpy/external/pp/_harmony_integrate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import Sequence # noqa: TCH003
5+
from collections.abc import Sequence # noqa: TC003
66
from typing import TYPE_CHECKING
77

88
import numpy as np

src/scanpy/external/tl/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from ._wishbone import wishbone
1313

1414
__all__ = [
15+
"cyclone",
1516
"harmony_timeseries",
1617
"palantir",
1718
"palantir_results",
1819
"phate",
1920
"phenograph",
20-
"cyclone",
21-
"sandbag",
2221
"sam",
22+
"sandbag",
2323
"trimap",
2424
"wishbone",
2525
]

src/scanpy/metrics/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from ._metrics import confusion_matrix
77
from ._morans_i import morans_i
88

9-
__all__ = ["gearys_c", "morans_i", "confusion_matrix"]
9+
__all__ = ["confusion_matrix", "gearys_c", "morans_i"]

src/scanpy/metrics/_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
_Vals = NDArray | SpBase | DaskArray | pd.DataFrame | pd.Series
2525

2626

27-
__all__ = ["_get_graph", "_SparseMetric"]
27+
__all__ = ["_SparseMetric", "_get_graph"]
2828

2929

3030
@dataclass

src/scanpy/neighbors/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ def _handle_transformer(
704704
# Use defaults from UMAP’s `nearest_neighbors` function
705705
kwds.update(
706706
n_jobs=settings.n_jobs,
707-
n_trees=min(64, 5 + int(round((self._adata.n_obs) ** 0.5 / 20.0))),
708-
n_iters=max(5, int(round(np.log2(self._adata.n_obs)))),
707+
n_trees=min(64, 5 + round((self._adata.n_obs) ** 0.5 / 20.0)),
708+
n_iters=max(5, round(np.log2(self._adata.n_obs))),
709709
)
710710
transformer = PyNNDescentTransformer(**kwds)
711711
elif transformer == "rapids":

src/scanpy/plotting/__init__.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,30 @@
5555
from ._utils import matrix, timeseries, timeseries_as_heatmap, timeseries_subplot
5656

5757
__all__ = [
58-
"palettes",
58+
"DotPlot",
59+
"MatrixPlot",
60+
"StackedViolin",
5961
"clustermap",
6062
"correlation_matrix",
6163
"dendrogram",
62-
"heatmap",
63-
"ranking",
64-
"scatter",
65-
"tracksplot",
66-
"violin",
67-
"DotPlot",
64+
"diffmap",
6865
"dotplot",
69-
"MatrixPlot",
70-
"matrixplot",
71-
"filter_genes_dispersion",
72-
"highly_variable_genes",
73-
"highest_expr_genes",
74-
"set_rcParams_defaults",
75-
"set_rcParams_scanpy",
76-
"StackedViolin",
77-
"stacked_violin",
78-
"scrublet_score_distribution",
7966
"dpt_groups_pseudotime",
8067
"dpt_timeseries",
68+
"draw_graph",
69+
"embedding",
8170
"embedding_density",
71+
"filter_genes_dispersion",
72+
"heatmap",
73+
"highest_expr_genes",
74+
"highly_variable_genes",
75+
"matrix",
76+
"matrixplot",
77+
"paga",
78+
"paga_compare",
79+
"paga_path",
80+
"palettes",
81+
"pca",
8282
"pca_loadings",
8383
"pca_overview",
8484
"pca_scatter",
@@ -90,19 +90,19 @@
9090
"rank_genes_groups_stacked_violin",
9191
"rank_genes_groups_tracksplot",
9292
"rank_genes_groups_violin",
93+
"ranking",
94+
"scatter",
95+
"scrublet_score_distribution",
96+
"set_rcParams_defaults",
97+
"set_rcParams_scanpy",
9398
"sim",
94-
"paga",
95-
"paga_compare",
96-
"paga_path",
97-
"diffmap",
98-
"draw_graph",
99-
"embedding",
100-
"pca",
10199
"spatial",
102-
"tsne",
103-
"umap",
104-
"matrix",
100+
"stacked_violin",
105101
"timeseries",
106102
"timeseries_as_heatmap",
107103
"timeseries_subplot",
104+
"tracksplot",
105+
"tsne",
106+
"umap",
107+
"violin",
108108
]

src/scanpy/plotting/_anndata.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections import OrderedDict
66
from collections.abc import Collection, Mapping, Sequence
7-
from itertools import product
7+
from itertools import pairwise, product
88
from types import NoneType
99
from typing import TYPE_CHECKING, NamedTuple, TypedDict, cast
1010

@@ -885,7 +885,7 @@ def violin( # noqa: PLR0912, PLR0913, PLR0915
885885
raise ValueError(msg)
886886

887887
if groupby is not None:
888-
obs_df = get.obs_df(adata, keys=[groupby] + keys, layer=layer, use_raw=use_raw)
888+
obs_df = get.obs_df(adata, keys=[groupby, *keys], layer=layer, use_raw=use_raw)
889889
if kwds.get("palette") is None:
890890
if not isinstance(adata.obs[groupby].dtype, CategoricalDtype):
891891
msg = (
@@ -1640,8 +1640,8 @@ def tracksplot( # noqa: PLR0912, PLR0913, PLR0915
16401640
# obtain the start and end of each category and make
16411641
# a list of ranges that will be used to plot a different
16421642
# color
1643-
cumsum = [0] + list(np.cumsum(obs_tidy.index.value_counts(sort=False)))
1644-
x_values = [(x, y) for x, y in zip(cumsum[:-1], cumsum[1:], strict=True)]
1643+
cumsum = [0, *np.cumsum(obs_tidy.index.value_counts(sort=False)).tolist()]
1644+
x_values = list(pairwise(cumsum))
16451645

16461646
dendro_height = 1 if dendrogram else 0
16471647

@@ -2071,7 +2071,7 @@ def _prepare_dataframe( # noqa: PLR0912
20712071
# if not a list, turn into a list
20722072
groupby = [groupby]
20732073
for group in groupby:
2074-
if group not in list(adata.obs_keys()) + [adata.obs.index.name]:
2074+
if group not in [*adata.obs_keys(), adata.obs.index.name]:
20752075
if adata.obs.index.name is not None:
20762076
msg = f' or index name "{adata.obs.index.name}"'
20772077
else:

src/scanpy/plotting/_tools/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import functools
4+
import operator
35
from collections.abc import Mapping, Sequence
46
from copy import copy
57
from typing import TYPE_CHECKING
@@ -546,7 +548,9 @@ def _rank_genes_groups_plot( # noqa: PLR0912, PLR0913, PLR0915
546548
if var_names is not None:
547549
if isinstance(var_names, Mapping):
548550
# get a single list of all gene names in the dictionary
549-
var_names_list = sum([list(x) for x in var_names.values()], [])
551+
var_names_list = functools.reduce(
552+
operator.iadd, [list(x) for x in var_names.values()], []
553+
)
550554
elif isinstance(var_names, str):
551555
var_names_list = [var_names]
552556
else:

src/scanpy/plotting/_tools/paga.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,14 @@ def _paga_graph( # noqa: PLR0912, PLR0913, PLR0915
982982

983983
cumsum = np.cumsum(fracs)
984984
cumsum = cumsum / cumsum[-1]
985-
cumsum = [0] + cumsum.tolist()
985+
cumsum = [0, *cumsum.tolist()]
986986

987987
for r1, r2, color in zip(
988988
cumsum[:-1], cumsum[1:], color_single, strict=True
989989
):
990990
angles = np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 20)
991-
x = [0] + np.cos(angles).tolist()
992-
y = [0] + np.sin(angles).tolist()
991+
x = [0, *np.cos(angles).tolist()]
992+
y = [0, *np.sin(angles).tolist()]
993993

994994
xy = np.column_stack([x, y])
995995
s = np.abs(xy).max()

0 commit comments

Comments
 (0)