Skip to content

Commit 6ce6032

Browse files
authored
Drop isort 4, and clean up the resulting code (#10641)
1 parent 7be0b8f commit 6ce6032

File tree

7 files changed

+24
-57
lines changed

7 files changed

+24
-57
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Version requirement for `isort` has been bumped to >=5.0.0.
2+
The internal compatibility for older `isort` versions exposed via `pylint.utils.IsortDriver` has
3+
been removed.
4+
5+
Refs #10637

pylint/checkers/imports.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import astroid
1919
import astroid.modutils
20+
import isort
2021
from astroid import nodes
2122
from astroid.nodes._base_nodes import ImportNode
2223

@@ -35,7 +36,6 @@
3536
from pylint.interfaces import HIGH
3637
from pylint.reporters.ureports.nodes import Paragraph, Section, VerbatimText
3738
from pylint.typing import MessageDefinitionTuple
38-
from pylint.utils import IsortDriver
3939
from pylint.utils.linterstats import LinterStats
4040

4141
if TYPE_CHECKING:
@@ -322,6 +322,7 @@ def _make_graph(
322322
DEFAULT_PREFERRED_MODULES = ()
323323

324324

325+
# pylint: disable-next = too-many-instance-attributes
325326
class ImportsChecker(DeprecatedMixin, BaseChecker):
326327
"""BaseChecker for import statements.
327328
@@ -458,6 +459,15 @@ def __init__(self, linter: PyLinter) -> None:
458459
)
459460
self._excluded_edges: defaultdict[str, set[str]] = defaultdict(set)
460461

462+
self._isort_config = isort.Config(
463+
# There is no typo here. EXTRA_standard_library is
464+
# what most users want. The option has been named
465+
# KNOWN_standard_library for ages in pylint, and we
466+
# don't want to break compatibility.
467+
extra_standard_library=linter.config.known_standard_library,
468+
known_third_party=linter.config.known_third_party,
469+
)
470+
461471
def open(self) -> None:
462472
"""Called before visiting project (i.e set of modules)."""
463473
self.linter.stats.dependencies = {}
@@ -746,7 +756,6 @@ def _is_fallback_import(
746756
imports = [import_node for (import_node, _) in imports]
747757
return any(astroid.are_exclusive(import_node, node) for import_node in imports)
748758

749-
# pylint: disable = too-many-statements
750759
def _check_imports_order(self, _module_node: nodes.Module) -> tuple[
751760
list[tuple[ImportNode, str]],
752761
list[tuple[ImportNode, str]],
@@ -765,7 +774,6 @@ def _check_imports_order(self, _module_node: nodes.Module) -> tuple[
765774
third_party_not_ignored: list[tuple[ImportNode, str]] = []
766775
first_party_not_ignored: list[tuple[ImportNode, str]] = []
767776
local_not_ignored: list[tuple[ImportNode, str]] = []
768-
isort_driver = IsortDriver(self.linter.config)
769777
for node, modname in self._imports_stack:
770778
if modname.startswith("."):
771779
package = "." + modname.split(".")[1]
@@ -775,7 +783,7 @@ def _check_imports_order(self, _module_node: nodes.Module) -> tuple[
775783
ignore_for_import_order = not self.linter.is_message_enabled(
776784
"wrong-import-order", node.fromlineno
777785
)
778-
import_category = isort_driver.place_module(package)
786+
import_category = isort.place_module(package, config=self._isort_config)
779787
node_and_package_import = (node, package)
780788

781789
match import_category:

pylint/checkers/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import annotations
88

9-
import _string
9+
import _string # pylint: disable=wrong-import-order # Ruff and Isort disagree about the order here
1010
import builtins
1111
import fnmatch
1212
import itertools

pylint/utils/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from pylint.utils.file_state import FileState
1212
from pylint.utils.linterstats import LinterStats, ModuleStats, merge_stats
1313
from pylint.utils.utils import (
14-
HAS_ISORT_5,
15-
IsortDriver,
1614
_check_csv,
1715
_check_regexp_csv,
1816
_splitstrip,
@@ -29,10 +27,8 @@
2927
)
3028

3129
__all__ = [
32-
"HAS_ISORT_5",
3330
"ASTWalker",
3431
"FileState",
35-
"IsortDriver",
3632
"LinterStats",
3733
"ModuleStats",
3834
"_check_csv",

pylint/utils/utils.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44

55
from __future__ import annotations
66

7-
try:
8-
import isort.api
9-
import isort.settings
10-
11-
HAS_ISORT_5 = True
12-
except ImportError: # isort < 5
13-
import isort
14-
15-
HAS_ISORT_5 = False
16-
17-
import argparse
187
import codecs
198
import os
209
import re
@@ -344,30 +333,3 @@ def _ini_format(stream: TextIO, options: list[tuple[str, OptionDict, Any]]) -> N
344333
# remove trailing ',' from last element of the list
345334
value = value[:-1]
346335
print(f"{optname}={value}", file=stream)
347-
348-
349-
class IsortDriver:
350-
"""A wrapper around isort API that changed between versions 4 and 5."""
351-
352-
def __init__(self, config: argparse.Namespace) -> None:
353-
if HAS_ISORT_5:
354-
self.isort5_config = isort.settings.Config(
355-
# There is no typo here. EXTRA_standard_library is
356-
# what most users want. The option has been named
357-
# KNOWN_standard_library for ages in pylint, and we
358-
# don't want to break compatibility.
359-
extra_standard_library=config.known_standard_library,
360-
known_third_party=config.known_third_party,
361-
)
362-
else:
363-
# pylint: disable-next=no-member
364-
self.isort4_obj = isort.SortImports( # type: ignore[attr-defined]
365-
file_contents="",
366-
known_standard_library=config.known_standard_library,
367-
known_third_party=config.known_third_party,
368-
)
369-
370-
def place_module(self, package: str) -> str:
371-
if HAS_ISORT_5:
372-
return isort.api.place_module(package, self.isort5_config)
373-
return self.isort4_obj.place_module(package) # type: ignore[no-any-return]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dependencies = [
4545
"dill>=0.2; python_version<'3.11'",
4646
"dill>=0.3.6; python_version>='3.11'",
4747
"dill>=0.3.7; python_version>='3.12'",
48-
"isort>=4.2.5,!=5.13,<7",
48+
"isort>=5,!=5.13,<8",
4949
"mccabe>=0.6,<0.8",
5050
"platformdirs>=2.2",
5151
"tomli>=1.1; python_version<'3.11'",

tests/test_functional.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,13 @@
2323
LintModuleOutputUpdate,
2424
get_functional_test_files_from_directory,
2525
)
26-
from pylint.utils import HAS_ISORT_5
2726

2827
if TYPE_CHECKING:
2928
from pylint.lint import PyLinter
3029

3130
FUNCTIONAL_DIR = Path(__file__).parent.resolve() / "functional"
3231

3332

34-
# isort 5 has slightly different rules as isort 4. Testing both would be hard: test with isort 5 only.
35-
TESTS = [
36-
t
37-
for t in get_functional_test_files_from_directory(FUNCTIONAL_DIR)
38-
if not (t.base == "wrong_import_order" and not HAS_ISORT_5)
39-
]
40-
TESTS_NAMES = [t.base for t in TESTS]
4133
TEST_WITH_EXPECTED_DEPRECATION = [
4234
"anomalous_backslash_escape",
4335
"anomalous_unicode_escape",
@@ -55,7 +47,11 @@ def revert_stateful_config_changes(linter: PyLinter) -> Iterator[PyLinter]:
5547

5648

5749
@pytest.mark.usefixtures("revert_stateful_config_changes")
58-
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
50+
@pytest.mark.parametrize(
51+
"test_file",
52+
get_functional_test_files_from_directory(FUNCTIONAL_DIR),
53+
ids=lambda x: x.base,
54+
)
5955
def test_functional(test_file: FunctionalTestFile, pytestconfig: Config) -> None:
6056
__tracebackhide__ = True # pylint: disable=unused-variable
6157
lint_test: LintModuleOutputUpdate | testutils.LintModuleTest

0 commit comments

Comments
 (0)