Skip to content

Commit 23d886f

Browse files
committed
chore: simplify and touch up typing
1 parent 41515e6 commit 23d886f

File tree

1 file changed

+16
-12
lines changed
  • src/boost_histogram/_internal

1 file changed

+16
-12
lines changed

src/boost_histogram/_internal/hist.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
CppAxis = NewType("CppAxis", object)
5858

5959
SimpleIndexing = Union[SupportsIndex, slice]
60-
InnerIndexing = Union[SimpleIndexing, Callable[[Axis], int], "ellipsis"]
60+
InnerIndexing = Union[SimpleIndexing, Callable[[Axis], int]]
6161
FullInnerIndexing = Union[InnerIndexing, List[InnerIndexing]]
6262
IndexingWithMapping = Union[FullInnerIndexing, Mapping[int, FullInnerIndexing]]
63-
IndexingExpr = Union[IndexingWithMapping, Tuple[IndexingWithMapping, ...]]
63+
IndexingExpr = Union[IndexingWithMapping, Tuple[IndexingWithMapping, ...], "ellipsis"]
6464

6565
T = TypeVar("T")
6666

@@ -621,19 +621,23 @@ def _compute_uhi_index(self, index: InnerIndexing, axis: int) -> SimpleIndexing:
621621
"""
622622
# Support sum and rebin directly
623623
if index is sum or hasattr(index, "factor"): # type: ignore[comparison-overlap]
624-
index = slice(None, None, index)
624+
return slice(None, None, index)
625625

626626
# General locators
627627
# Note that MyPy doesn't like these very much - the fix
628628
# will be to properly set input types
629-
elif callable(index):
630-
index = index(self.axes[axis])
631-
elif isinstance(index, SupportsIndex):
629+
if callable(index):
630+
return index(self.axes[axis])
631+
632+
if isinstance(index, float):
633+
raise TypeError(f"Index {index} must be an integer, not float")
634+
635+
if isinstance(index, SupportsIndex):
632636
if abs(int(index)) >= self._hist.axis(axis).size:
633637
raise IndexError("histogram index is out of range")
634-
index %= self._hist.axis(axis).size
638+
return index % self._hist.axis(axis).size # type: ignore[no-any-return]
635639

636-
return index # type: ignore[return-value]
640+
return index
637641

638642
def _compute_commonindex(
639643
self, index: IndexingExpr
@@ -656,10 +660,10 @@ def _compute_commonindex(
656660

657661
# Normalize -> h[i] == h[i,]
658662
else:
659-
if not isinstance(index, tuple):
660-
index = (index,)
663+
tuple_index = (index,) if not isinstance(index, tuple) else index
664+
661665
# Now a list
662-
indexes = _expand_ellipsis(index, hist.rank())
666+
indexes = _expand_ellipsis(tuple_index, hist.rank())
663667

664668
if len(indexes) != hist.rank():
665669
raise IndexError("Wrong number of indices for histogram")
@@ -669,7 +673,7 @@ def _compute_commonindex(
669673
for i in range(len(indexes)): # pylint: disable=consider-using-enumerate
670674
# Support list of UHI indexers
671675
if isinstance(indexes[i], list):
672-
indexes[i] = [self._compute_uhi_index(index, i) for index in indexes[i]]
676+
indexes[i] = [self._compute_uhi_index(ind, i) for ind in indexes[i]]
673677
else:
674678
indexes[i] = self._compute_uhi_index(indexes[i], i)
675679

0 commit comments

Comments
 (0)