Skip to content

Commit 9f5a944

Browse files
authored
chore: add pylint (#690)
1 parent cdbfabb commit 9f5a944

File tree

14 files changed

+204
-166
lines changed

14 files changed

+204
-166
lines changed

.github/workflows/tests.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ jobs:
3131
- name: Build
3232
run: cmake --build build -j 2
3333

34+
pylint:
35+
runs-on: ubuntu-latest
36+
name: PyLint
37+
38+
steps:
39+
- uses: actions/checkout@v1
40+
with:
41+
submodules: true
42+
43+
- name: Run PyLint
44+
run: pipx run nox -s pylint
45+
3446

3547
cmake:
3648
runs-on: ubuntu-latest

noxfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ def lint(session: nox.Session) -> None:
4141
session.run("pre-commit", "run", "--all-files", *session.posargs)
4242

4343

44+
@nox.session
45+
def pylint(session: nox.Session) -> None:
46+
"""
47+
Run pylint.
48+
"""
49+
50+
session.install("pylint")
51+
session.install("-e", ".")
52+
session.run("pylint", "src")
53+
54+
4455
@nox.session
4556
def make_pickle(session: nox.Session) -> None:
4657
"""

pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,28 @@ manylinux-i686-image = "manylinux2014"
8585
select = "cp3?-*"
8686
manylinux-x86_64-image = "manylinux2010"
8787
manylinux-i686-image = "manylinux2010"
88+
89+
[tool.pylint]
90+
91+
master.py-version = "3.6"
92+
master.extension-pkg-allow-list = ["boost_histogram._core"]
93+
similarities.ignore-imports = "yes"
94+
messages_control.disable = [
95+
"fixme",
96+
"invalid-name",
97+
"line-too-long",
98+
"missing-class-docstring",
99+
"missing-function-docstring",
100+
"missing-module-docstring",
101+
"no-member", # C extensions mess with this
102+
"protected-access",
103+
"too-few-public-methods",
104+
"too-many-arguments",
105+
"too-many-branches",
106+
"too-many-lines",
107+
"too-many-locals",
108+
"too-many-return-statements",
109+
"too-many-statements",
110+
"wrong-import-position",
111+
"cyclic-import", # TODO: move files out of _internal
112+
]

src/boost_histogram/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from . import accumulators, axis, numpy, storage
22
from ._internal.enum import Kind
33
from ._internal.hist import Histogram, IndexingExpr
4-
from .tag import loc, overflow, rebin, sum, underflow
4+
from .tag import ( # pylint: disable=redefined-builtin
5+
loc,
6+
overflow,
7+
rebin,
8+
sum,
9+
underflow,
10+
)
511
from .version import version as __version__
612

713
try:

src/boost_histogram/_internal/axestuple.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class ArrayTuple(tuple): # type: ignore[type-arg]
1818
def __getattr__(self, name: str) -> Any:
1919
if name in self._REDUCTIONS:
2020
return partial(getattr(np, name), np.broadcast_arrays(*self)) # type: ignore[no-untyped-call]
21-
else:
22-
return self.__class__(getattr(a, name) for a in self)
21+
22+
return self.__class__(getattr(a, name) for a in self)
2323

2424
def __dir__(self) -> List[str]:
2525
names = dir(self.__class__) + dir("np.typing.NDArray[Any]")
@@ -51,6 +51,7 @@ def __init__(self, __iterable: Iterable[Axis]) -> None:
5151
raise TypeError(
5252
f"Only an iterable of Axis supported in AxesTuple, got {item}"
5353
)
54+
super().__init__()
5455

5556
@property
5657
def size(self) -> Tuple[int, ...]:
@@ -105,7 +106,7 @@ def __getattr__(self, attr: str) -> Tuple[Any, ...]:
105106

106107
def __setattr__(self, attr: str, values: Any) -> None:
107108
try:
108-
return super().__setattr__(attr, values)
109+
super().__setattr__(attr, values)
109110
except AttributeError:
110111
for s, v in zip_strict(self, values):
111112
s.__setattr__(attr, v)

src/boost_histogram/_internal/axis.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Union,
1515
)
1616

17-
import numpy as np
17+
import numpy as np # pylint: disable=unused-import
1818

1919
import boost_histogram
2020

@@ -31,10 +31,9 @@ def _isstr(value: Any) -> bool:
3131

3232
if isinstance(value, (str, bytes)):
3333
return True
34-
elif hasattr(value, "__iter__"):
34+
if hasattr(value, "__iter__"):
3535
return all(_isstr(v) for v in value)
36-
else:
37-
return False
36+
return False
3837

3938

4039
def _opts(**kwargs: bool) -> Set[str]:
@@ -87,7 +86,7 @@ def __init__(
8786
raise KeyError(
8887
"Cannot provide metadata by keyword and __dict__, use __dict__ only"
8988
)
90-
elif __dict__ is not None:
89+
if __dict__ is not None:
9190
self._ax.metadata = __dict__
9291
elif metadata is not None:
9392
self._ax.metadata["metadata"] = metadata
@@ -112,14 +111,11 @@ def index(self, value: Union[float, str]) -> int:
112111
Return the fractional index(es) given a value (or values) on the axis.
113112
"""
114113

115-
if not _isstr(value):
116-
return self._ax.index(value) # type: ignore[no-any-return]
117-
else:
118-
raise TypeError(
119-
"index({value}) cannot be a string for a numerical axis".format(
120-
value=value
121-
)
122-
)
114+
if _isstr(value):
115+
msg = f"index({value}) cannot be a string for a numerical axis"
116+
raise TypeError(msg)
117+
118+
return self._ax.index(value) # type: ignore[no-any-return]
123119

124120
def value(self, index: float) -> float:
125121
"""
@@ -486,7 +482,8 @@ def _repr_args_(self) -> List[str]:
486482
if len(self) > 20:
487483
ret = [repr(self.edges)]
488484
else:
489-
ret = ["[{}]".format(", ".join(format(v, "g") for v in self.edges))]
485+
args = ", ".join(format(v, "g") for v in self.edges)
486+
ret = [f"[{args}]"]
490487

491488
if self.traits.growth:
492489
ret.append("growth=True")
@@ -670,17 +667,15 @@ def index(self, value: Union[float, str]) -> int:
670667

671668
if _isstr(value):
672669
return self._ax.index(value) # type: ignore[no-any-return]
673-
else:
674-
raise TypeError(
675-
"index({value}) must be a string or iterable of strings for a StrCategory axis".format(
676-
value=value
677-
)
678-
)
670+
671+
msg = f"index({value}) must be a string or iterable of strings for a StrCategory axis"
672+
raise TypeError(msg)
679673

680674
def _repr_args_(self) -> List[str]:
681675
"Return inner part of signature for use in repr"
682676

683-
ret = ["[{}]".format(", ".join(repr(c) for c in self))]
677+
args = ", ".join(repr(c) for c in self)
678+
ret = [f"[{args}]"]
684679
ret += super()._repr_args_()
685680
return ret
686681

@@ -732,7 +727,8 @@ def __init__(
732727
def _repr_args_(self) -> List[str]:
733728
"Return inner part of signature for use in repr"
734729

735-
ret = ["[{}]".format(", ".join(format(c, "g") for c in self))]
730+
args = ", ".join(format(c, "g") for c in self)
731+
ret = [f"[{args}]"]
736732
ret += super()._repr_args_()
737733
return ret
738734

src/boost_histogram/_internal/axis_transform.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def _convert_cpp(cls: Type[T], this: Any) -> T:
3333
def __repr__(self) -> str:
3434
if hasattr(self, "_this"):
3535
return repr(self._this)
36-
else:
37-
return f"{self.__class__.__name__}() # Missing _this, broken class"
36+
37+
return f"{self.__class__.__name__}() # Missing _this, broken class"
3838

3939
def _produce(self, bins: int, start: float, stop: float) -> Any:
4040
# Note: this is an ABC; _type must be defined on children
@@ -62,7 +62,7 @@ class Pow(AxisTransform, family=boost_histogram):
6262
__slots__ = ()
6363
_type = ca.regular_pow
6464

65-
def __init__(self, power: float):
65+
def __init__(self, power: float): # pylint: disable=super-init-not-called
6666
"Create a new transform instance"
6767
# Note: this comes from family
6868
(cpp_class,) = self._types # type: ignore[attr-defined]
@@ -84,7 +84,7 @@ class Function(AxisTransform, family=boost_histogram):
8484
__slots__ = ()
8585
_type = ca.regular_trans
8686

87-
def __init__(
87+
def __init__( # pylint: disable=super-init-not-called
8888
self, forward: Any, inverse: Any, *, convert: Any = None, name: str = ""
8989
):
9090
"""

src/boost_histogram/_internal/deprecated.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ def __call__(self, func: Any) -> Any:
1717
@functools.wraps(func)
1818
def decorated_func(*args: Any, **kwargs: Any) -> Any:
1919
warnings.warn(
20-
"{} is deprecated: {}".format(
21-
self._name or func.__name__, self._reason
22-
),
20+
f"{self._name or func.__name__} is deprecated: {self._reason}",
2321
category=FutureWarning,
2422
stacklevel=2,
2523
)
2624
return func(*args, **kwargs)
2725

28-
decorated_func.__doc__ = "DEPRECATED: " + self._reason + "\n" + func.__doc__
26+
decorated_func.__doc__ = f"DEPRECATED: {self._reason}\n{func.__doc__}"
2927
return decorated_func

0 commit comments

Comments
 (0)