Skip to content

Commit 480163b

Browse files
committed
chore(types): update to NumPy 1.21
1 parent 92c37be commit 480163b

File tree

8 files changed

+87
-62
lines changed

8 files changed

+87
-62
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ repos:
7575
- id: mypy
7676
files: src
7777
args: []
78-
additional_dependencies: [numpy==1.20.*, uhi, types-dataclasses]
78+
additional_dependencies: [numpy==1.21.*, uhi, types-dataclasses]
7979

8080
- repo: https://github.com/mgedmin/check-manifest
8181
rev: "0.46"

src/boost_histogram/_core/axis/__init__.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class _BaseAxis:
3434
@property
3535
def extent(self) -> int: ...
3636
@property
37-
def edges(self) -> np.ndarray: ...
37+
def edges(self) -> "np.typing.NDArray[Any]": ...
3838
@property
39-
def centers(self) -> np.ndarray: ...
39+
def centers(self) -> "np.typing.NDArray[Any]": ...
4040
@property
41-
def widths(self) -> np.ndarray: ...
42-
def index(self, arg0: ArrayLike) -> int | np.ndarray: ...
43-
def value(self, arg0: ArrayLike) -> float | np.ndarray: ...
41+
def widths(self) -> "np.typing.NDArray[Any]": ...
42+
def index(self, arg0: ArrayLike) -> int | "np.typing.NDArray[Any]": ...
43+
def value(self, arg0: ArrayLike) -> float | "np.typing.NDArray[Any]": ...
4444

4545
class _BaseRegular(_BaseAxis):
4646
def __init__(self, bins: int, start: float, stop: float) -> None: ...

src/boost_histogram/_core/hist.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class _BaseHistogram:
2222
def __copy__(self: T) -> T: ...
2323
def __deepcopy__(self: T, memo: Any) -> T: ...
2424
def __iadd__(self: T, other: _BaseHistogram) -> T: ...
25-
def to_numpy(self, flow: bool = ...) -> Tuple[np.ndarray, ...]: ...
26-
def view(self, flow: bool = ...) -> np.ndarray: ...
25+
def to_numpy(self, flow: bool = ...) -> Tuple["np.typing.NDArray[Any]", ...]: ...
26+
def view(self, flow: bool = ...) -> "np.typing.NDArray[Any]": ...
2727
def axis(self, i: int = ...) -> axis._BaseAxis: ...
2828
def fill(self, *args: ArrayLike, weight: ArrayLike | None = ...) -> None: ...
2929
def empty(self, flow: bool = ...) -> bool: ...

src/boost_histogram/_internal/axestuple.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class ArrayTuple(tuple): # type: ignore
1717

1818
def __getattr__(self, name: str) -> Any:
1919
if name in self._REDUCTIONS:
20-
return partial(getattr(np, name), np.broadcast_arrays(*self))
20+
return partial(getattr(np, name), np.broadcast_arrays(*self)) # type: ignore
2121
else:
2222
return self.__class__(getattr(a, name) for a in self)
2323

2424
def __dir__(self) -> List[str]:
25-
names = dir(self.__class__) + dir(np.ndarray)
25+
names = dir(self.__class__) + dir("np.typing.NDArray[Any]")
2626
return sorted(n for n in names if not n.startswith("_"))
2727

2828
def __call__(self, *args: Any, **kwargs: Any) -> Any:
@@ -34,7 +34,7 @@ def broadcast(self: A) -> A:
3434
Use this method to broadcast them out into their full memory
3535
representation.
3636
"""
37-
return self.__class__(np.broadcast_arrays(*self))
37+
return self.__class__(np.broadcast_arrays(*self)) # type: ignore
3838

3939

4040
B = TypeVar("B", bound="AxesTuple")
@@ -56,17 +56,17 @@ def extent(self) -> Tuple[int, ...]:
5656
@property
5757
def centers(self) -> ArrayTuple:
5858
gen = (s.centers for s in self)
59-
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS))
59+
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS)) # type: ignore
6060

6161
@property
6262
def edges(self) -> ArrayTuple:
6363
gen = (s.edges for s in self)
64-
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS))
64+
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS)) # type: ignore
6565

6666
@property
6767
def widths(self) -> ArrayTuple:
6868
gen = (s.widths for s in self)
69-
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS))
69+
return ArrayTuple(np.meshgrid(*gen, **self._MGRIDOPTS)) # type: ignore
7070

7171
def value(self, *indexes: float) -> Tuple[float, ...]:
7272
if len(indexes) != len(self):

src/boost_histogram/_internal/axis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,18 @@ def __getitem__(self, i: AxCallOrInt) -> Union[int, str, Tuple[float, float]]:
248248
return self.bin(i)
249249

250250
@property
251-
def edges(self) -> np.ndarray:
251+
def edges(self) -> "np.typing.NDArray[Any]":
252252
return self._ax.edges # type: ignore
253253

254254
@property
255-
def centers(self) -> np.ndarray:
255+
def centers(self) -> "np.typing.NDArray[Any]":
256256
"""
257257
An array of bin centers.
258258
"""
259259
return self._ax.centers # type: ignore
260260

261261
@property
262-
def widths(self) -> np.ndarray:
262+
def widths(self) -> "np.typing.NDArray[Any]":
263263
"""
264264
An array of bin widths.
265265
"""

src/boost_histogram/_internal/hist.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
T = TypeVar("T")
6666

6767

68-
def _fill_cast(value: T, *, inner: bool = False) -> Union[T, np.ndarray, Tuple[T, ...]]:
68+
def _fill_cast(
69+
value: T, *, inner: bool = False
70+
) -> Union[T, "np.typing.NDArray[Any]", Tuple[T, ...]]:
6971
"""
7072
Convert to NumPy arrays. Some buffer objects do not get converted by forcecast.
7173
If not called by itself (inner=False), then will work through one level of tuple/list.
@@ -297,13 +299,13 @@ def ndim(self) -> int:
297299

298300
def view(
299301
self, flow: bool = False
300-
) -> Union[np.ndarray, WeightedSumView, WeightedMeanView, MeanView]:
302+
) -> Union["np.typing.NDArray[Any]", WeightedSumView, WeightedMeanView, MeanView]:
301303
"""
302304
Return a view into the data, optionally with overflow turned on.
303305
"""
304306
return _to_view(self._hist.view(flow))
305307

306-
def __array__(self) -> np.ndarray:
308+
def __array__(self) -> "np.typing.NDArray[Any]":
307309
return self.view(False)
308310

309311
def __eq__(self, other: Any) -> bool:
@@ -312,11 +314,15 @@ def __eq__(self, other: Any) -> bool:
312314
def __ne__(self, other: Any) -> bool:
313315
return (not hasattr(other, "_hist")) or self._hist != other._hist
314316

315-
def __add__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
317+
def __add__(
318+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
319+
) -> H:
316320
result = self.copy(deep=False)
317321
return result.__iadd__(other)
318322

319-
def __iadd__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
323+
def __iadd__(
324+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
325+
) -> H:
320326
if isinstance(other, (int, float)) and other == 0:
321327
return self
322328
self._compute_inplace_op("__iadd__", other)
@@ -326,36 +332,52 @@ def __iadd__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
326332

327333
return self
328334

329-
def __radd__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
335+
def __radd__(
336+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
337+
) -> H:
330338
return self + other
331339

332340
# If these fail, the underlying object throws the correct error
333-
def __mul__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
341+
def __mul__(
342+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
343+
) -> H:
334344
result = self.copy(deep=False)
335345
return result._compute_inplace_op("__imul__", other)
336346

337-
def __rmul__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
347+
def __rmul__(
348+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
349+
) -> H:
338350
return self * other
339351

340-
def __truediv__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
352+
def __truediv__(
353+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
354+
) -> H:
341355
result = self.copy(deep=False)
342356
return result._compute_inplace_op("__itruediv__", other)
343357

344-
def __div__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
358+
def __div__(
359+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
360+
) -> H:
345361
result = self.copy(deep=False)
346362
return result._compute_inplace_op("__idiv__", other)
347363

348-
def __idiv__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
364+
def __idiv__(
365+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
366+
) -> H:
349367
return self._compute_inplace_op("__idiv__", other)
350368

351-
def __itruediv__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
369+
def __itruediv__(
370+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
371+
) -> H:
352372
return self._compute_inplace_op("__itruediv__", other)
353373

354-
def __imul__(self: H, other: Union["Histogram", np.ndarray, float]) -> H:
374+
def __imul__(
375+
self: H, other: Union["Histogram", "np.typing.NDArray[Any]", float]
376+
) -> H:
355377
return self._compute_inplace_op("__imul__", other)
356378

357379
def _compute_inplace_op(
358-
self: H, name: str, other: Union["Histogram", np.ndarray, float]
380+
self: H, name: str, other: Union["Histogram", "np.typing.NDArray[Any]", float]
359381
) -> H:
360382
# Also takes CppHistogram, but that confuses mypy because it's hard to pick out
361383
if isinstance(other, Histogram):
@@ -442,26 +464,26 @@ def fill(
442464
}:
443465
raise RuntimeError("Mean histograms do not support threaded filling")
444466

445-
data = [np.array_split(a, threads) for a in args_ars]
467+
data = [np.array_split(a, threads) for a in args_ars] # type: ignore
446468

447469
if weight is None or np.isscalar(weight):
448470
assert threads is not None
449471
weights = [weight_ars] * threads
450472
else:
451-
weights = np.array_split(weight_ars, threads)
473+
weights = np.array_split(weight_ars, threads) # type: ignore
452474

453475
if sample_ars is None or np.isscalar(sample_ars):
454476
assert threads is not None
455477
samples = [sample_ars] * threads
456478
else:
457-
samples = np.array_split(sample_ars, threads)
479+
samples = np.array_split(sample_ars, threads) # type: ignore
458480

459481
if self._hist._storage_type is _core.storage.atomic_int64:
460482

461483
def fun(
462484
weight: Optional[ArrayLike],
463485
sample: Optional[ArrayLike],
464-
*args: np.ndarray,
486+
*args: "np.typing.NDArray[Any]",
465487
) -> None:
466488
self._hist.fill(*args, weight=weight, sample=sample)
467489

@@ -471,7 +493,7 @@ def fun(
471493
def fun(
472494
weight: Optional[ArrayLike],
473495
sample: Optional[ArrayLike],
474-
*args: np.ndarray,
496+
*args: "np.typing.NDArray[Any]",
475497
) -> None:
476498
local_hist = self._hist.__copy__()
477499
local_hist.reset()
@@ -646,7 +668,10 @@ def _compute_commonindex(
646668

647669
def to_numpy(
648670
self, flow: bool = False, *, dd: bool = False, view: bool = False
649-
) -> Union[Tuple[np.ndarray, ...], Tuple[np.ndarray, Tuple[np.ndarray, ...]]]:
671+
) -> Union[
672+
Tuple["np.typing.NDArray[Any]", ...],
673+
Tuple["np.typing.NDArray[Any]", Tuple["np.typing.NDArray[Any]", ...]],
674+
]:
650675
"""
651676
Convert to a NumPy style tuple of return arrays. Edges are converted to
652677
match NumPy standards, with upper edge inclusive, unlike
@@ -887,7 +912,7 @@ def __setitem__(
887912
if (
888913
value.ndim > 0
889914
and len(view.dtype) > 0 # type: ignore
890-
and len(value.dtype) == 0 # type: ignore
915+
and len(value.dtype) == 0
891916
and len(view.dtype) == value.shape[-1] # type: ignore
892917
):
893918
value_shape = value.shape[:-1]
@@ -984,7 +1009,7 @@ def kind(self) -> Kind:
9841009
else:
9851010
return Kind.COUNT
9861011

987-
def values(self, flow: bool = False) -> np.ndarray:
1012+
def values(self, flow: bool = False) -> "np.typing.NDArray[Any]":
9881013
"""
9891014
Returns the accumulated values. The counts for simple histograms, the
9901015
sum of weights for weighted histograms, the mean for profiles, etc.
@@ -995,7 +1020,7 @@ def values(self, flow: bool = False) -> np.ndarray:
9951020
:param flow: Enable flow bins. Not part of PlottableHistogram, but
9961021
included for consistency with other methods and flexibility.
9971022
998-
:return: np.ndarray[np.float64]
1023+
:return: "np.typing.NDArray[Any]"[np.float64]
9991024
"""
10001025

10011026
view = self.view(flow)
@@ -1005,7 +1030,7 @@ def values(self, flow: bool = False) -> np.ndarray:
10051030
else:
10061031
return view.value # type: ignore
10071032

1008-
def variances(self, flow: bool = False) -> Optional[np.ndarray]:
1033+
def variances(self, flow: bool = False) -> Optional["np.typing.NDArray[Any]"]:
10091034
"""
10101035
Returns the estimated variance of the accumulated values. The sum of squared
10111036
weights for weighted histograms, the variance of samples for profiles, etc.
@@ -1026,7 +1051,7 @@ def variances(self, flow: bool = False) -> Optional[np.ndarray]:
10261051
:param flow: Enable flow bins. Not part of PlottableHistogram, but
10271052
included for consistency with other methods and flexibility.
10281053
1029-
:return: np.ndarray[np.float64]
1054+
:return: "np.typing.NDArray[Any]"[np.float64]
10301055
"""
10311056

10321057
view = self.view(flow)
@@ -1053,7 +1078,7 @@ def variances(self, flow: bool = False) -> Optional[np.ndarray]:
10531078
else:
10541079
return view.variance # type: ignore
10551080

1056-
def counts(self, flow: bool = False) -> np.ndarray:
1081+
def counts(self, flow: bool = False) -> "np.typing.NDArray[Any]":
10571082
"""
10581083
Returns the number of entries in each bin for an unweighted
10591084
histogram or profile and an effective number of entries (defined below)
@@ -1073,7 +1098,7 @@ def counts(self, flow: bool = False) -> np.ndarray:
10731098
The larger the spread in weights, the smaller it is, but it is always 0
10741099
if filled 0 times, and 1 if filled once, and more than 1 otherwise.
10751100
1076-
:return: np.ndarray[np.float64]
1101+
:return: "np.typing.NDArray[Any]"[np.float64]
10771102
"""
10781103

10791104
view = self.view(flow)

0 commit comments

Comments
 (0)