RFC isort as linter and import sorter
See original GitHub issueWhat do we think of isort
? I’ve seen it in other projects and seems like a sane tool.
We could add it to our linter and pre-commit hooks if others think it’s a good thing. It does look nice to me.
Looking at the import section of test_common.py
, we have:
import os
import warnings
import sys
import re
import pkgutil
from inspect import isgenerator, signature, Parameter
from itertools import product, chain
from functools import partial
import pytest
import numpy as np
from sklearn.utils import all_estimators
from sklearn.utils._testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning
from sklearn.exceptions import FitFailedWarning
from sklearn.utils.estimator_checks import check_estimator
import sklearn
from sklearn.decomposition import PCA
from sklearn.linear_model._base import LinearClassifierMixin
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.model_selection import HalvingGridSearchCV
from sklearn.model_selection import HalvingRandomSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.utils import IS_PYPY
from sklearn.utils._tags import _DEFAULT_TAGS, _safe_tags
from sklearn.utils._testing import (
SkipTest,
set_random_state,
)
from sklearn.utils.estimator_checks import (
_construct_instance,
_set_checking_parameters,
_get_check_estimator_ids,
check_class_weight_balanced_linear_classifier,
parametrize_with_checks,
check_dataframe_column_names_consistency,
check_n_features_in_after_fitting,
check_transformer_get_feature_names_out,
check_transformer_get_feature_names_out_pandas,
)
And running isort --profile black sklearn/tests/test_common.py
will result in:
import os
import pkgutil
import re
import sys
import warnings
from functools import partial
from inspect import Parameter, isgenerator, signature
from itertools import chain, product
import numpy as np
import pytest
import sklearn
from sklearn.decomposition import PCA
from sklearn.exceptions import ConvergenceWarning, FitFailedWarning
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.linear_model import LogisticRegression, Ridge
from sklearn.linear_model._base import LinearClassifierMixin
from sklearn.model_selection import (
GridSearchCV,
HalvingGridSearchCV,
HalvingRandomSearchCV,
RandomizedSearchCV,
)
from sklearn.pipeline import make_pipeline
from sklearn.utils import IS_PYPY, all_estimators
from sklearn.utils._tags import _DEFAULT_TAGS, _safe_tags
from sklearn.utils._testing import SkipTest, ignore_warnings, set_random_state
from sklearn.utils.estimator_checks import (
_construct_instance,
_get_check_estimator_ids,
_set_checking_parameters,
check_class_weight_balanced_linear_classifier,
check_dataframe_column_names_consistency,
check_estimator,
check_n_features_in_after_fitting,
check_transformer_get_feature_names_out,
check_transformer_get_feature_names_out_pandas,
parametrize_with_checks,
)
with the following diff:
diff --git a/sklearn/tests/test_common.py b/sklearn/tests/test_common.py
index 251f0831f..d43524292 100644
--- a/sklearn/tests/test_common.py
+++ b/sklearn/tests/test_common.py
@@ -7,52 +7,44 @@ General tests for all estimators in sklearn.
# License: BSD 3 clause
import os
-import warnings
-import sys
-import re
import pkgutil
-from inspect import isgenerator, signature, Parameter
-from itertools import product, chain
+import re
+import sys
+import warnings
from functools import partial
+from inspect import Parameter, isgenerator, signature
+from itertools import chain, product
-import pytest
import numpy as np
-
-from sklearn.utils import all_estimators
-from sklearn.utils._testing import ignore_warnings
-from sklearn.exceptions import ConvergenceWarning
-from sklearn.exceptions import FitFailedWarning
-from sklearn.utils.estimator_checks import check_estimator
+import pytest
import sklearn
-
from sklearn.decomposition import PCA
-from sklearn.linear_model._base import LinearClassifierMixin
-from sklearn.linear_model import LogisticRegression
-from sklearn.linear_model import Ridge
-from sklearn.model_selection import GridSearchCV
-from sklearn.model_selection import RandomizedSearchCV
+from sklearn.exceptions import ConvergenceWarning, FitFailedWarning
from sklearn.experimental import enable_halving_search_cv # noqa
-from sklearn.model_selection import HalvingGridSearchCV
-from sklearn.model_selection import HalvingRandomSearchCV
+from sklearn.linear_model import LogisticRegression, Ridge
+from sklearn.linear_model._base import LinearClassifierMixin
+from sklearn.model_selection import (
+ GridSearchCV,
+ HalvingGridSearchCV,
+ HalvingRandomSearchCV,
+ RandomizedSearchCV,
+)
from sklearn.pipeline import make_pipeline
-
-from sklearn.utils import IS_PYPY
+from sklearn.utils import IS_PYPY, all_estimators
from sklearn.utils._tags import _DEFAULT_TAGS, _safe_tags
-from sklearn.utils._testing import (
- SkipTest,
- set_random_state,
-)
+from sklearn.utils._testing import SkipTest, ignore_warnings, set_random_state
from sklearn.utils.estimator_checks import (
_construct_instance,
- _set_checking_parameters,
_get_check_estimator_ids,
+ _set_checking_parameters,
check_class_weight_balanced_linear_classifier,
- parametrize_with_checks,
check_dataframe_column_names_consistency,
+ check_estimator,
check_n_features_in_after_fitting,
check_transformer_get_feature_names_out,
check_transformer_get_feature_names_out_pandas,
+ parametrize_with_checks,
)
Issue Analytics
- State:
- Created 2 years ago
- Comments:16 (16 by maintainers)
Top Results From Across the Web
isort
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command...
Read more >Use isort to sort your Python module imports automatically
isort is a Python library to sort imports alphabetically, and automatically separated into sections and by type. Before we use isort to format...
Read more >The Real Python Podcast
... about or started to use tools like linters, code formatters, import sorters, ... RFC 6750 - The OAuth 2.0 Authorization Framework: Bearer...
Read more >isort - command-not-found.com
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections. It provides a command line utility, ...
Read more >Simple index - piwheels
... pyngdom byuoauth mock-import pyiarduinoi2cph odoo13-addon-web-dialog-size yunpian-sdk-python wix-protos-answers-dwh-label django-treenav snn python-sap ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am +1 for it such that @jeremiedbb does not complain when I manually sort imports 😃
I am worried about imposing yet another dev dependency to our first time contributors.
As long as we do not enforce isort in the linter step, then why not.
What would be great would be to have a sklearn-format-bot such as commenting a PR with
/format
would call isort followed by black only on the Python files touched by the PR (and black only on the examples where isort would not be mandated).And we would comment with this only prior to merging.
Then we could make black linting optional: it would not block the other builds and it would be the responsibility of the merger to format the PR.
We could still use
pyflakes
(instead offlake8
) as a mandatory linter step to fail early and avoid wasting CI time with all-failing-pytests on undefined variable errors for instance.