Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make C0412: Imports from package are not grouped, compatible with isort #2806

Closed
Pierre-Sassoulas opened this issue Mar 9, 2019 · 4 comments · Fixed by #2824
Closed

Make C0412: Imports from package are not grouped, compatible with isort #2806

Pierre-Sassoulas opened this issue Mar 9, 2019 · 4 comments · Fixed by #2824
Labels
Enhancement ✨ Improvement to a component

Comments

@Pierre-Sassoulas
Copy link
Member

Description

C0412 and isort are incompatible when there is a mix of from x import y and import x. For example this is what isort want :

import astroid
import isort
from astroid import are_exclusive, decorators
from astroid.modutils import get_module_part, is_standard_module

But Pylint output a C0412 for the previous code and would want something like this :

import isort
import astroid
from astroid import are_exclusive, decorators
from astroid.modutils import get_module_part, is_standard_module

Possible solutions

  • We could modify C0412 and take grouped import into account by import type (import x styled import grouped together, and from x import y styled import grouped together, instead of all import from a package grouped together).
  • Or we could still take this problem into account but by displaying another message than C0412 (Possibly : "Mixed type of importation, choose a style between import x and and from x import y").
@PCManticore
Copy link
Contributor

@Pierre-Sassoulas I'd be inclined to have the first solution. Also with what versions are you reproducing this bug? I can't with latest pylint and isort.

@PCManticore PCManticore added the Enhancement ✨ Improvement to a component label Mar 19, 2019
@Pierre-Sassoulas
Copy link
Member Author

Pierre-Sassoulas commented Mar 19, 2019

Isort was version 4.3.13 and pylint latest unreleased from last week (2.4.0 ?). I was able to reproduce with pylint 2.3.0 and isort 4.3.15 :

$ cat isort_compatible.py 
import astroid
import isort
from astroid import are_exclusive, decorators
from astroid.modutils import get_module_part, is_standard_module

$ cat pylint_compatible.py 
import isort
import astroid
from astroid import are_exclusive, decorators
from astroid.modutils import get_module_part, is_standard_module

$ isort isort_compatible.py --check-only 
# Nothing happen so it's sorted according to isort
$ ✘ isort pylint_compatible.py --check-only 
ERROR: /home/pierre/workspace/pylint/pylint_compatible.py Imports are incorrectly sorted.
 $ ✘ pylint isort_compatible.py
************* Module isort_compatible
isort_compatible.py:1:0: W0611: Unused import astroid (unused-import)
isort_compatible.py:2:0: W0611: Unused import isort (unused-import)
isort_compatible.py:3:0: W0611: Unused are_exclusive imported from astroid (unused-import)
isort_compatible.py:3:0: W0611: Unused decorators imported from astroid (unused-import)
isort_compatible.py:4:0: W0611: Unused get_module_part imported from astroid.modutils (unused-import)
isort_compatible.py:4:0: W0611: Unused is_standard_module imported from astroid.modutils (unused-import)
isort_compatible.py:3:0: C0412: Imports from package astroid are not grouped (ungrouped-imports)

------------------------------------
Your code has been rated at -7.50/10
$  ✘ pylint pylint_compatible.py 
************* Module pylint_compatible
pylint_compatible.py:1:0: W0611: Unused import isort (unused-import)
pylint_compatible.py:2:0: W0611: Unused import astroid (unused-import)
pylint_compatible.py:3:0: W0611: Unused are_exclusive imported from astroid (unused-import)
pylint_compatible.py:3:0: W0611: Unused decorators imported from astroid (unused-import)
pylint_compatible.py:4:0: W0611: Unused get_module_part imported from astroid.modutils (unused-import)
pylint_compatible.py:4:0: W0611: Unused is_standard_module imported from astroid.modutils (unused-import)

------------------------------------
Your code has been rated at -5.00/10

@PCManticore
Copy link
Contributor

Thanks @Pierre-Sassoulas I can reproduce it as well with the latest isort. As mentioned, I'm in favour of first option.

@Pierre-Sassoulas
Copy link
Member Author

This seems easier than refactoring the message store. Well a LOT easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants