diff --git a/doc/user_guide/configuration/all-options.rst b/doc/user_guide/configuration/all-options.rst index 309714a0de..27c2de6734 100644 --- a/doc/user_guide/configuration/all-options.rst +++ b/doc/user_guide/configuration/all-options.rst @@ -1041,6 +1041,13 @@ Standard Checkers **Default:** ``()`` +--known-first-party +""""""""""""""""""" +*Force import order to recognize a module as part of a first party library.* + +**Default:** ``()`` + + --known-third-party """"""""""""""""""" *Force import order to recognize a module as part of a third party library.* @@ -1082,6 +1089,8 @@ Standard Checkers known-standard-library = [] + known-first-party = [] + known-third-party = ["enchant"] preferred-modules = [] diff --git a/doc/whatsnew/fragments/10803.feature b/doc/whatsnew/fragments/10803.feature new file mode 100644 index 0000000000..2baa7911c2 --- /dev/null +++ b/doc/whatsnew/fragments/10803.feature @@ -0,0 +1,3 @@ +Add support for `--known-first-party` similar to `--known-third-party`. + +Refs #10803 diff --git a/examples/pylintrc b/examples/pylintrc index c0fd3e4d35..4d4c276701 100644 --- a/examples/pylintrc +++ b/examples/pylintrc @@ -398,6 +398,9 @@ int-import-graph= # compatibility libraries. known-standard-library= +# Force import order to recognize a module as part of a first party library. +known-first-party= + # Force import order to recognize a module as part of a third party library. known-third-party=enchant diff --git a/examples/pyproject.toml b/examples/pyproject.toml index 98852248b0..c11d973652 100644 --- a/examples/pyproject.toml +++ b/examples/pyproject.toml @@ -350,6 +350,9 @@ max-module-lines = 1000 # libraries. # known-standard-library = +# Force import order to recognize a module as part of a third party library. +# known-first-party = + # Force import order to recognize a module as part of a third party library. known-third-party = [ "enchant" ] diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index b045f74c21..039fd7cc9f 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -318,6 +318,7 @@ def _make_graph( DEFAULT_STANDARD_LIBRARY = () +DEFAULT_KNOWN_FIRST_PARTY = () DEFAULT_KNOWN_THIRD_PARTY = ("enchant",) DEFAULT_PREFERRED_MODULES = () @@ -401,6 +402,16 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): "the standard compatibility libraries.", }, ), + ( + "known-first-party", + { + "default": DEFAULT_KNOWN_FIRST_PARTY, + "type": "csv", + "metavar": "", + "help": "Force import order to recognize a module as part of " + "a first party library.", + }, + ), ( "known-third-party", { @@ -758,6 +769,7 @@ def _isort_config(self) -> isort.Config: # 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_first_party=self.linter.config.known_first_party, known_third_party=self.linter.config.known_third_party, ) diff --git a/tests/functional/w/wrong_import_order2.py b/tests/functional/w/wrong_import_order2.py index 6f68906738..be24704a49 100644 --- a/tests/functional/w/wrong_import_order2.py +++ b/tests/functional/w/wrong_import_order2.py @@ -6,12 +6,16 @@ import os from sys import argv -# external imports +# external/third-party imports import isort import datetime # std import that should be treated as third-party +# First-party imports from six import moves +import pylint +import re # std import that should be treated as first-party + # local_imports from . import my_package from .my_package import myClass diff --git a/tests/functional/w/wrong_import_order2.rc b/tests/functional/w/wrong_import_order2.rc index 5f52571850..a0a4026646 100644 --- a/tests/functional/w/wrong_import_order2.rc +++ b/tests/functional/w/wrong_import_order2.rc @@ -1,2 +1,3 @@ [IMPORTS] +known-first-party=re known-third-party=datetime