diff --git a/doc/whatsnew/fragments/10801.bugfix b/doc/whatsnew/fragments/10801.bugfix new file mode 100644 index 0000000000..4ac7c1c426 --- /dev/null +++ b/doc/whatsnew/fragments/10801.bugfix @@ -0,0 +1,3 @@ +Fix `--known_third_party` config being ignored. + +Closes #10801 diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index b069a7a0c6..b045f74c21 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -322,7 +322,6 @@ def _make_graph( DEFAULT_PREFERRED_MODULES = () -# pylint: disable-next = too-many-instance-attributes class ImportsChecker(DeprecatedMixin, BaseChecker): """BaseChecker for import statements. @@ -459,15 +458,6 @@ def __init__(self, linter: PyLinter) -> None: ) self._excluded_edges: defaultdict[str, set[str]] = defaultdict(set) - self._isort_config = isort.Config( - # There is no typo here. EXTRA_standard_library is - # what most users want. The option has been named - # KNOWN_standard_library for ages in pylint, and we - # don't want to break compatibility. - extra_standard_library=linter.config.known_standard_library, - known_third_party=linter.config.known_third_party, - ) - def open(self) -> None: """Called before visiting project (i.e set of modules).""" self.linter.stats.dependencies = {} @@ -756,6 +746,21 @@ def _is_fallback_import( imports = [import_node for (import_node, _) in imports] return any(astroid.are_exclusive(import_node, node) for import_node in imports) + @property + def _isort_config(self) -> isort.Config: + """Get the config for use with isort. + + Only valid after CLI parsing finished, i.e. not in __init__ + """ + return isort.Config( + # There is no typo here. EXTRA_standard_library is + # what most users want. The option has been named + # KNOWN_standard_library for ages in pylint, and we + # don't want to break compatibility. + extra_standard_library=self.linter.config.known_standard_library, + known_third_party=self.linter.config.known_third_party, + ) + def _check_imports_order(self, _module_node: nodes.Module) -> tuple[ list[tuple[ImportNode, str]], list[tuple[ImportNode, str]], @@ -763,7 +768,7 @@ def _check_imports_order(self, _module_node: nodes.Module) -> tuple[ ]: """Checks imports of module `node` are grouped by category. - Imports must follow this order: standard, 3rd party, local + Imports must follow this order: standard, 3rd party, 1st party, local """ std_imports: list[tuple[ImportNode, str]] = [] third_party_imports: list[tuple[ImportNode, str]] = [] diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 7a45b472bd..5c44a8256e 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -6,7 +6,7 @@ from __future__ import annotations -import _string # pylint: disable=wrong-import-order # Ruff and Isort disagree about the order here +import _string import builtins import fnmatch import itertools diff --git a/tests/functional/w/wrong_import_order2.py b/tests/functional/w/wrong_import_order2.py index 7157512ddc..6f68906738 100644 --- a/tests/functional/w/wrong_import_order2.py +++ b/tests/functional/w/wrong_import_order2.py @@ -8,6 +8,7 @@ # external imports import isort +import datetime # std import that should be treated as third-party from six import moves diff --git a/tests/functional/w/wrong_import_order2.rc b/tests/functional/w/wrong_import_order2.rc new file mode 100644 index 0000000000..5f52571850 --- /dev/null +++ b/tests/functional/w/wrong_import_order2.rc @@ -0,0 +1,2 @@ +[IMPORTS] +known-third-party=datetime