Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10801.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix `--known_third_party` config being ignored.

Closes #10801
27 changes: 16 additions & 11 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -756,14 +746,29 @@ 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]],
list[tuple[ImportNode, str]],
]:
"""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]] = []
Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/functional/w/wrong_import_order2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# external imports
import isort
import datetime # std import that should be treated as third-party

from six import moves

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/w/wrong_import_order2.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[IMPORTS]
known-third-party=datetime
Loading