Skip to content

Commit

Permalink
Feat - Makes C0412 (ungrouped-imports) compatible with isort
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Mar 21, 2019
1 parent ec2eb68 commit fdae04e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,6 @@ contributors:
* Bluesheeptoken: contributor

* Michael Scott Cuthbert: contributor

* Pierre Sassoulas : contributor
- Made C0412 (ungrouped import) compatible with isort
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Release date: TBA

Close #2816

* C0412 (ungrouped-import) is now compatible with isort.

Close #2806


What's New in Pylint 2.3.0?
===========================

Expand Down
12 changes: 12 additions & 0 deletions doc/whatsnew/2.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ Other Changes
command line. In addition to the ``--from-stdin`` flag a (single) file
name needs to be specified on the command line, which is needed for the
report.

* The checker for ungrouped imports is now more permissive.

The import can now be sorted alphabetically by import style.
This makes pylint compatible with isort.

This does not trigger a C0412 anymore and is OK ::

import unittest
import subprocess
from unittest import TestCase
from unittest.mock import MagicMock
9 changes: 7 additions & 2 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,19 @@ def leave_module(self, node):
# Check imports are grouped by category (standard, 3rd party, local)
std_imports, ext_imports, loc_imports = self._check_imports_order(node)

# Check imports are grouped by package within a given category
met = set()
# Check that imports are grouped by package within a given category
met_import = set() #  set for 'import x' style
met_from = set() #  set for 'from x import y' style
current_package = None
for import_node, import_name in std_imports + ext_imports + loc_imports:
if not self.linter.is_message_enabled(
"ungrouped-imports", import_node.fromlineno
):
continue
if isinstance(import_node, astroid.node_classes.ImportFrom):
met = met_from
else:
met = met_import
package, _, _ = import_name.partition(".")
if current_package and current_package != package and package in met:
self.add_message("ungrouped-imports", node=import_node, args=package)
Expand Down
8 changes: 7 additions & 1 deletion pylint/test/functional/ungrouped_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
from os import pardir
import scipy
from os import sep
import astroid # [ungrouped-imports]
from astroid import exceptions # [ungrouped-imports]
if True:
import logging.handlers # [ungrouped-imports]
from os.path import join # [ungrouped-imports]
# Test related to compatibility with isort:
# We check that we do not create error with the old way pylint was handling it
import subprocess
import unittest
from unittest import TestCase
from unittest.mock import MagicMock
6 changes: 6 additions & 0 deletions pylint/test/functional/ungrouped_imports_isort_compatible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Checks import order rule with imports that isort could generate"""
# pylint: disable=unused-import
import astroid
import isort
from astroid import are_exclusive, decorators
from astroid.modutils import get_module_part, is_standard_module
Empty file.

0 comments on commit fdae04e

Please sign in to comment.