Skip to content

Commit 60d5afb

Browse files
authored
Widen line lengths for overload authoring (#93)
1 parent f3b1eaa commit 60d5afb

26 files changed

+99
-311
lines changed

docs/conf.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@
4949
autodoc_default_options = {
5050
"special-members": True,
5151
# everything except __call__ really, to avoid having to write autosummary templates
52-
"exclude-members": (
53-
"__setattr__,__delattr__,__repr__,__eq__,__or__,__ror__,__hash__,__weakref__,__init__,__new__"
54-
),
52+
"exclude-members": "__setattr__,__delattr__,__repr__,__eq__,__or__,__ror__,__hash__,__weakref__,__init__,__new__",
5553
}
5654
napoleon_google_docstring = False
5755
napoleon_numpy_docstring = True
@@ -115,27 +113,21 @@ def find_type_alias(name: str) -> tuple[str, str] | tuple[None, None]:
115113
return None, None
116114

117115

118-
def resolve_type_aliases(
119-
app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement
120-
) -> reference | None:
116+
def resolve_type_aliases(app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement) -> reference | None:
121117
"""Resolve :class: references to our type aliases as :attr: instead."""
122118
if (node["refdomain"], node["reftype"]) != ("py", "class"):
123119
return None
124120
typ, target = find_type_alias(node["reftarget"])
125121
if typ is None or target is None:
126122
return None
127123
if target.startswith("fast_array_utils."):
128-
ref = env.get_domain("py").resolve_xref(
129-
env, node["refdoc"], app.builder, typ, target, node, contnode
130-
)
124+
ref = env.get_domain("py").resolve_xref(env, node["refdoc"], app.builder, typ, target, node, contnode)
131125
else:
132126
from sphinx.ext.intersphinx import resolve_reference_any_inventory
133127

134128
node["reftype"] = typ
135129
node["reftarget"] = target
136-
ref = resolve_reference_any_inventory(
137-
env=env, honor_disabled_refs=False, node=node, contnode=contnode
138-
)
130+
ref = resolve_reference_any_inventory(env=env, honor_disabled_refs=False, node=node, contnode=contnode)
139131
if ref is None:
140132
msg = f"Could not resolve {typ} {target} (from {node['reftarget']})"
141133
raise AssertionError(msg)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ python = [ "3.13", "3.12", "3.11" ]
9393
extras = [ "full", "default", "min" ]
9494

9595
[tool.ruff]
96-
line-length = 100
96+
line-length = 160
9797
namespace-packages = [ "src/testing" ]
9898
format.preview = true
9999
format.docstring-code-format = true

src/fast_array_utils/_plugins/numba_sparse.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,9 @@ def __init__(self, dmm: DataModelManager, fe_type: CSType) -> None:
124124
sparse.csr_array,
125125
sparse.csc_array,
126126
]
127-
TYPES: Sequence[type[CSType]] = [
128-
type(f"{cls.__name__}Type", (CSType,), {"cls": cls, "name": cls.__name__}) for cls in CLASSES
129-
]
130-
TYPEOF_FUNCS: Mapping[type[CSBase], Callable[[CSBase, _TypeofContext], CSType]] = {
131-
typ.cls: make_typeof_fn(typ) for typ in TYPES
132-
}
133-
MODELS: Mapping[type[CSType], type[CSModel]] = {
134-
typ: type(f"{typ.cls.__name__}Model", (CSModel,), {}) for typ in TYPES
135-
}
127+
TYPES: Sequence[type[CSType]] = [type(f"{cls.__name__}Type", (CSType,), {"cls": cls, "name": cls.__name__}) for cls in CLASSES]
128+
TYPEOF_FUNCS: Mapping[type[CSBase], Callable[[CSBase, _TypeofContext], CSType]] = {typ.cls: make_typeof_fn(typ) for typ in TYPES}
129+
MODELS: Mapping[type[CSType], type[CSModel]] = {typ: type(f"{typ.cls.__name__}Model", (CSModel,), {}) for typ in TYPES}
136130

137131

138132
def unbox_matrix(typ: CSType, obj: Value, c: UnboxContext) -> NativeValue:
@@ -253,9 +247,7 @@ def overload_sparse_copy(inst: CSType) -> None | Callable[[CSType], CSType]:
253247

254248
# nopython code:
255249
def copy(inst: CSType) -> CSType: # pragma: no cover
256-
return _sparse_copy(
257-
inst, inst.data.copy(), inst.indices.copy(), inst.indptr.copy(), inst.shape
258-
) # type: ignore[return-value]
250+
return _sparse_copy(inst, inst.data.copy(), inst.indices.copy(), inst.indptr.copy(), inst.shape) # type: ignore[return-value]
259251

260252
return copy
261253

src/fast_array_utils/conv/__init__.py

+5-23
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,23 @@
2121

2222

2323
@overload
24-
def to_dense(
25-
x: CpuArray | DiskArray | types.sparray | types.spmatrix | types.CSDataset,
26-
/,
27-
*,
28-
to_cpu_memory: bool = False,
29-
) -> NDArray[Any]: ...
24+
def to_dense(x: CpuArray | DiskArray | types.sparray | types.spmatrix | types.CSDataset, /, *, to_cpu_memory: bool = False) -> NDArray[Any]: ...
3025

3126

3227
@overload
33-
def to_dense(
34-
x: types.DaskArray, /, *, to_cpu_memory: Literal[False] = False
35-
) -> types.DaskArray: ...
28+
def to_dense(x: types.DaskArray, /, *, to_cpu_memory: Literal[False] = False) -> types.DaskArray: ...
3629
@overload
3730
def to_dense(x: types.DaskArray, /, *, to_cpu_memory: Literal[True]) -> NDArray[Any]: ...
3831

3932

4033
@overload
41-
def to_dense(
42-
x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[False] = False
43-
) -> types.CupyArray: ...
34+
def to_dense(x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[False] = False) -> types.CupyArray: ...
4435
@overload
45-
def to_dense(
46-
x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[True]
47-
) -> NDArray[Any]: ...
36+
def to_dense(x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[True]) -> NDArray[Any]: ...
4837

4938

5039
def to_dense(
51-
x: CpuArray
52-
| GpuArray
53-
| DiskArray
54-
| types.CSDataset
55-
| types.DaskArray
56-
| types.sparray
57-
| types.spmatrix
58-
| types.CupySpMatrix,
40+
x: CpuArray | GpuArray | DiskArray | types.CSDataset | types.DaskArray | types.sparray | types.spmatrix | types.CupySpMatrix,
5941
/,
6042
*,
6143
to_cpu_memory: bool = False,

src/fast_array_utils/conv/_to_dense.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919
# fallback’s arg0 type has to include types of registered functions
2020
@singledispatch
2121
def to_dense_(
22-
x: CpuArray
23-
| GpuArray
24-
| DiskArray
25-
| types.DaskArray
26-
| types.sparray
27-
| types.spmatrix
28-
| types.CupySpMatrix,
22+
x: CpuArray | GpuArray | DiskArray | types.DaskArray | types.sparray | types.spmatrix | types.CupySpMatrix,
2923
/,
3024
*,
3125
to_cpu_memory: bool = False,
@@ -35,19 +29,15 @@ def to_dense_(
3529

3630

3731
@to_dense_.register(types.spmatrix | types.sparray) # type: ignore[call-overload,misc]
38-
def _to_dense_cs(
39-
x: types.spmatrix | types.sparray, /, *, to_cpu_memory: bool = False
40-
) -> NDArray[Any]:
32+
def _to_dense_cs(x: types.spmatrix | types.sparray, /, *, to_cpu_memory: bool = False) -> NDArray[Any]:
4133
from . import scipy
4234

4335
del to_cpu_memory # it already is
4436
return scipy.to_dense(x)
4537

4638

4739
@to_dense_.register(types.DaskArray)
48-
def _to_dense_dask(
49-
x: types.DaskArray, /, *, to_cpu_memory: bool = False
50-
) -> NDArray[Any] | types.DaskArray:
40+
def _to_dense_dask(x: types.DaskArray, /, *, to_cpu_memory: bool = False) -> NDArray[Any] | types.DaskArray:
5141
from . import to_dense
5242

5343
x = x.map_blocks(partial(to_dense, to_cpu_memory=to_cpu_memory))
@@ -66,8 +56,6 @@ def _to_dense_ooc(x: types.CSDataset, /, *, to_cpu_memory: bool = False) -> NDAr
6656

6757

6858
@to_dense_.register(types.CupyArray | types.CupySpMatrix) # type: ignore[call-overload,misc]
69-
def _to_dense_cupy(
70-
x: GpuArray, /, *, to_cpu_memory: bool = False
71-
) -> NDArray[Any] | types.CupyArray:
59+
def _to_dense_cupy(x: GpuArray, /, *, to_cpu_memory: bool = False) -> NDArray[Any] | types.CupyArray:
7260
x = x.toarray() if isinstance(x, types.CupySpMatrix) else x
7361
return x.get() if to_cpu_memory else x

src/fast_array_utils/conv/scipy/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ def to_dense(x: types.spmatrix | types.sparray, order: Literal["C", "F"] = "C")
4242
try:
4343
from ._to_dense import _to_dense_csc_numba, _to_dense_csr_numba
4444
except ImportError:
45-
warn(
46-
"numba is not installed; falling back to slow conversion", RuntimeWarning, stacklevel=2
47-
)
45+
warn("numba is not installed; falling back to slow conversion", RuntimeWarning, stacklevel=2)
4846
return x.toarray(order=order)
4947

5048
out = np.zeros(x.shape, dtype=x.dtype, order=order)

src/fast_array_utils/stats/__init__.py

+15-74
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828

2929
@overload
30-
def is_constant(
31-
x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None
32-
) -> bool: ...
30+
def is_constant(x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None) -> bool: ...
3331
@overload
3432
def is_constant(x: NDArray[Any] | types.CSBase, /, *, axis: Literal[0, 1]) -> NDArray[np.bool]: ...
3533
@overload
@@ -82,25 +80,13 @@ def is_constant(
8280
# TODO(flying-sheep): support CSDataset (TODO)
8381
# https://github.com/scverse/fast-array-utils/issues/52
8482
@overload
85-
def mean(
86-
x: CpuArray | GpuArray | DiskArray,
87-
/,
88-
*,
89-
axis: Literal[None] = None,
90-
dtype: DTypeLike | None = None,
91-
) -> np.number[Any]: ...
83+
def mean(x: CpuArray | GpuArray | DiskArray, /, *, axis: Literal[None] = None, dtype: DTypeLike | None = None) -> np.number[Any]: ...
9284
@overload
93-
def mean(
94-
x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
95-
) -> NDArray[np.number[Any]]: ...
85+
def mean(x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None) -> NDArray[np.number[Any]]: ...
9686
@overload
97-
def mean(
98-
x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
99-
) -> types.CupyArray: ...
87+
def mean(x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None) -> types.CupyArray: ...
10088
@overload
101-
def mean(
102-
x: types.DaskArray, /, *, axis: Literal[0, 1], dtype: ToDType[Any] | None = None
103-
) -> types.DaskArray: ...
89+
def mean(x: types.DaskArray, /, *, axis: Literal[0, 1], dtype: ToDType[Any] | None = None) -> types.DaskArray: ...
10490

10591

10692
def mean(
@@ -149,21 +135,13 @@ def mean(
149135

150136

151137
@overload
152-
def mean_var(
153-
x: CpuArray | GpuArray, /, *, axis: Literal[None] = None, correction: int = 0
154-
) -> tuple[np.float64, np.float64]: ...
138+
def mean_var(x: CpuArray | GpuArray, /, *, axis: Literal[None] = None, correction: int = 0) -> tuple[np.float64, np.float64]: ...
155139
@overload
156-
def mean_var(
157-
x: CpuArray, /, *, axis: Literal[0, 1], correction: int = 0
158-
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
140+
def mean_var(x: CpuArray, /, *, axis: Literal[0, 1], correction: int = 0) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
159141
@overload
160-
def mean_var(
161-
x: GpuArray, /, *, axis: Literal[0, 1], correction: int = 0
162-
) -> tuple[types.CupyArray, types.CupyArray]: ...
142+
def mean_var(x: GpuArray, /, *, axis: Literal[0, 1], correction: int = 0) -> tuple[types.CupyArray, types.CupyArray]: ...
163143
@overload
164-
def mean_var(
165-
x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, correction: int = 0
166-
) -> tuple[types.DaskArray, types.DaskArray]: ...
144+
def mean_var(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, correction: int = 0) -> tuple[types.DaskArray, types.DaskArray]: ...
167145

168146

169147
def mean_var(
@@ -226,58 +204,21 @@ def mean_var(
226204
# TODO(flying-sheep): support CSDataset (TODO)
227205
# https://github.com/scverse/fast-array-utils/issues/52
228206
@overload
229-
def sum(
230-
x: CpuArray | DiskArray,
231-
/,
232-
*,
233-
axis: None = None,
234-
dtype: DTypeLike | None = None,
235-
keep_cupy_as_array: bool = False,
236-
) -> np.number[Any]: ...
207+
def sum(x: CpuArray | DiskArray, /, *, axis: None = None, dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> np.number[Any]: ...
237208
@overload
238-
def sum(
239-
x: CpuArray | DiskArray,
240-
/,
241-
*,
242-
axis: Literal[0, 1],
243-
dtype: DTypeLike | None = None,
244-
keep_cupy_as_array: bool = False,
245-
) -> NDArray[Any]: ...
209+
def sum(x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> NDArray[Any]: ...
246210

247211

248212
@overload
249-
def sum(
250-
x: GpuArray,
251-
/,
252-
*,
253-
axis: None = None,
254-
dtype: DTypeLike | None = None,
255-
keep_cupy_as_array: Literal[False] = False,
256-
) -> np.number[Any]: ...
213+
def sum(x: GpuArray, /, *, axis: None = None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[False] = False) -> np.number[Any]: ...
257214
@overload
258-
def sum(
259-
x: GpuArray, /, *, axis: None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[True]
260-
) -> types.CupyArray: ...
215+
def sum(x: GpuArray, /, *, axis: None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[True]) -> types.CupyArray: ...
261216
@overload
262-
def sum(
263-
x: GpuArray,
264-
/,
265-
*,
266-
axis: Literal[0, 1],
267-
dtype: DTypeLike | None = None,
268-
keep_cupy_as_array: bool = False,
269-
) -> types.CupyArray: ...
217+
def sum(x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> types.CupyArray: ...
270218

271219

272220
@overload
273-
def sum(
274-
x: types.DaskArray,
275-
/,
276-
*,
277-
axis: Literal[0, 1, None] = None,
278-
dtype: DTypeLike | None = None,
279-
keep_cupy_as_array: bool = False,
280-
) -> types.DaskArray: ...
221+
def sum(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> types.DaskArray: ...
281222

282223

283224
def sum(

src/fast_array_utils/stats/_is_constant.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ def is_constant_(
3030

3131

3232
@is_constant_.register(np.ndarray | types.CupyArray) # type: ignore[call-overload,misc]
33-
def _is_constant_ndarray(
34-
a: NDArray[Any] | types.CupyArray, /, *, axis: Literal[0, 1, None] = None
35-
) -> bool | NDArray[np.bool] | types.CupyArray:
33+
def _is_constant_ndarray(a: NDArray[Any] | types.CupyArray, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool] | types.CupyArray:
3634
# Should eventually support nd, not now.
3735
match axis:
3836
case None:
@@ -49,9 +47,7 @@ def _is_constant_rows(a: NDArray[Any] | types.CupyArray) -> NDArray[np.bool] | t
4947

5048

5149
@is_constant_.register(types.CSBase) # type: ignore[call-overload,misc]
52-
def _is_constant_cs(
53-
a: types.CSBase, /, *, axis: Literal[0, 1, None] = None
54-
) -> bool | NDArray[np.bool]:
50+
def _is_constant_cs(a: types.CSBase, /, *, axis: Literal[0, 1, None] = None) -> bool | NDArray[np.bool]:
5551
from . import is_constant
5652

5753
if len(a.shape) == 1: # pragma: no cover
@@ -87,17 +83,13 @@ def _is_constant_cs_major(a: types.CSBase, shape: tuple[int, int]) -> NDArray[np
8783

8884

8985
@is_constant_.register(types.DaskArray)
90-
def _is_constant_dask(
91-
a: types.DaskArray, /, *, axis: Literal[0, 1, None] = None
92-
) -> types.DaskArray:
86+
def _is_constant_dask(a: types.DaskArray, /, *, axis: Literal[0, 1, None] = None) -> types.DaskArray:
9387
import dask.array as da
9488

9589
from . import is_constant
9690

9791
if axis is not None:
98-
return da.map_blocks(
99-
partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool)
100-
)
92+
return da.map_blocks(partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool))
10193

10294
rv = (
10395
(a == a[0, 0].compute()).all()

src/fast_array_utils/stats/_mean_var.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ def mean_var_(
4646
return mean_, var
4747

4848

49-
def _sparse_mean_var(
50-
mtx: types.CSBase, /, *, axis: Literal[0, 1]
51-
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
49+
def _sparse_mean_var(mtx: types.CSBase, /, *, axis: Literal[0, 1]) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
5250
"""Calculate means and variances for each row or column of a sparse matrix.
5351
5452
This code and internal functions are based on sklearns `sparsefuncs.mean_variance_axis`.

src/fast_array_utils/stats/_power.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ def _power(x: Array, n: int, /) -> Array:
3232

3333

3434
@_power.register(types.CSMatrix | types.CupyCSMatrix) # type: ignore[call-overload,misc]
35-
def _power_cs(
36-
x: types.CSMatrix | types.CupyCSMatrix, n: int, /
37-
) -> types.CSMatrix | types.CupyCSMatrix:
35+
def _power_cs(x: types.CSMatrix | types.CupyCSMatrix, n: int, /) -> types.CSMatrix | types.CupyCSMatrix:
3836
return x.power(n)
3937

4038

0 commit comments

Comments
 (0)