Skip to content

Commit 5c8062b

Browse files
authored
Add support for isort option --known-first-party (#10803)
* Add support for isort: known-first-party Similar to known-third-party this recognizes modules as first party modules. * Add feature doc fragment
1 parent 5a770e3 commit 5c8062b

7 files changed

Lines changed: 36 additions & 1 deletion

File tree

doc/user_guide/configuration/all-options.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,13 @@ Standard Checkers
10411041
**Default:** ``()``
10421042

10431043

1044+
--known-first-party
1045+
"""""""""""""""""""
1046+
*Force import order to recognize a module as part of a first party library.*
1047+
1048+
**Default:** ``()``
1049+
1050+
10441051
--known-third-party
10451052
"""""""""""""""""""
10461053
*Force import order to recognize a module as part of a third party library.*
@@ -1082,6 +1089,8 @@ Standard Checkers
10821089
10831090
known-standard-library = []
10841091
1092+
known-first-party = []
1093+
10851094
known-third-party = ["enchant"]
10861095
10871096
preferred-modules = []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add support for `--known-first-party` similar to `--known-third-party`.
2+
3+
Refs #10803

examples/pylintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ int-import-graph=
398398
# compatibility libraries.
399399
known-standard-library=
400400

401+
# Force import order to recognize a module as part of a first party library.
402+
known-first-party=
403+
401404
# Force import order to recognize a module as part of a third party library.
402405
known-third-party=enchant
403406

examples/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ max-module-lines = 1000
350350
# libraries.
351351
# known-standard-library =
352352

353+
# Force import order to recognize a module as part of a third party library.
354+
# known-first-party =
355+
353356
# Force import order to recognize a module as part of a third party library.
354357
known-third-party = [ "enchant" ]
355358

pylint/checkers/imports.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def _make_graph(
318318

319319

320320
DEFAULT_STANDARD_LIBRARY = ()
321+
DEFAULT_KNOWN_FIRST_PARTY = ()
321322
DEFAULT_KNOWN_THIRD_PARTY = ("enchant",)
322323
DEFAULT_PREFERRED_MODULES = ()
323324

@@ -401,6 +402,16 @@ class ImportsChecker(DeprecatedMixin, BaseChecker):
401402
"the standard compatibility libraries.",
402403
},
403404
),
405+
(
406+
"known-first-party",
407+
{
408+
"default": DEFAULT_KNOWN_FIRST_PARTY,
409+
"type": "csv",
410+
"metavar": "<modules>",
411+
"help": "Force import order to recognize a module as part of "
412+
"a first party library.",
413+
},
414+
),
404415
(
405416
"known-third-party",
406417
{
@@ -758,6 +769,7 @@ def _isort_config(self) -> isort.Config:
758769
# KNOWN_standard_library for ages in pylint, and we
759770
# don't want to break compatibility.
760771
extra_standard_library=self.linter.config.known_standard_library,
772+
known_first_party=self.linter.config.known_first_party,
761773
known_third_party=self.linter.config.known_third_party,
762774
)
763775

tests/functional/w/wrong_import_order2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import os
77
from sys import argv
88

9-
# external imports
9+
# external/third-party imports
1010
import isort
1111
import datetime # std import that should be treated as third-party
1212

13+
# First-party imports
1314
from six import moves
1415

16+
import pylint
17+
import re # std import that should be treated as first-party
18+
1519
# local_imports
1620
from . import my_package
1721
from .my_package import myClass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[IMPORTS]
2+
known-first-party=re
23
known-third-party=datetime

0 commit comments

Comments
 (0)