diff --git a/.ci/test.sh b/.ci/test.sh index f18198b5924f..d4d217d673a4 100755 --- a/.ci/test.sh +++ b/.ci/test.sh @@ -76,13 +76,16 @@ if [[ $TASK == "lint" ]]; then conda install -q -y -n $CONDA_ENV \ cmakelint \ cpplint \ + flake8 \ isort \ mypy \ - pycodestyle \ pydocstyle \ "r-lintr>=3.0" echo "Linting Python code" - pycodestyle --ignore=E501,W503 --exclude=./.nuget,./external_libs . || exit -1 + flake8 \ + --ignore=E501,W503 \ + --exclude=./.nuget,./external_libs,./python-package/build \ + . || exit -1 pydocstyle --convention=numpy --add-ignore=D105 --match-dir="^(?!^external_libs|test|example).*" --match="(?!^test_|setup).*\.py" . || exit -1 isort . --check-only || exit -1 mypy --ignore-missing-imports python-package/ || true diff --git a/docs/conf.py b/docs/conf.py index c97bf7c7fd62..38ec99e75a36 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -105,7 +105,7 @@ def run(self) -> List: 'scipy.sparse', ] try: - import sklearn + import sklearn # noqa: F401 except ImportError: autodoc_mock_imports.append('sklearn') # hide type hints in API docs diff --git a/python-package/lightgbm/compat.py b/python-package/lightgbm/compat.py index adbc5f62593e..6e1eafb3111e 100644 --- a/python-package/lightgbm/compat.py +++ b/python-package/lightgbm/compat.py @@ -36,14 +36,14 @@ def __init__(self, *args, **kwargs): """matplotlib""" try: - import matplotlib + import matplotlib # noqa: F401 MATPLOTLIB_INSTALLED = True except ImportError: MATPLOTLIB_INSTALLED = False """graphviz""" try: - import graphviz + import graphviz # noqa: F401 GRAPHVIZ_INSTALLED = True except ImportError: GRAPHVIZ_INSTALLED = False diff --git a/python-package/setup.py b/python-package/setup.py index 0075aeb64019..f40229f2c430 100644 --- a/python-package/setup.py +++ b/python-package/setup.py @@ -96,7 +96,7 @@ def silent_call(cmd: List[str], raise_error: bool = False, error_msg: str = '') with open(LOG_PATH, "ab") as log: subprocess.check_call(cmd, stderr=log, stdout=log) return 0 - except Exception as err: + except Exception: if raise_error: raise Exception("\n".join((error_msg, LOG_NOTICE))) return 1 diff --git a/tests/distributed/_test_distributed.py b/tests/distributed/_test_distributed.py index 9e1dd8e4f5a4..61b0d9469acb 100644 --- a/tests/distributed/_test_distributed.py +++ b/tests/distributed/_test_distributed.py @@ -4,7 +4,7 @@ import subprocess from concurrent.futures import ThreadPoolExecutor from pathlib import Path -from typing import Any, Dict, Generator, List +from typing import Any, Dict, Generator, List, Optional import numpy as np import pytest @@ -106,7 +106,7 @@ def _write_data(self, partitions: List[np.ndarray]) -> None: for i, partition in enumerate(partitions): np.savetxt(str(TESTS_DIR / f'train{i}.txt'), partition, delimiter=',') - def fit(self, partitions: List[np.ndarray], train_config: Dict = {}) -> None: + def fit(self, partitions: List[np.ndarray], train_config: Optional[Dict] = None) -> None: """Run the distributed training process on a single machine. For each worker i: @@ -117,6 +117,7 @@ def fit(self, partitions: List[np.ndarray], train_config: Dict = {}) -> None: 5. The trained model is saved as model{i}.txt. Each model file only differs in data and local_listen_port. The whole training set is saved as train.txt. """ + train_config = train_config or {} self.train_config = copy.deepcopy(self.default_train_config) self.train_config.update(train_config) self.n_workers = self.train_config['num_machines'] diff --git a/tests/python_package_test/test_callback.py b/tests/python_package_test/test_callback.py index d75b9309918c..cb5dc707bf43 100644 --- a/tests/python_package_test/test_callback.py +++ b/tests/python_package_test/test_callback.py @@ -3,7 +3,7 @@ import lightgbm as lgb -from .utils import SERIALIZERS, pickle_and_unpickle_object, pickle_obj, unpickle_obj +from .utils import SERIALIZERS, pickle_and_unpickle_object def reset_feature_fraction(boosting_round): diff --git a/tests/python_package_test/test_dask.py b/tests/python_package_test/test_dask.py index a38dee5c830c..30eb81055e75 100644 --- a/tests/python_package_test/test_dask.py +++ b/tests/python_package_test/test_dask.py @@ -1724,7 +1724,7 @@ def test_dask_methods_and_sklearn_equivalents_have_similar_signatures(methods): @pytest.mark.parametrize('task', tasks) def test_training_succeeds_when_data_is_dataframe_and_label_is_column_array(task, cluster): - with Client(cluster) as client: + with Client(cluster): _, _, _, _, dX, dy, dw, dg = _create_data( objective=task, output='dataframe', @@ -1803,7 +1803,7 @@ def _tested_estimators(): @pytest.mark.parametrize("estimator", _tested_estimators()) @pytest.mark.parametrize("check", sklearn_checks_to_run()) def test_sklearn_integration(estimator, check, cluster): - with Client(cluster) as client: + with Client(cluster): estimator.set_params(local_listen_port=18000, time_out=5) name = type(estimator).__name__ check(name, estimator) diff --git a/tests/python_package_test/test_engine.py b/tests/python_package_test/test_engine.py index 93e0b4f648ba..9f91ffcbeb46 100644 --- a/tests/python_package_test/test_engine.py +++ b/tests/python_package_test/test_engine.py @@ -2903,7 +2903,7 @@ def test_forced_split_feature_indices(tmp_path): "forcedsplits_filename": tmp_split_file } with pytest.raises(lgb.basic.LightGBMError, match="Forced splits file includes feature index"): - bst = lgb.train(params, lgb_train) + lgb.train(params, lgb_train) def test_forced_bins(): diff --git a/tests/python_package_test/test_sklearn.py b/tests/python_package_test/test_sklearn.py index 5873bf9112c3..35ba6e11220b 100644 --- a/tests/python_package_test/test_sklearn.py +++ b/tests/python_package_test/test_sklearn.py @@ -446,7 +446,9 @@ def test_clone_and_property(): gbm_clone = clone(gbm) assert isinstance(gbm.booster_, lgb.Booster) + assert isinstance(gbm_clone.booster_, lgb.Booster) assert isinstance(gbm.feature_importances_, np.ndarray) + assert isinstance(gbm_clone.feature_importances_, np.ndarray) X, y = load_digits(n_class=2, return_X_y=True) clf = lgb.LGBMClassifier(n_estimators=10, verbose=-1)