Skip to content

Commit

Permalink
style: add more pre-commit checks
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jan 6, 2022
1 parent 61c845b commit d7ba10c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ run --all-files` to check even without installing the hook.

# Testing

Use PyTest to run the unit checks:
Use pytest to run the unit checks:

```bash
pytest
Expand Down
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ updates:
schedule:
interval: "daily"
ignore:
# Offical actions have moving tags like v1
# Official actions have moving tags like v1
# that are used, so they don't need updates here
- dependency-name: "actions/*"
28 changes: 27 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,31 @@ repos:
hooks:
- id: mypy
files: ^(src|tests)
args: []
args: [--show-error-codes]
additional_dependencies: [numpy==1.22.*, pytest, importlib_metadata]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
- id: python-no-eval
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
args: ["-L", "hist,thist,ans,nd,gaus"]

- repo: local
hooks:
- id: disallow-caps
name: Disallow improper capitalization
language: pygrep
entry: PyBind|Numpy|Cmake|CCache|Github|PyTest
exclude: .pre-commit-config.yaml
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. uhi documentation master file, created by
sphinx-quickstart on Tue Jan 19 16:19:27 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
contain the root ``toctree`` directive.
UHI: Unified Histogram Interface
=================================
Expand Down
8 changes: 4 additions & 4 deletions docs/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ are slices, and follow the rules listed above. This looks like:
h[{7: slice(0, 2, bh.rebin(4))}] # slice and rebin axis 7
If you don't like manually building slices, you can use the `Slicer()` utility
If you don't like manually building slices, you can use the ``Slicer()`` utility
to recover the original slicing syntax inside the dict:

.. code:: python3
Expand Down Expand Up @@ -118,8 +118,8 @@ Rejected proposals or proposals for future consideration, maybe ``hist``-only:

.. code:: python3
h2 = h[1.0j:2.5j + 1] # Adding a j suffix to a number could be used in place of `loc(x)`
h2 = h[1.0] # Floats in place of `loc(x)`: too easy to make a mistake
h2 = h[1.0j:2.5j + 1] # Adding a j suffix to a number could be used in place of ``loc(x)``
h2 = h[1.0] # Floats in place of ``loc(x)``: too easy to make a mistake
Examples
--------
Expand Down Expand Up @@ -255,7 +255,7 @@ Basic implementation example (WIP):
"When used in the start or stop of a Histogram's slice, x is taken to be the position in data coordinates."
def __init__(self, value, offset):
self.value = value
self.offset = offest
self.offset = offset
# supporting __add__ and __sub__ also recommended
Expand Down
2 changes: 1 addition & 1 deletion docs/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Plotters should only depend on the methods and attributes listed below. In short

Axes have:

* ``ax[i]``: A tuple of (lower, upper) bin, or the discrete bin value (integer or sting)
* ``ax[i]``: A tuple of (lower, upper) bin, or the discrete bin value (integer or string)
* ``len(ax)``: The number of bins
* Iteration is supported
* ``ax.traits.circular``: True if circular
Expand Down
28 changes: 14 additions & 14 deletions src/uhi/numpy_plottable.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def __getitem__(self, index: int) -> Tuple[float, float]:
Get the pair of edges (not discrete) or bin label (discrete).
"""

return tuple(self.edges[index]) # type: ignore
return tuple(self.edges[index]) # type: ignore[return-value]

def __len__(self) -> int:
"""
Return the number of bins (not counting flow bins, which are ignored
for this Protocol currently).
"""
return self.edges.shape[0] # type: ignore
return self.edges.shape[0] # type: ignore[no-any-return]

def __eq__(self, other: Any) -> bool:
"""
Expand Down Expand Up @@ -133,14 +133,14 @@ def __init__(
self._variances = variances

if len(bins) == 1 and isinstance(bins[0], tuple):
(bins,) = bins # type: ignore
(bins,) = bins # type: ignore[assignment]

if len(bins) == 0:
bins = tuple([None] * len(hist.shape))

self.kind = kind
self.axes: Sequence[PlottableAxis] = [
_bin_helper(shape, b) for shape, b in zip(hist.shape, bins) # type: ignore
_bin_helper(shape, b) for shape, b in zip(hist.shape, bins) # type: ignore[arg-type]
]

def __repr__(self) -> str:
Expand Down Expand Up @@ -184,7 +184,7 @@ def __init__(self, tAxis: Any) -> None:
self.tAx = tAxis

def __len__(self) -> int:
return self.tAx.GetNbins() # type: ignore
return self.tAx.GetNbins() # type: ignore[no-any-return]

def __getitem__(self, index: int) -> Any:
pass
Expand Down Expand Up @@ -226,7 +226,7 @@ def traits(self) -> PlottableTraits:
return Traits(circular=False, discrete=True)

def __getitem__(self, index: int) -> str:
return self.tAx.GetBinLabel(index + 1) # type: ignore
return self.tAx.GetBinLabel(index + 1) # type: ignore[no-any-return]

def __iter__(self) -> Iterator[str]:
for i in range(len(self)):
Expand All @@ -248,7 +248,7 @@ def __init__(self, thist: Any) -> None:

@property
def name(self) -> str:
return self.thist.GetName() # type: ignore
return self.thist.GetName() # type: ignore[no-any-return]


class ROOTPlottableHistogram(ROOTPlottableHistBase):
Expand All @@ -264,13 +264,13 @@ def kind(self) -> str:
return Kind.COUNT

def values(self) -> "np.typing.NDArray[Any]":
return _roottarray_asnumpy(self.thist, shape=self._shape)[ # type: ignore
return _roottarray_asnumpy(self.thist, shape=self._shape)[ # type: ignore[no-any-return]
tuple([slice(1, -1)] * len(self._shape))
]

def variances(self) -> "np.typing.NDArray[Any]":
if self.hasWeights:
return _roottarray_asnumpy(self.thist.GetSumw2(), shape=self._shape)[ # type: ignore
return _roottarray_asnumpy(self.thist.GetSumw2(), shape=self._shape)[ # type: ignore[no-any-return]
tuple([slice(1, -1)] * len(self._shape))
]
else:
Expand All @@ -281,7 +281,7 @@ def counts(self) -> "np.typing.NDArray[Any]":
return self.values()

sumw = self.values()
return np.divide( # type: ignore
return np.divide( # type: ignore[no-any-return]
sumw ** 2,
self.variances(),
out=np.zeros_like(sumw, dtype=np.float64),
Expand All @@ -298,12 +298,12 @@ def kind(self) -> str:
return Kind.MEAN

def values(self) -> "np.typing.NDArray[Any]":
return np.array( # type: ignore
return np.array( # type: ignore[no-any-return]
[self.thist.GetBinContent(i) for i in range(self.thist.GetNcells())]
).reshape(self._shape, order="F")[tuple([slice(1, -1)] * len(self._shape))]

def variances(self) -> "np.typing.NDArray[Any]":
return ( # type: ignore
return ( # type: ignore[no-any-return]
np.array([self.thist.GetBinError(i) for i in range(self.thist.GetNcells())])
** 2
).reshape(self._shape, order="F")[tuple([slice(1, -1)] * len(self._shape))]
Expand All @@ -313,12 +313,12 @@ def counts(self) -> "np.typing.NDArray[Any]":
tuple([slice(1, -1)] * len(self._shape))
]
if not (self.thist.GetSumw2() and self.thist.GetSumw2N()):
return sumw # type: ignore
return sumw # type: ignore[no-any-return]

sumw2 = _roottarray_asnumpy(self.thist.GetSumw2(), shape=self._shape)[
tuple([slice(1, -1)] * len(self._shape))
]
return np.divide( # type: ignore
return np.divide( # type: ignore[no-any-return]
sumw ** 2,
sumw2,
out=np.zeros_like(sumw, dtype=np.float64),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_uhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@


def test_version() -> None:
assert __version__ == metadata.version("uhi") # type: ignore
assert __version__ == metadata.version("uhi") # type: ignore[no-untyped-call]

0 comments on commit d7ba10c

Please sign in to comment.