Skip to content

Commit

Permalink
[ci] [python-package] add more linting checks (#6049)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Aug 19, 2023
1 parent aafdf74 commit c60bc73
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
8 changes: 3 additions & 5 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class _MissingType(Enum):

class _DummyLogger:
def info(self, msg: str) -> None:
print(msg)
print(msg) # noqa: T201

def warning(self, msg: str) -> None:
warnings.warn(msg, stacklevel=3)
Expand Down Expand Up @@ -467,11 +467,10 @@ def _get_all_param_aliases() -> Dict[str, List[str]]:
ctypes.c_int64(actual_len),
ctypes.byref(tmp_out_len),
ptr_string_buffer))
aliases = json.loads(
return json.loads(
string_buffer.value.decode('utf-8'),
object_hook=lambda obj: {k: [k] + v for k, v in obj.items()}
)
return aliases

@classmethod
def get(cls, *args) -> Set[str]:
Expand Down Expand Up @@ -3209,8 +3208,7 @@ def __copy__(self) -> "Booster":

def __deepcopy__(self, _) -> "Booster":
model_str = self.model_to_string(num_iteration=-1)
booster = Booster(model_str=model_str)
return booster
return Booster(model_str=model_str)

def __getstate__(self) -> Dict[str, Any]:
this = self.__dict__.copy()
Expand Down
10 changes: 2 additions & 8 deletions python-package/lightgbm/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,7 @@ def create_tree_digraph(

model = booster.dump_model()
tree_infos = model['tree_info']
if 'feature_names' in model:
feature_names = model['feature_names']
else:
feature_names = None

feature_names = model.get('feature_names', None)
monotone_constraints = model.get('monotone_constraints', None)

if tree_index < len(tree_infos):
Expand All @@ -722,7 +718,7 @@ def create_tree_digraph(
)[0]
example_case = example_case[0]

graph = _to_graphviz(
return _to_graphviz(
tree_info=tree_info,
show_info=show_info,
feature_names=feature_names,
Expand All @@ -734,8 +730,6 @@ def create_tree_digraph(
**kwargs
)

return graph


def plot_tree(
booster: Union[Booster, LGBMModel],
Expand Down
16 changes: 13 additions & 3 deletions python-package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ select = [
# pycodestyle
"E",
# pyflakes
"F"
"F",
# flake8-return: unnecessary assignment before return
"RET504",
# flake8-simplify: use dict.get() instead of an if-else block
"SIM401",
# flake8-print
"T",
]

# this should be set to the oldest version of python LightGBM supports
Expand All @@ -120,13 +126,17 @@ target-version = "py37"
[tool.ruff.per-file-ignores]
"examples/*" = [
# pydocstyle
"D"
"D",
# flake8-print
"T"
]
"tests/*" = [
# (flake8-bugbear) Found useless expression
"B018",
# pydocstyle
"D"
"D",
# flake8-print
"T"
]

[tool.ruff.pydocstyle]
Expand Down
8 changes: 3 additions & 5 deletions tests/distributed/_test_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _find_random_open_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
port = s.getsockname()[1]
return port
return port # noqa: RET504


def _generate_n_ports(n: int) -> Generator[int, None, None]:
Expand All @@ -47,8 +47,7 @@ def create_data(task: str, n_samples: int = 1_000) -> np.ndarray:
X, y = make_blobs(n_samples, centers=centers, random_state=42)
elif task == 'regression':
X, y = make_regression(n_samples, n_features=4, n_informative=2, random_state=42)
dataset = np.hstack([y.reshape(-1, 1), X])
return dataset
return np.hstack([y.reshape(-1, 1), X])


class DistributedMockup:
Expand Down Expand Up @@ -149,8 +148,7 @@ def predict(self, predict_config: Dict[str, Any]) -> np.ndarray:
result = subprocess.run(cmd)
if result.returncode != 0:
raise RuntimeError('Error in prediction')
y_pred = np.loadtxt(str(TESTS_DIR / 'predictions.txt'))
return y_pred
return np.loadtxt(str(TESTS_DIR / 'predictions.txt'))

def write_train_config(self, i: int) -> None:
"""Create a file train{i}.conf with the required configuration to train.
Expand Down
3 changes: 1 addition & 2 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,8 +1720,7 @@ def generate_trainset_for_monotone_constraints_tests(x3_to_category=True):
categorical_features = []
if x3_to_category:
categorical_features = [2]
trainset = lgb.Dataset(x, label=y, categorical_feature=categorical_features, free_raw_data=False)
return trainset
return lgb.Dataset(x, label=y, categorical_feature=categorical_features, free_raw_data=False)


@pytest.mark.skipif(getenv('TASK', '') == 'cuda', reason='Monotone constraints are not yet supported by CUDA version')
Expand Down
2 changes: 1 addition & 1 deletion tests/python_package_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ def pickle_and_unpickle_object(obj, serializer):
filepath=tmp_file.name,
serializer=serializer
)
return obj_from_disk
return obj_from_disk # noqa: RET504

0 comments on commit c60bc73

Please sign in to comment.