Skip to content

Widen line lengths for overload authoring #93

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 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 4 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
autodoc_default_options = {
"special-members": True,
# everything except __call__ really, to avoid having to write autosummary templates
"exclude-members": (
"__setattr__,__delattr__,__repr__,__eq__,__or__,__ror__,__hash__,__weakref__,__init__,__new__"
),
"exclude-members": "__setattr__,__delattr__,__repr__,__eq__,__or__,__ror__,__hash__,__weakref__,__init__,__new__",
}
napoleon_google_docstring = False
napoleon_numpy_docstring = True
Expand Down Expand Up @@ -115,27 +113,21 @@ def find_type_alias(name: str) -> tuple[str, str] | tuple[None, None]:
return None, None


def resolve_type_aliases(
app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement
) -> reference | None:
def resolve_type_aliases(app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement) -> reference | None:
"""Resolve :class: references to our type aliases as :attr: instead."""
if (node["refdomain"], node["reftype"]) != ("py", "class"):
return None
typ, target = find_type_alias(node["reftarget"])
if typ is None or target is None:
return None
if target.startswith("fast_array_utils."):
ref = env.get_domain("py").resolve_xref(
env, node["refdoc"], app.builder, typ, target, node, contnode
)
ref = env.get_domain("py").resolve_xref(env, node["refdoc"], app.builder, typ, target, node, contnode)
else:
from sphinx.ext.intersphinx import resolve_reference_any_inventory

node["reftype"] = typ
node["reftarget"] = target
ref = resolve_reference_any_inventory(
env=env, honor_disabled_refs=False, node=node, contnode=contnode
)
ref = resolve_reference_any_inventory(env=env, honor_disabled_refs=False, node=node, contnode=contnode)
if ref is None:
msg = f"Could not resolve {typ} {target} (from {node['reftarget']})"
raise AssertionError(msg)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ python = [ "3.13", "3.12", "3.11" ]
extras = [ "full", "default", "min" ]

[tool.ruff]
line-length = 100
line-length = 160
namespace-packages = [ "src/testing" ]
format.preview = true
format.docstring-code-format = true
Expand Down
16 changes: 4 additions & 12 deletions src/fast_array_utils/_plugins/numba_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,9 @@ def __init__(self, dmm: DataModelManager, fe_type: CSType) -> None:
sparse.csr_array,
sparse.csc_array,
]
TYPES: Sequence[type[CSType]] = [
type(f"{cls.__name__}Type", (CSType,), {"cls": cls, "name": cls.__name__}) for cls in CLASSES
]
TYPEOF_FUNCS: Mapping[type[CSBase], Callable[[CSBase, _TypeofContext], CSType]] = {
typ.cls: make_typeof_fn(typ) for typ in TYPES
}
MODELS: Mapping[type[CSType], type[CSModel]] = {
typ: type(f"{typ.cls.__name__}Model", (CSModel,), {}) for typ in TYPES
}
TYPES: Sequence[type[CSType]] = [type(f"{cls.__name__}Type", (CSType,), {"cls": cls, "name": cls.__name__}) for cls in CLASSES]
TYPEOF_FUNCS: Mapping[type[CSBase], Callable[[CSBase, _TypeofContext], CSType]] = {typ.cls: make_typeof_fn(typ) for typ in TYPES}
MODELS: Mapping[type[CSType], type[CSModel]] = {typ: type(f"{typ.cls.__name__}Model", (CSModel,), {}) for typ in TYPES}


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

# nopython code:
def copy(inst: CSType) -> CSType: # pragma: no cover
return _sparse_copy(
inst, inst.data.copy(), inst.indices.copy(), inst.indptr.copy(), inst.shape
) # type: ignore[return-value]
return _sparse_copy(inst, inst.data.copy(), inst.indices.copy(), inst.indptr.copy(), inst.shape) # type: ignore[return-value]

return copy

Expand Down
28 changes: 5 additions & 23 deletions src/fast_array_utils/conv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,23 @@


@overload
def to_dense(
x: CpuArray | DiskArray | types.sparray | types.spmatrix | types.CSDataset,
/,
*,
to_cpu_memory: bool = False,
) -> NDArray[Any]: ...
def to_dense(x: CpuArray | DiskArray | types.sparray | types.spmatrix | types.CSDataset, /, *, to_cpu_memory: bool = False) -> NDArray[Any]: ...


@overload
def to_dense(
x: types.DaskArray, /, *, to_cpu_memory: Literal[False] = False
) -> types.DaskArray: ...
def to_dense(x: types.DaskArray, /, *, to_cpu_memory: Literal[False] = False) -> types.DaskArray: ...
@overload
def to_dense(x: types.DaskArray, /, *, to_cpu_memory: Literal[True]) -> NDArray[Any]: ...


@overload
def to_dense(
x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[False] = False
) -> types.CupyArray: ...
def to_dense(x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[False] = False) -> types.CupyArray: ...
@overload
def to_dense(
x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[True]
) -> NDArray[Any]: ...
def to_dense(x: GpuArray | types.CupySpMatrix, /, *, to_cpu_memory: Literal[True]) -> NDArray[Any]: ...


def to_dense(
x: CpuArray
| GpuArray
| DiskArray
| types.CSDataset
| types.DaskArray
| types.sparray
| types.spmatrix
| types.CupySpMatrix,
x: CpuArray | GpuArray | DiskArray | types.CSDataset | types.DaskArray | types.sparray | types.spmatrix | types.CupySpMatrix,
/,
*,
to_cpu_memory: bool = False,
Expand Down
20 changes: 4 additions & 16 deletions src/fast_array_utils/conv/_to_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
# fallback’s arg0 type has to include types of registered functions
@singledispatch
def to_dense_(
x: CpuArray
| GpuArray
| DiskArray
| types.DaskArray
| types.sparray
| types.spmatrix
| types.CupySpMatrix,
x: CpuArray | GpuArray | DiskArray | types.DaskArray | types.sparray | types.spmatrix | types.CupySpMatrix,
/,
*,
to_cpu_memory: bool = False,
Expand All @@ -35,19 +29,15 @@ def to_dense_(


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

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


@to_dense_.register(types.DaskArray)
def _to_dense_dask(
x: types.DaskArray, /, *, to_cpu_memory: bool = False
) -> NDArray[Any] | types.DaskArray:
def _to_dense_dask(x: types.DaskArray, /, *, to_cpu_memory: bool = False) -> NDArray[Any] | types.DaskArray:
from . import to_dense

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


@to_dense_.register(types.CupyArray | types.CupySpMatrix) # type: ignore[call-overload,misc]
def _to_dense_cupy(
x: GpuArray, /, *, to_cpu_memory: bool = False
) -> NDArray[Any] | types.CupyArray:
def _to_dense_cupy(x: GpuArray, /, *, to_cpu_memory: bool = False) -> NDArray[Any] | types.CupyArray:
x = x.toarray() if isinstance(x, types.CupySpMatrix) else x
return x.get() if to_cpu_memory else x
4 changes: 1 addition & 3 deletions src/fast_array_utils/conv/scipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def to_dense(x: types.spmatrix | types.sparray, order: Literal["C", "F"] = "C")
try:
from ._to_dense import _to_dense_csc_numba, _to_dense_csr_numba
except ImportError:
warn(
"numba is not installed; falling back to slow conversion", RuntimeWarning, stacklevel=2
)
warn("numba is not installed; falling back to slow conversion", RuntimeWarning, stacklevel=2)
return x.toarray(order=order)

out = np.zeros(x.shape, dtype=x.dtype, order=order)
Expand Down
89 changes: 15 additions & 74 deletions src/fast_array_utils/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@


@overload
def is_constant(
x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None
) -> bool: ...
def is_constant(x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None) -> bool: ...
@overload
def is_constant(x: NDArray[Any] | types.CSBase, /, *, axis: Literal[0, 1]) -> NDArray[np.bool]: ...
@overload
Expand Down Expand Up @@ -82,25 +80,13 @@ def is_constant(
# TODO(flying-sheep): support CSDataset (TODO)
# https://github.com/scverse/fast-array-utils/issues/52
@overload
def mean(
x: CpuArray | GpuArray | DiskArray,
/,
*,
axis: Literal[None] = None,
dtype: DTypeLike | None = None,
) -> np.number[Any]: ...
def mean(x: CpuArray | GpuArray | DiskArray, /, *, axis: Literal[None] = None, dtype: DTypeLike | None = None) -> np.number[Any]: ...
@overload
def mean(
x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> NDArray[np.number[Any]]: ...
def mean(x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None) -> NDArray[np.number[Any]]: ...
@overload
def mean(
x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> types.CupyArray: ...
def mean(x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None) -> types.CupyArray: ...
@overload
def mean(
x: types.DaskArray, /, *, axis: Literal[0, 1], dtype: ToDType[Any] | None = None
) -> types.DaskArray: ...
def mean(x: types.DaskArray, /, *, axis: Literal[0, 1], dtype: ToDType[Any] | None = None) -> types.DaskArray: ...


def mean(
Expand Down Expand Up @@ -149,21 +135,13 @@ def mean(


@overload
def mean_var(
x: CpuArray | GpuArray, /, *, axis: Literal[None] = None, correction: int = 0
) -> tuple[np.float64, np.float64]: ...
def mean_var(x: CpuArray | GpuArray, /, *, axis: Literal[None] = None, correction: int = 0) -> tuple[np.float64, np.float64]: ...
@overload
def mean_var(
x: CpuArray, /, *, axis: Literal[0, 1], correction: int = 0
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
def mean_var(x: CpuArray, /, *, axis: Literal[0, 1], correction: int = 0) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
@overload
def mean_var(
x: GpuArray, /, *, axis: Literal[0, 1], correction: int = 0
) -> tuple[types.CupyArray, types.CupyArray]: ...
def mean_var(x: GpuArray, /, *, axis: Literal[0, 1], correction: int = 0) -> tuple[types.CupyArray, types.CupyArray]: ...
@overload
def mean_var(
x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, correction: int = 0
) -> tuple[types.DaskArray, types.DaskArray]: ...
def mean_var(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, correction: int = 0) -> tuple[types.DaskArray, types.DaskArray]: ...


def mean_var(
Expand Down Expand Up @@ -226,58 +204,21 @@ def mean_var(
# TODO(flying-sheep): support CSDataset (TODO)
# https://github.com/scverse/fast-array-utils/issues/52
@overload
def sum(
x: CpuArray | DiskArray,
/,
*,
axis: None = None,
dtype: DTypeLike | None = None,
keep_cupy_as_array: bool = False,
) -> np.number[Any]: ...
def sum(x: CpuArray | DiskArray, /, *, axis: None = None, dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> np.number[Any]: ...
@overload
def sum(
x: CpuArray | DiskArray,
/,
*,
axis: Literal[0, 1],
dtype: DTypeLike | None = None,
keep_cupy_as_array: bool = False,
) -> NDArray[Any]: ...
def sum(x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> NDArray[Any]: ...


@overload
def sum(
x: GpuArray,
/,
*,
axis: None = None,
dtype: DTypeLike | None = None,
keep_cupy_as_array: Literal[False] = False,
) -> np.number[Any]: ...
def sum(x: GpuArray, /, *, axis: None = None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[False] = False) -> np.number[Any]: ...
@overload
def sum(
x: GpuArray, /, *, axis: None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[True]
) -> types.CupyArray: ...
def sum(x: GpuArray, /, *, axis: None, dtype: DTypeLike | None = None, keep_cupy_as_array: Literal[True]) -> types.CupyArray: ...
@overload
def sum(
x: GpuArray,
/,
*,
axis: Literal[0, 1],
dtype: DTypeLike | None = None,
keep_cupy_as_array: bool = False,
) -> types.CupyArray: ...
def sum(x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> types.CupyArray: ...


@overload
def sum(
x: types.DaskArray,
/,
*,
axis: Literal[0, 1, None] = None,
dtype: DTypeLike | None = None,
keep_cupy_as_array: bool = False,
) -> types.DaskArray: ...
def sum(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, dtype: DTypeLike | None = None, keep_cupy_as_array: bool = False) -> types.DaskArray: ...


def sum(
Expand Down
16 changes: 4 additions & 12 deletions src/fast_array_utils/stats/_is_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def is_constant_(


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


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

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


@is_constant_.register(types.DaskArray)
def _is_constant_dask(
a: types.DaskArray, /, *, axis: Literal[0, 1, None] = None
) -> types.DaskArray:
def _is_constant_dask(a: types.DaskArray, /, *, axis: Literal[0, 1, None] = None) -> types.DaskArray:
import dask.array as da

from . import is_constant

if axis is not None:
return da.map_blocks(
partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool)
)
return da.map_blocks(partial(is_constant, axis=axis), a, drop_axis=axis, meta=np.array([], dtype=np.bool))

rv = (
(a == a[0, 0].compute()).all()
Expand Down
4 changes: 1 addition & 3 deletions src/fast_array_utils/stats/_mean_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def mean_var_(
return mean_, var


def _sparse_mean_var(
mtx: types.CSBase, /, *, axis: Literal[0, 1]
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
def _sparse_mean_var(mtx: types.CSBase, /, *, axis: Literal[0, 1]) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
"""Calculate means and variances for each row or column of a sparse matrix.

This code and internal functions are based on sklearns `sparsefuncs.mean_variance_axis`.
Expand Down
4 changes: 1 addition & 3 deletions src/fast_array_utils/stats/_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def _power(x: Array, n: int, /) -> Array:


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


Expand Down
Loading
Loading