Skip to content
Snippets Groups Projects
Commit 7f6bae95 authored by hippo91's avatar hippo91 Committed by Claudiu Popa
Browse files

Separate the handling of third party and first party imports

parent 019dd8af
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -146,6 +146,10 @@ What's New in Pylint 1.8?
deeply nested inside loop.
Close #1661
 
* Fix no ``wrong-import-order`` message emitted on ordering of first and third party
libraries. With this fix, pylint distinguishes third and first party
modules when checking import order.
Close #1702
 
What's New in Pylint 1.7.1?
=========================
Loading
Loading
Loading
Loading
@@ -351,5 +351,9 @@ Other Changes
 
* The Python 3 porting checker respects disabled checkers found in the config file.
 
* Modules, classes, or methods consist of compound statements that exceed the ``docstring-min-length``
are now correctly emitting `missing-docstring`
* Modules, classes, or methods consist of compound statements that exceed the ``docstring-min-length``
are now correctly emitting `missing-docstring`
* Fix no ``wrong-import-order`` message emitted on ordering of first and third party libraries.
With this fix, pylint distinguishes first and third party modules when checking
import order.
# -*- coding: utf-8 -*-
# Copyright (c) 2006-2015 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2012-2014 Google, Inc.
# Copyright (c) 2014-2016 Claudiu Popa <pcmanticore@gmail.com>
Loading
Loading
@@ -563,10 +564,14 @@ class ImportsChecker(BaseChecker):
 
Imports must follow this order: standard, 3rd party, local
"""
extern_imports = []
local_imports = []
std_imports = []
extern_not_ignored = []
third_party_imports = []
first_party_imports = []
# need of a list that holds third or first party ordered import
external_imports = []
local_imports = []
third_party_not_ignored = []
first_party_not_ignored = []
local_not_ignored = []
isort_obj = isort.SortImports(
file_contents='', known_third_party=self.config.known_third_party,
Loading
Loading
@@ -581,29 +586,42 @@ class ImportsChecker(BaseChecker):
ignore_for_import_order = not self.linter.is_message_enabled('wrong-import-order',
node.fromlineno)
import_category = isort_obj.place_module(package)
node_and_package_import = (node, package)
if import_category in ('FUTURE', 'STDLIB'):
std_imports.append((node, package))
wrong_import = extern_not_ignored or local_not_ignored
std_imports.append(node_and_package_import)
wrong_import = (third_party_not_ignored or first_party_not_ignored
or local_not_ignored)
if self._is_fallback_import(node, wrong_import):
continue
if wrong_import and not nested:
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))
elif import_category == 'THIRDPARTY':
third_party_imports.append(node_and_package_import)
external_imports.append(node_and_package_import)
if not nested and not ignore_for_import_order:
third_party_not_ignored.append(node_and_package_import)
wrong_import = first_party_not_ignored or local_not_ignored
if wrong_import and not nested:
self.add_message('wrong-import-order', node=node,
args=('third party import "%s"' % node.as_string(),
'"%s"' % wrong_import[0][0].as_string()))
elif import_category == 'FIRSTPARTY':
first_party_imports.append(node_and_package_import)
external_imports.append(node_and_package_import)
if not nested and not ignore_for_import_order:
extern_not_ignored.append((node, package))
first_party_not_ignored.append(node_and_package_import)
wrong_import = local_not_ignored
if wrong_import and not nested:
self.add_message('wrong-import-order', node=node,
args=('external import "%s"' % node.as_string(),
args=('first party import "%s"' % node.as_string(),
'"%s"' % wrong_import[0][0].as_string()))
elif import_category == 'LOCALFOLDER':
local_imports.append((node, package))
if not nested and not ignore_for_import_order:
local_not_ignored.append((node, package))
return std_imports, extern_imports, local_imports
return std_imports, external_imports, local_imports
 
def _get_imported_module(self, importnode, modname):
try:
Loading
Loading
"""Checks import order rule"""
# pylint: disable=unused-import,relative-import,ungrouped-imports,import-error,no-name-in-module,relative-beyond-top-level
from __future__ import absolute_import
try:
from six.moves import configparser
except ImportError:
Loading
Loading
wrong-import-order:11::standard import "import os.path" should be placed before "import six"
wrong-import-order:13::standard import "import sys" should be placed before "import six"
wrong-import-order:14::standard import "import datetime" should be placed before "import six"
wrong-import-order:17::external import "import totally_missing" should be placed before "from .package import Class"
wrong-import-order:19::external import "import astroid" should be placed before "from .package import Class"
wrong-import-order:12::standard import "import os.path" should be placed before "import six"
wrong-import-order:14::standard import "import sys" should be placed before "import six"
wrong-import-order:15::standard import "import datetime" should be placed before "import six"
wrong-import-order:18::first party import "import totally_missing" should be placed before "from .package import Class"
wrong-import-order:20::third party import "import astroid" should be placed before "import unused_import"
Loading
Loading
@@ -7,7 +7,7 @@ import os
from sys import argv
 
# external imports
import lxml
import isort
 
from six import moves
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment