From d6ebd063fff7ff9ed557c3f2bcacc8f9456583e6 Mon Sep 17 00:00:00 2001 From: Nikita Titov Date: Tue, 16 Feb 2021 20:09:13 +0300 Subject: [PATCH] [ci][python] run isort in CI linting job (#3990) * run isort in CI linting job * workaround conda compatibility issues --- .ci/get_workflow_status.py | 1 + .ci/test.sh | 7 ++++--- .editorconfig | 3 +++ .nuget/create_nuget.py | 2 -- python-package/lightgbm/__init__.py | 6 ++---- python-package/lightgbm/basic.py | 3 +-- python-package/lightgbm/compat.py | 3 +-- python-package/lightgbm/dask.py | 15 ++++++--------- python-package/lightgbm/engine.py | 3 +-- python-package/lightgbm/sklearn.py | 12 ++++-------- tests/python_package_test/test_engine.py | 7 ++----- tests/python_package_test/test_sklearn.py | 13 +++++++------ 12 files changed, 32 insertions(+), 43 deletions(-) diff --git a/.ci/get_workflow_status.py b/.ci/get_workflow_status.py index e4f338ac1..e693ec637 100644 --- a/.ci/get_workflow_status.py +++ b/.ci/get_workflow_status.py @@ -10,6 +10,7 @@ import json from os import environ from sys import argv, exit from time import sleep + try: from urllib import request except ImportError: diff --git a/.ci/test.sh b/.ci/test.sh index 80b2bea93..dff66479f 100755 --- a/.ci/test.sh +++ b/.ci/test.sh @@ -20,8 +20,8 @@ cd $BUILD_DIRECTORY if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then cd $BUILD_DIRECTORY/docs - conda install -q -y -n $CONDA_ENV -c conda-forge doxygen - pip install --user -r requirements.txt rstcheck + conda install -q -y -n $CONDA_ENV -c conda-forge doxygen rstcheck + pip install --user -r requirements.txt # check reStructuredText formatting cd $BUILD_DIRECTORY/python-package rstcheck --report warning `find . -type f -name "*.rst"` || exit -1 @@ -55,10 +55,11 @@ if [[ $TASK == "lint" ]]; then libxml2 \ "r-xfun>=0.19" \ "r-lintr>=2.0" - pip install --user cpplint mypy + pip install --user cpplint isort mypy echo "Linting Python code" pycodestyle --ignore=E501,W503 --exclude=./.nuget,./external_libs . || 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 echo "Linting R code" Rscript ${BUILD_DIRECTORY}/.ci/lint_r_code.R ${BUILD_DIRECTORY} || exit -1 diff --git a/.editorconfig b/.editorconfig index 068bd1c86..f7bd94f4f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,9 @@ indent_size = 2 [*.{py,sh,js}] indent_size = 4 +line_length = 120 +skip = external_libs +known_first_party = lightgbm # Placeholder files [{*.gitkeep,__init__.py}] diff --git a/.nuget/create_nuget.py b/.nuget/create_nuget.py index b9862b368..4f20e0f24 100644 --- a/.nuget/create_nuget.py +++ b/.nuget/create_nuget.py @@ -3,10 +3,8 @@ import datetime import os import sys - from distutils.file_util import copy_file - if __name__ == "__main__": source = sys.argv[1] current_dir = os.path.abspath(os.path.dirname(__file__)) diff --git a/python-package/lightgbm/__init__.py b/python-package/lightgbm/__init__.py index 46117904f..c7a1e0303 100644 --- a/python-package/lightgbm/__init__.py +++ b/python-package/lightgbm/__init__.py @@ -6,8 +6,7 @@ Contributors: https://github.com/microsoft/LightGBM/graphs/contributors. import os from .basic import Booster, Dataset, register_logger -from .callback import (early_stopping, print_evaluation, record_evaluation, - reset_parameter) +from .callback import early_stopping, print_evaluation, record_evaluation, reset_parameter from .engine import CVBooster, cv, train try: @@ -15,8 +14,7 @@ try: except ImportError: pass try: - from .plotting import (create_tree_digraph, plot_importance, plot_metric, - plot_split_value_histogram, plot_tree) + from .plotting import create_tree_digraph, plot_importance, plot_metric, plot_split_value_histogram, plot_tree except ImportError: pass try: diff --git a/python-package/lightgbm/basic.py b/python-package/lightgbm/basic.py index df3de3bfe..5581cb797 100644 --- a/python-package/lightgbm/basic.py +++ b/python-package/lightgbm/basic.py @@ -14,8 +14,7 @@ from typing import Any, Dict import numpy as np import scipy.sparse -from .compat import (PANDAS_INSTALLED, concat, dt_DataTable, is_dtype_sparse, - pd_DataFrame, pd_Series) +from .compat import PANDAS_INSTALLED, concat, dt_DataTable, is_dtype_sparse, pd_DataFrame, pd_Series from .libpath import find_lib_path diff --git a/python-package/lightgbm/compat.py b/python-package/lightgbm/compat.py index 3d55a905a..ceb0b2907 100644 --- a/python-package/lightgbm/compat.py +++ b/python-package/lightgbm/compat.py @@ -61,8 +61,7 @@ try: from sklearn.preprocessing import LabelEncoder from sklearn.utils.class_weight import compute_sample_weight from sklearn.utils.multiclass import check_classification_targets - from sklearn.utils.validation import (assert_all_finite, check_array, - check_X_y) + from sklearn.utils.validation import assert_all_finite, check_array, check_X_y try: from sklearn.exceptions import NotFittedError from sklearn.model_selection import GroupKFold, StratifiedKFold diff --git a/python-package/lightgbm/dask.py b/python-package/lightgbm/dask.py index cccf6b97d..c1cb430d8 100644 --- a/python-package/lightgbm/dask.py +++ b/python-package/lightgbm/dask.py @@ -9,20 +9,17 @@ It is based on dask-lightgbm, which was based on dask-xgboost. import socket from collections import defaultdict from copy import deepcopy -from typing import Any, Callable, Dict, Iterable, List, Optional, Type, Union, Set +from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Type, Union from urllib.parse import urlparse import numpy as np import scipy.sparse as ss -from .basic import (_LIB, LightGBMError, _choose_param_value, _ConfigAliases, - _log_warning, _safe_call) -from .compat import (DASK_INSTALLED, PANDAS_INSTALLED, SKLEARN_INSTALLED, - Client, LGBMNotFittedError, concat, dask_Array, - dask_DataFrame, dask_Series, default_client, delayed, - get_worker, pd_DataFrame, pd_Series, wait) -from .sklearn import (LGBMClassifier, LGBMModel, LGBMRanker, LGBMRegressor, - _lgbmmodel_doc_fit, _lgbmmodel_doc_predict) +from .basic import _LIB, LightGBMError, _choose_param_value, _ConfigAliases, _log_warning, _safe_call +from .compat import (DASK_INSTALLED, PANDAS_INSTALLED, SKLEARN_INSTALLED, Client, LGBMNotFittedError, concat, + dask_Array, dask_DataFrame, dask_Series, default_client, delayed, get_worker, pd_DataFrame, + pd_Series, wait) +from .sklearn import LGBMClassifier, LGBMModel, LGBMRanker, LGBMRegressor, _lgbmmodel_doc_fit, _lgbmmodel_doc_predict _DaskCollection = Union[dask_Array, dask_DataFrame, dask_Series] _DaskMatrixLike = Union[dask_Array, dask_DataFrame] diff --git a/python-package/lightgbm/engine.py b/python-package/lightgbm/engine.py index 62ea50147..478b1efac 100644 --- a/python-package/lightgbm/engine.py +++ b/python-package/lightgbm/engine.py @@ -7,8 +7,7 @@ from operator import attrgetter import numpy as np from . import callback -from .basic import (Booster, Dataset, LightGBMError, _ConfigAliases, - _InnerPredictor, _log_warning) +from .basic import Booster, Dataset, LightGBMError, _ConfigAliases, _InnerPredictor, _log_warning from .compat import SKLEARN_INSTALLED, _LGBMGroupKFold, _LGBMStratifiedKFold diff --git a/python-package/lightgbm/sklearn.py b/python-package/lightgbm/sklearn.py index 316696c4b..d6b882114 100644 --- a/python-package/lightgbm/sklearn.py +++ b/python-package/lightgbm/sklearn.py @@ -5,14 +5,10 @@ from inspect import signature import numpy as np -from .basic import (Dataset, LightGBMError, _choose_param_value, - _ConfigAliases, _log_warning) -from .compat import (SKLEARN_INSTALLED, LGBMNotFittedError, - _LGBMAssertAllFinite, _LGBMCheckArray, - _LGBMCheckClassificationTargets, _LGBMCheckSampleWeight, - _LGBMCheckXY, _LGBMClassifierBase, - _LGBMComputeSampleWeight, _LGBMLabelEncoder, - _LGBMModelBase, _LGBMRegressorBase, dt_DataTable, +from .basic import Dataset, LightGBMError, _choose_param_value, _ConfigAliases, _log_warning +from .compat import (SKLEARN_INSTALLED, LGBMNotFittedError, _LGBMAssertAllFinite, _LGBMCheckArray, + _LGBMCheckClassificationTargets, _LGBMCheckSampleWeight, _LGBMCheckXY, _LGBMClassifierBase, + _LGBMComputeSampleWeight, _LGBMLabelEncoder, _LGBMModelBase, _LGBMRegressorBase, dt_DataTable, pd_DataFrame) from .engine import train diff --git a/tests/python_package_test/test_engine.py b/tests/python_package_test/test_engine.py index eccc42ea9..bfcb77737 100644 --- a/tests/python_package_test/test_engine.py +++ b/tests/python_package_test/test_engine.py @@ -11,11 +11,8 @@ import psutil import pytest from scipy.sparse import csr_matrix, isspmatrix_csc, isspmatrix_csr from sklearn.datasets import load_svmlight_file, make_multilabel_classification -from sklearn.metrics import (average_precision_score, log_loss, - mean_absolute_error, mean_squared_error, - roc_auc_score) -from sklearn.model_selection import (GroupKFold, TimeSeriesSplit, - train_test_split) +from sklearn.metrics import average_precision_score, log_loss, mean_absolute_error, mean_squared_error, roc_auc_score +from sklearn.model_selection import GroupKFold, TimeSeriesSplit, train_test_split import lightgbm as lgb diff --git a/tests/python_package_test/test_sklearn.py b/tests/python_package_test/test_sklearn.py index 945985a9a..187ae34b2 100644 --- a/tests/python_package_test/test_sklearn.py +++ b/tests/python_package_test/test_sklearn.py @@ -1,30 +1,31 @@ # coding: utf-8 import itertools -import joblib import math import os -import lightgbm as lgb +import joblib import numpy as np import pytest from pkg_resources import parse_version from sklearn import __version__ as sk_version from sklearn.base import clone from sklearn.datasets import load_svmlight_file, make_multilabel_classification -from sklearn.utils.estimator_checks import check_parameters_default_constructible from sklearn.metrics import log_loss, mean_squared_error from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, train_test_split -from sklearn.multioutput import (MultiOutputClassifier, ClassifierChain, MultiOutputRegressor, - RegressorChain) +from sklearn.multioutput import ClassifierChain, MultiOutputClassifier, MultiOutputRegressor, RegressorChain +from sklearn.utils.estimator_checks import check_parameters_default_constructible from sklearn.utils.validation import check_is_fitted +import lightgbm as lgb + from .utils import load_boston, load_breast_cancer, load_digits, load_iris, load_linnerud sk_version = parse_version(sk_version) if sk_version < parse_version("0.23"): import warnings + from sklearn.exceptions import SkipTestWarning - from sklearn.utils.estimator_checks import _yield_all_checks, SkipTest + from sklearn.utils.estimator_checks import SkipTest, _yield_all_checks else: from sklearn.utils.estimator_checks import parametrize_with_checks