Unverified Commit 6f19edde authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[ci] [python-package] enforce 'pylint' checks (fixes #4308) (#6334)

parent 776c5c3c
...@@ -1215,7 +1215,7 @@ class _InnerPredictor: ...@@ -1215,7 +1215,7 @@ class _InnerPredictor:
) )
if pred_leaf: if pred_leaf:
preds = preds.astype(np.int32) preds = preds.astype(np.int32)
is_sparse = isinstance(preds, scipy.sparse.spmatrix) or isinstance(preds, list) is_sparse = isinstance(preds, (list, scipy.sparse.spmatrix))
if not is_sparse and preds.size != nrow: if not is_sparse and preds.size != nrow:
if preds.size % nrow == 0: if preds.size % nrow == 0:
preds = preds.reshape(nrow, -1) preds = preds.reshape(nrow, -1)
...@@ -2681,7 +2681,10 @@ class Dataset: ...@@ -2681,7 +2681,10 @@ class Dataset:
'In multiclass classification init_score can also be a list of lists, numpy 2-D array or pandas DataFrame.' 'In multiclass classification init_score can also be a list of lists, numpy 2-D array or pandas DataFrame.'
) )
else: else:
dtype = np.int32 if (field_name == 'group' or field_name == 'position') else np.float32 if field_name in {'group', 'position'}:
dtype = np.int32
else:
dtype = np.float32
data = _list_to_1d_numpy(data, dtype=dtype, name=field_name) data = _list_to_1d_numpy(data, dtype=dtype, name=field_name)
ptr_data: Union[_ctypes_float_ptr, _ctypes_int_ptr] ptr_data: Union[_ctypes_float_ptr, _ctypes_int_ptr]
...@@ -3106,7 +3109,7 @@ class Dataset: ...@@ -3106,7 +3109,7 @@ class Dataset:
if self._need_slice and self.used_indices is not None and self.reference is not None: if self._need_slice and self.used_indices is not None and self.reference is not None:
self.data = self.reference.data self.data = self.reference.data
if self.data is not None: if self.data is not None:
if isinstance(self.data, np.ndarray) or isinstance(self.data, scipy.sparse.spmatrix): if isinstance(self.data, (np.ndarray, scipy.sparse.spmatrix)):
self.data = self.data[self.used_indices, :] self.data = self.data[self.used_indices, :]
elif isinstance(self.data, pd_DataFrame): elif isinstance(self.data, pd_DataFrame):
self.data = self.data.iloc[self.used_indices].copy() self.data = self.data.iloc[self.used_indices].copy()
...@@ -3284,7 +3287,7 @@ class Dataset: ...@@ -3284,7 +3287,7 @@ class Dataset:
self.data = None self.data = None
elif isinstance(self.data, scipy.sparse.spmatrix): elif isinstance(self.data, scipy.sparse.spmatrix):
sparse_format = self.data.getformat() sparse_format = self.data.getformat()
if isinstance(other.data, np.ndarray) or isinstance(other.data, scipy.sparse.spmatrix): if isinstance(other.data, (np.ndarray, scipy.sparse.spmatrix)):
self.data = scipy.sparse.hstack((self.data, other.data), format=sparse_format) self.data = scipy.sparse.hstack((self.data, other.data), format=sparse_format)
elif isinstance(other.data, pd_DataFrame): elif isinstance(other.data, pd_DataFrame):
self.data = scipy.sparse.hstack((self.data, other.data.values), format=sparse_format) self.data = scipy.sparse.hstack((self.data, other.data.values), format=sparse_format)
......
...@@ -393,8 +393,8 @@ def plot_metric( ...@@ -393,8 +393,8 @@ def plot_metric(
for name in dataset_names_iter: for name in dataset_names_iter:
metrics_for_one = eval_results[name] metrics_for_one = eval_results[name]
results = metrics_for_one[metric] results = metrics_for_one[metric]
max_result = max(max(results), max_result) max_result = max(*results, max_result)
min_result = min(min(results), min_result) min_result = min(*results, min_result)
ax.plot(x_, results, label=name) ax.plot(x_, results, label=name)
ax.legend(loc='best') ax.legend(loc='best')
...@@ -804,7 +804,7 @@ def plot_tree( ...@@ -804,7 +804,7 @@ def plot_tree(
The plot with single tree. The plot with single tree.
""" """
if MATPLOTLIB_INSTALLED: if MATPLOTLIB_INSTALLED:
import matplotlib.image as image import matplotlib.image
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
else: else:
raise ImportError('You must install matplotlib and restart your session to plot tree.') raise ImportError('You must install matplotlib and restart your session to plot tree.')
...@@ -821,7 +821,7 @@ def plot_tree( ...@@ -821,7 +821,7 @@ def plot_tree(
s = BytesIO() s = BytesIO()
s.write(graph.pipe(format='png')) s.write(graph.pipe(format='png'))
s.seek(0) s.seek(0)
img = image.imread(s) img = matplotlib.image.imread(s)
ax.imshow(img) ax.imshow(img)
ax.axis('off') ax.axis('off')
......
...@@ -125,7 +125,21 @@ ignore = [ ...@@ -125,7 +125,21 @@ ignore = [
# (pydocstyle) Missing docstring in magic method # (pydocstyle) Missing docstring in magic method
"D105", "D105",
# (pycodestyle) Line too long # (pycodestyle) Line too long
"E501" "E501",
# (pylint) Too many branches
"PLR0912",
# (pylint) Too many arguments in function definition
"PLR0913",
# (pylint) Too many statements
"PLR0915",
# (pylint) Consider merging multiple comparisons
"PLR1714",
# (pylint) Magic value used in comparison
"PLR2004",
# (pylint) for loop veriable overwritten by assignment target
"PLW2901",
# (pylint) use 'elif' instead of 'else' then 'if', to reduce indentation
"PLR5501"
] ]
select = [ select = [
# flake8-bugbear # flake8-bugbear
...@@ -138,6 +152,8 @@ select = [ ...@@ -138,6 +152,8 @@ select = [
"E", "E",
# pyflakes # pyflakes
"F", "F",
# pylint
"PL",
# flake8-return: unnecessary assignment before return # flake8-return: unnecessary assignment before return
"RET504", "RET504",
# flake8-simplify: use dict.get() instead of an if-else block # flake8-simplify: use dict.get() instead of an if-else block
...@@ -159,6 +175,10 @@ select = [ ...@@ -159,6 +175,10 @@ select = [
# flake8-print # flake8-print
"T" "T"
] ]
"python-package/lightgbm/basic.py" = [
# (pylint) Using the global statement is discouraged
"PLW0603"
]
"tests/*" = [ "tests/*" = [
# (flake8-bugbear) Found useless expression # (flake8-bugbear) Found useless expression
"B018", "B018",
......
...@@ -79,7 +79,7 @@ class DistributedMockup: ...@@ -79,7 +79,7 @@ class DistributedMockup:
"""Start the training process on the `i`-th worker.""" """Start the training process on the `i`-th worker."""
config_path = TESTS_DIR / f"train{i}.conf" config_path = TESTS_DIR / f"train{i}.conf"
cmd = [self.executable, f"config={config_path}"] cmd = [self.executable, f"config={config_path}"]
return subprocess.run(cmd) return subprocess.run(cmd, check=True)
def _set_ports(self) -> None: def _set_ports(self) -> None:
"""Randomly assign a port for training to each worker and save all ports to mlist.txt.""" """Randomly assign a port for training to each worker and save all ports to mlist.txt."""
...@@ -145,7 +145,7 @@ class DistributedMockup: ...@@ -145,7 +145,7 @@ class DistributedMockup:
with open(config_path, "wt") as file: with open(config_path, "wt") as file:
_write_dict(self.predict_config, file) _write_dict(self.predict_config, file)
cmd = [self.executable, f"config={config_path}"] cmd = [self.executable, f"config={config_path}"]
result = subprocess.run(cmd) result = subprocess.run(cmd, check=True)
if result.returncode != 0: if result.returncode != 0:
raise RuntimeError("Error in prediction") raise RuntimeError("Error in prediction")
return np.loadtxt(str(TESTS_DIR / "predictions.txt")) return np.loadtxt(str(TESTS_DIR / "predictions.txt"))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment