Skip to content

Commit

Permalink
chore: fixes for pylint and mypy
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 9, 2023
1 parent 30b0e82 commit dcc57a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ messages_control.disable = [
"missing-class-docstring",
"missing-function-docstring",
"missing-module-docstring",
"no-member", # C extensions mess with this
"c-extension-no-member", # C extensions mess with this
"protected-access",
"too-few-public-methods",
"too-many-arguments",
Expand Down
2 changes: 1 addition & 1 deletion src/boost_histogram/_core/axis/transform.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def _sq_fn(arg0: float) -> float: ...

class _BaseTransform:
def forward(self, arg0: float) -> float: ...
def reverse(self, arg0: float) -> float: ...
def inverse(self, arg0: float) -> float: ...
def __repr__(self) -> str: ...
def __copy__(self: T) -> T: ...
def __deepcopy__(self: T, memo: Any) -> T: ...
Expand Down
32 changes: 17 additions & 15 deletions src/boost_histogram/_internal/axis_transform.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import copy
from typing import Any, TypeVar
from typing import Any, ClassVar, TypeVar

import boost_histogram

Expand All @@ -15,7 +15,7 @@
class AxisTransform:
__slots__ = ("_this",)
_family: object
_this: Any
_this: ca.transform._BaseTransform

def __init_subclass__(cls, *, family: object) -> None:
super().__init_subclass__()
Expand All @@ -39,41 +39,40 @@ def __repr__(self) -> str:
return f"{self.__class__.__name__}() # Missing _this, broken class"

def _produce(self, bins: int, start: float, stop: float) -> Any:
# Note: this is an ABC; _type must be defined on children
# These can be fixed later with a Protocol
return self.__class__._type(bins, start, stop) # type: ignore[attr-defined]
raise NotImplementedError()

def __init__(self) -> None:
"Create a new transform instance"
# Note: this comes from family
(cpp_class,) = self._types # type: ignore[attr-defined]
self._this = cpp_class()
raise NotImplementedError()

def forward(self, value: float) -> float:
"Compute the forward transform"
return self._this.forward(value) # type: ignore[no-any-return]
return self._this.forward(value)

def inverse(self, value: float) -> float:
"Compute the inverse transform"
return self._this.inverse(value) # type: ignore[no-any-return]
return self._this.inverse(value)


@set_module("boost_histogram.axis.transform")
@register({ca.transform.pow})
class Pow(AxisTransform, family=boost_histogram):
__slots__ = ()
_type = ca.regular_pow
_this: ca.transform.pow

# Note: this comes from family
_types: ClassVar[set[type[ca.transform.pow]]]

def __init__(self, power: float): # pylint: disable=super-init-not-called
"Create a new transform instance"
# Note: this comes from family
(cpp_class,) = self._types # type: ignore[attr-defined]
(cpp_class,) = self._types
self._this = cpp_class(power)

@property
def power(self) -> float:
"The power of the transform"
return self._this.power # type: ignore[no-any-return]
return self._this.power

# This one does need to be a normal method
def _produce(self, bins: int, start: float, stop: float) -> Any:
Expand All @@ -85,6 +84,10 @@ def _produce(self, bins: int, start: float, stop: float) -> Any:
class Function(AxisTransform, family=boost_histogram):
__slots__ = ()
_type = ca.regular_trans
_this: ca.transform.func_transform

# Note: this comes from family
_types: ClassVar[set[type[ca.transform.func_transform]]]

def __init__( # pylint: disable=super-init-not-called
self, forward: Any, inverse: Any, *, convert: Any = None, name: str = ""
Expand Down Expand Up @@ -135,8 +138,7 @@ def log(x):
"""

# Note: this comes from family
(cpp_class,) = self._types # type: ignore[attr-defined]
(cpp_class,) = self._types
self._this = cpp_class(forward, inverse, convert, name)

# This one does need to be a normal method
Expand Down
1 change: 1 addition & 0 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Histogram:

axes: AxesTuple
_hist: CppHistogram
_variance_known: bool

def __init_subclass__(cls, *, family: object | None = None) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions src/boost_histogram/_internal/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import boost_histogram

from .._core import accumulators
from .._core import storage as store
from .._core import accumulators # pylint: disable=no-name-in-module
from .._core import storage as store # pylint: disable=no-name-in-module
from .utils import set_module


Expand Down
17 changes: 11 additions & 6 deletions src/boost_histogram/_internal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
import numpy as np

from ..accumulators import Mean, WeightedMean, WeightedSum
from .typing import ArrayLike, StrIndex, Ufunc
from .typing import ArrayLike, Literal, StrIndex, Ufunc

UFMethod = Literal["__call__", "reduce", "reduceat", "accumulate", "outer", "inner"]


class View(np.ndarray): # type: ignore[type-arg]
__slots__ = ()
_FIELDS: ClassVar[tuple[str, ...]]
_PARENT: type[WeightedSum] | type[WeightedMean] | type[Mean]

def __getitem__(self, ind: StrIndex) -> np.typing.NDArray[Any]: # type: ignore[override]
sliced = super().__getitem__(ind) # type: ignore[index]

# If the shape is empty, return the parent type
if not sliced.shape:
return self._PARENT._make(*sliced) # type: ignore[attr-defined, no-any-return]
return self._PARENT._make(*sliced) # type: ignore[no-any-return]

# If the dtype has changed, return a normal array (no longer a record)
if sliced.dtype != self.dtype:
Expand Down Expand Up @@ -47,7 +50,7 @@ def __setitem__(self, ind: StrIndex, value: ArrayLike) -> None:
msg = "Needs matching ndarray or n+1 dim array"
if array.ndim == current_ndim + 1:
if len(self._FIELDS) == array.shape[-1]:
self.__setitem__(ind, self._PARENT._array(*np.moveaxis(array, -1, 0))) # type: ignore[attr-defined]
self.__setitem__(ind, self._PARENT._array(*np.moveaxis(array, -1, 0)))
return
msg += f", final dimension should be {len(self._FIELDS)} for this storage, got {array.shape[-1]} instead"
raise ValueError(msg)
Expand Down Expand Up @@ -109,7 +112,7 @@ class WeightedSumView(View):

# Could be implemented on master View
def __array_ufunc__(
self, ufunc: Ufunc, method: str, *inputs: Any, **kwargs: Any
self, ufunc: Ufunc, method: UFMethod, *inputs: Any, **kwargs: Any
) -> np.typing.NDArray[Any]:
# Avoid infinite recursion
raw_inputs = [np.asarray(x) for x in inputs]
Expand Down Expand Up @@ -155,7 +158,8 @@ def __array_ufunc__(
return result.view(self.__class__) # type: ignore[no-any-return]

# If unsupported, just pass through (will return not implemented)
return super().__array_ufunc__(ufunc, method, *raw_inputs, **kwargs) # type: ignore[no-any-return, arg-type]
# pylint: disable-next=no-member
return super().__array_ufunc__(ufunc, method, *raw_inputs, **kwargs) # type: ignore[no-any-return]

# View with normal value or array
if ufunc in {np.add, np.subtract}:
Expand Down Expand Up @@ -234,7 +238,8 @@ def __array_ufunc__(
return result.view(self.__class__) # type: ignore[no-any-return]

# If unsupported, just pass through (will return NotImplemented or things like == will work but not return subclasses)
return super().__array_ufunc__(ufunc, method, *raw_inputs, **kwargs) # type: ignore[no-any-return, arg-type]
# pylint: disable-next=no-member
return super().__array_ufunc__(ufunc, method, *raw_inputs, **kwargs) # type: ignore[no-any-return]


@fields(
Expand Down
2 changes: 1 addition & 1 deletion src/boost_histogram/accumulators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from ._core.accumulators import ( # pylint: disable=import-error
from ._core.accumulators import ( # pylint: disable=import-error,no-name-in-module
Mean,
Sum,
WeightedMean,
Expand Down

0 comments on commit dcc57a4

Please sign in to comment.