diff options
author | Moises Lopez - https://www.vauxoo.com/ <moylop260@vauxoo.com> | 2016-04-30 06:07:15 -0500 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-04-30 14:07:15 +0300 |
commit | 9812c5a49ae430945e9ad5dde7ec523904498805 (patch) | |
tree | f7f6b9972ee055ce8986be6d81209572f15edf38 | |
parent | 2b9afbb95722ef3b76c0649c01a2d8066f812327 (diff) | |
download | pylint-git-9812c5a49ae430945e9ad5dde7ec523904498805.tar.gz |
[REF] import-checkers: Use of isort (#879)
Use the isort library for wrong-import-order checker.
-rw-r--r-- | CONTRIBUTORS.txt | 3 | ||||
-rw-r--r-- | README.rst | 8 | ||||
-rw-r--r-- | pylint/__pkginfo__.py | 1 | ||||
-rw-r--r-- | pylint/checkers/imports.py | 70 | ||||
-rw-r--r-- | pylint/test/functional/import_error.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/reimported.py | 2 | ||||
-rw-r--r-- | pylint/test/functional/super_checks.py | 2 | ||||
-rw-r--r-- | pylint/test/input/func_w0404.py | 2 | ||||
-rw-r--r-- | tox.ini | 2 |
9 files changed, 61 insertions, 31 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 99105e56c..1de7fc424 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -84,6 +84,7 @@ Order doesn't matter (not that much, at least ;) * Łukasz Rogalski: invalid-length-returned -* Moisés López (Vauxoo): Support for deprecated-modules in modules not installed. +* Moisés López (Vauxoo): Support for deprecated-modules in modules not installed, + Refactory wrong-import-order to integrate it with `isort` library. * Luis Escobar (Vauxoo), Moisés López (Vauxoo): Add bad-docstring-quotes and docstring-first-line-empty diff --git a/README.rst b/README.rst index 7159110f4..a36072f94 100644 --- a/README.rst +++ b/README.rst @@ -40,6 +40,14 @@ Installation should be as simple as :: python -m pip install astroid
+Pylint requires isort package (the later the better).
+
+* https://github.com/timothycrosley/isort
+
+Installation should be as simple as ::
+
+ python -m pip install isort
+
If you want to install from a source distribution, extract the tarball and run
the following commands ::
diff --git a/pylint/__pkginfo__.py b/pylint/__pkginfo__.py index e8e748159..24370ecad 100644 --- a/pylint/__pkginfo__.py +++ b/pylint/__pkginfo__.py @@ -29,6 +29,7 @@ version = '.'.join([str(num) for num in numversion]) install_requires = [ 'astroid >= 1.5.0,<1.6.0', 'six', + 'isort >= 4.2.5', ] if sys.platform == 'win32': diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index 9d7291f1e..9a6ea2e6c 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -24,8 +24,8 @@ import six import astroid from astroid import are_exclusive -from astroid.modutils import (get_module_part, is_standard_module, - file_from_modpath) +from astroid.modutils import (get_module_part, is_standard_module) +import isort from pylint.interfaces import IAstroidChecker from pylint.utils import EmptyReport, get_global_option @@ -224,6 +224,11 @@ MSGS = { 'Used when code and imports are mixed'), } + +DEFAULT_STANDARD_LIBRARY = () +DEFAULT_KNOWN_THIRD_PARTY = ('enchant',) + + class ImportsChecker(BaseChecker): """checks for * external modules dependencies @@ -270,6 +275,21 @@ given file (report RP0402 must not be disabled)'} 'help' : 'Create a graph of internal dependencies in the \ given file (report RP0402 must not be disabled)'} ), + ('known-standard-library', + {'default': DEFAULT_STANDARD_LIBRARY, + 'type': 'csv', + 'metavar': '<modules>', + 'help': 'Force import order to recognize a module as part of' \ + ' the standard compatibility libraries.'} + ), + ('known-third-party', + {'default': DEFAULT_KNOWN_THIRD_PARTY, + 'type': 'csv', + 'metavar': '<modules>', + 'help': 'Force import order to recognize a module as part of' \ + ' a third party library.'} + ), + ) def __init__(self, linter=None): @@ -466,7 +486,10 @@ given file (report RP0402 must not be disabled)'} """Record the package `node` imports from""" importedname = importedmodnode.name if importedmodnode else None if not importedname: - importedname = node.names[0][0].split('.')[0] + if isinstance(node, astroid.ImportFrom): + importedname = node.modname + else: + importedname = node.names[0][0].split('.')[0] self._imports_stack.append((node, importedname)) @staticmethod @@ -483,36 +506,31 @@ given file (report RP0402 must not be disabled)'} extern_imports = [] local_imports = [] std_imports = [] + isort_obj = isort.SortImports( + file_contents='', known_third_party=self.config.known_third_party, + known_standard_library=self.config.known_standard_library, + ) for node, modname in self._imports_stack: package = modname.split('.')[0] - if is_standard_module(modname): + import_category = isort_obj.place_module(package) + if import_category in ('FUTURE', 'STDLIB'): std_imports.append((node, package)) wrong_import = extern_imports or local_imports - if not wrong_import: - continue if self._is_fallback_import(node, wrong_import): continue - self.add_message('wrong-import-order', node=node, - args=('standard import "%s"' % node.as_string(), - '"%s"' % wrong_import[0][0].as_string())) - else: - try: - filename = file_from_modpath([package]) - except ImportError: - continue - if not filename: - continue - - filename = os.path.normcase(os.path.abspath(filename)) - if not any(filename.startswith(path) for path in self._site_packages): - local_imports.append((node, package)) - continue + if wrong_import: + self.add_message('wrong-import-order', node=node, + args=('standard import "%s"' % node.as_string(), + '"%s"' % wrong_import[0][0].as_string())) + elif import_category in ('FIRSTPARTY', 'THIRDPARTY'): extern_imports.append((node, package)) - if not local_imports: - continue - self.add_message('wrong-import-order', node=node, - args=('external import "%s"' % node.as_string(), - '"%s"' % local_imports[0][0].as_string())) + wrong_import = local_imports + if wrong_import: + self.add_message('wrong-import-order', node=node, + args=('external import "%s"' % node.as_string(), + '"%s"' % wrong_import[0][0].as_string())) + elif import_category == 'LOCALFOLDER': + local_imports.append((node, package)) return std_imports, extern_imports, local_imports def _get_imported_module(self, importnode, modname): diff --git a/pylint/test/functional/import_error.py b/pylint/test/functional/import_error.py index de72e2fcd..8f7bb3f30 100644 --- a/pylint/test/functional/import_error.py +++ b/pylint/test/functional/import_error.py @@ -1,5 +1,5 @@ """ Test that import errors are detected. """
-# pylint: disable=invalid-name, unused-import, no-absolute-import, bare-except, broad-except
+# pylint: disable=invalid-name, unused-import, no-absolute-import, bare-except, broad-except, wrong-import-order
import totally_missing # [import-error]
try:
diff --git a/pylint/test/functional/reimported.py b/pylint/test/functional/reimported.py index 3651fa63d..b73f1ce08 100644 --- a/pylint/test/functional/reimported.py +++ b/pylint/test/functional/reimported.py @@ -1,4 +1,4 @@ -# pylint: disable=missing-docstring,unused-import,import-error, wildcard-import,unused-wildcard-import,redefined-builtin,no-name-in-module,ungrouped-imports
+# pylint: disable=missing-docstring,unused-import,import-error, wildcard-import,unused-wildcard-import,redefined-builtin,no-name-in-module,ungrouped-imports,wrong-import-order
from time import sleep, sleep # [reimported]
from lala import missing, missing # [reimported]
diff --git a/pylint/test/functional/super_checks.py b/pylint/test/functional/super_checks.py index 2959d0023..42a933386 100644 --- a/pylint/test/functional/super_checks.py +++ b/pylint/test/functional/super_checks.py @@ -1,4 +1,4 @@ -# pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring, wrong-import-position,invalid-name +# pylint: disable=too-few-public-methods,import-error, no-absolute-import,missing-docstring, wrong-import-position,invalid-name, wrong-import-order """check use of super""" from unknown import Missing diff --git a/pylint/test/input/func_w0404.py b/pylint/test/input/func_w0404.py index 5111c6036..2ce7d651c 100644 --- a/pylint/test/input/func_w0404.py +++ b/pylint/test/input/func_w0404.py @@ -9,7 +9,7 @@ from xml.etree import ElementTree from email import encoders import email.encoders -import sys #pylint: disable=ungrouped-imports +import sys #pylint: disable=ungrouped-imports,wrong-import-order __revision__ = 0 def no_reimport(): @@ -5,6 +5,7 @@ skip_missing_interpreters = true [testenv:pylint] deps = git+https://github.com/PyCQA/astroid@master + isort commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/pylint @@ -12,6 +13,7 @@ commands = pylint -rn --rcfile={toxinidir}/pylintrc {envsitepackagesdir}/pylint deps = git+https://github.com/PyCQA/astroid@master coverage + isort setenv = COVERAGE_FILE = {toxinidir}/.coverage.{envname} |