Skip to content

Commit 69d4f90

Browse files
authored
Backport PR #2112 on branch 0.12.x (ci: update Ruff) (#2114)
1 parent e21b1b7 commit 69d4f90

21 files changed

Lines changed: 218 additions & 251 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.12.5
3+
rev: v0.13.0
44
hooks:
5-
- id: ruff
5+
- id: ruff-check
66
args: ["--fix"]
77
- id: ruff-format
88
# The following can be removed once PLR0917 is out of preview

benchmarks/benchmarks/anndata.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ class GarbargeCollectionSuite:
1313
# custom because `memory_profiler` is a line-by-line profiler (also: https://github.com/pythonprofilers/memory_profiler/issues/402)
1414
def track_peakmem_garbage_collection(self, *_):
1515
def display_top(snapshot, key_type="lineno"):
16-
snapshot = snapshot.filter_traces(
17-
(
18-
tracemalloc.Filter(
19-
inclusive=False,
20-
filename_pattern="<frozen importlib._bootstrap>",
21-
),
22-
tracemalloc.Filter(
23-
inclusive=False,
24-
filename_pattern="<unknown>",
25-
),
26-
)
27-
)
16+
snapshot = snapshot.filter_traces((
17+
tracemalloc.Filter(
18+
inclusive=False,
19+
filename_pattern="<frozen importlib._bootstrap>",
20+
),
21+
tracemalloc.Filter(
22+
inclusive=False,
23+
filename_pattern="<unknown>",
24+
),
25+
))
2826
top_stats = snapshot.statistics(key_type)
2927
total = sum(stat.size for stat in top_stats)
3028
return total

benchmarks/benchmarks/readwrite.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ def peakmem_write_compressed(self, *_):
171171
self.adata.write_h5ad(self.writepth, compression="gzip")
172172

173173
def track_peakmem_write_compressed(self, *_):
174-
return get_peak_mem(
175-
(sedate(self.adata.write_h5ad), (self.writepth,), {"compression": "gzip"})
176-
)
174+
return get_peak_mem((
175+
sedate(self.adata.write_h5ad),
176+
(self.writepth,),
177+
{"compression": "gzip"},
178+
))
177179

178180

179181
class H5ADBackedWriteSuite(H5ADWriteSuite):

benchmarks/benchmarks/sparse_dataset.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ def make_alternating_mask(n):
2121

2222

2323
class SparseCSRContiguousSlice:
24-
_slices = MappingProxyType(
25-
{
26-
"0:1000": slice(0, 1000),
27-
"0:9000": slice(0, 9000),
28-
":9000:-1": slice(None, 9000, -1),
29-
"::-2": slice(None, None, 2),
30-
"array": np.array([0, 5000, 9999]),
31-
"arange": np.arange(0, 1000),
32-
"first": 0,
33-
"alternating": make_alternating_mask(10),
34-
}
35-
)
24+
_slices = MappingProxyType({
25+
"0:1000": slice(0, 1000),
26+
"0:9000": slice(0, 9000),
27+
":9000:-1": slice(None, 9000, -1),
28+
"::-2": slice(None, None, 2),
29+
"array": np.array([0, 5000, 9999]),
30+
"arange": np.arange(0, 1000),
31+
"first": 0,
32+
"alternating": make_alternating_mask(10),
33+
})
3634
params = (
3735
[
3836
(10_000, 10_000),

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ markers = [ "gpu: mark test to run on GPU", "zarr_io: mark tests that involve za
177177
src = [ "src" ]
178178

179179
[tool.ruff.format]
180+
preview = true
180181
docstring-code-format = true
181182

182183
[tool.ruff.lint]

src/anndata/_core/merge.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,15 +1748,10 @@ def concat( # noqa: PLR0912, PLR0913, PLR0915
17481748
for r, a in zip(reindexers, adatas, strict=True)
17491749
],
17501750
)
1751-
alt_pairwise = merge(
1752-
[
1753-
{
1754-
k: r(r(v, axis=0), axis=1)
1755-
for k, v in getattr(a, f"{alt_axis_name}p").items()
1756-
}
1757-
for r, a in zip(reindexers, adatas, strict=True)
1758-
]
1759-
)
1751+
alt_pairwise = merge([
1752+
{k: r(r(v, axis=0), axis=1) for k, v in getattr(a, f"{alt_axis_name}p").items()}
1753+
for r, a in zip(reindexers, adatas, strict=True)
1754+
])
17601755
uns = uns_merge([a.uns for a in adatas])
17611756

17621757
raw = None
@@ -1785,17 +1780,15 @@ def concat( # noqa: PLR0912, PLR0913, PLR0915
17851780
"not concatenating `.raw` attributes."
17861781
)
17871782
warn(msg, UserWarning, stacklevel=2)
1788-
return AnnData(
1789-
**{
1790-
"X": X,
1791-
"layers": layers,
1792-
axis_name: concat_annot,
1793-
alt_axis_name: alt_annot,
1794-
f"{axis_name}m": concat_mapping,
1795-
f"{alt_axis_name}m": alt_mapping,
1796-
f"{axis_name}p": concat_pairwise,
1797-
f"{alt_axis_name}p": alt_pairwise,
1798-
"uns": uns,
1799-
"raw": raw,
1800-
}
1801-
)
1783+
return AnnData(**{
1784+
"X": X,
1785+
"layers": layers,
1786+
axis_name: concat_annot,
1787+
alt_axis_name: alt_annot,
1788+
f"{axis_name}m": concat_mapping,
1789+
f"{alt_axis_name}m": alt_mapping,
1790+
f"{axis_name}p": concat_pairwise,
1791+
f"{alt_axis_name}p": alt_pairwise,
1792+
"uns": uns,
1793+
"raw": raw,
1794+
})

src/anndata/_core/sparse_dataset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ def get_compressed_vectors(
278278
indptr_slices = [slice(*(x.indptr[i : i + 2])) for i in row_idxs]
279279
# HDF5 cannot handle out-of-order integer indexing
280280
if isinstance(x.data, ZarrArray):
281-
as_np_indptr = np.concatenate(
282-
[np.arange(s.start, s.stop) for s in indptr_slices]
283-
)
281+
as_np_indptr = np.concatenate([
282+
np.arange(s.start, s.stop) for s in indptr_slices
283+
])
284284
data = x.data[as_np_indptr]
285285
indices = x.indices[as_np_indptr]
286286
else:
@@ -309,9 +309,9 @@ def get_compressed_vectors_for_slices(
309309
start_indptr = indptr_indices[0] - next(offsets)
310310
if len(slices) < 2: # there is only one slice so no need to concatenate
311311
return data, indices, start_indptr
312-
end_indptr = np.concatenate(
313-
[s[1:] - o for s, o in zip(indptr_indices[1:], offsets, strict=True)]
314-
)
312+
end_indptr = np.concatenate([
313+
s[1:] - o for s, o in zip(indptr_indices[1:], offsets, strict=True)
314+
])
315315
indptr = np.concatenate([start_indptr, end_indptr])
316316
return data, indices, indptr
317317

src/anndata/_core/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(
100100

101101
# TODO: This makes `deepcopy(obj)` return `obj._view_args.parent._adata_ref`, fix it
102102
def __deepcopy__(self, memo):
103-
parent, attrname, keys = self._view_args
103+
parent, attrname, _keys = self._view_args
104104
return deepcopy(getattr(parent._adata_ref, attrname))
105105

106106

src/anndata/_io/h5ad.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,13 @@ def read_h5ad(
264264

265265
def callback(func, elem_name: str, elem, iospec):
266266
if iospec.encoding_type == "anndata" or elem_name.endswith("/"):
267-
return AnnData(
268-
**{
269-
# This is covering up backwards compat in the anndata initializer
270-
# In most cases we should be able to call `func(elen[k])` instead
271-
k: read_dispatched(elem[k], callback)
272-
for k in elem
273-
if not k.startswith("raw.")
274-
}
275-
)
267+
return AnnData(**{
268+
# This is covering up backwards compat in the anndata initializer
269+
# In most cases we should be able to call `func(elen[k])` instead
270+
k: read_dispatched(elem[k], callback)
271+
for k in elem
272+
if not k.startswith("raw.")
273+
})
276274
elif elem_name.startswith("/raw."):
277275
return None
278276
elif elem_name == "/X" and "X" in as_sparse:

src/anndata/_io/specs/lazy_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from .registry import LazyDataStructures, LazyReader
3838

3939
BlockInfo = Mapping[
40-
Literal[None],
40+
None,
4141
dict[str, Sequence[tuple[int, int]]],
4242
]
4343

0 commit comments

Comments
 (0)