PyCQA / flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
GNU Lesser General Public License v3.0
278 stars 72 forks source link

feat: implement PyCharm import style #138

Closed LKI closed 6 years ago

LKI commented 6 years ago

PyCharm's import settings is closest to Smarkets, but it's sorting is case sensitive. So this PR implements a new style for PyCharm only.

But I'm not sure if it's appropriate to put a specific IDE import style here.

Please leave comment if any question.

pgjones commented 6 years ago

Could you link to something that states PyCharm uses this style?

labrys commented 6 years ago

There is nothing that specifically states that the import order is case sensitive in the docs, but I can confirm with empirical usage that it does sort capital letters before lowercase. References for available options below:

https://www.jetbrains.com/help/pycharm/code-style-python.html

OPTIONS

Sort import statements

Select or clear this checkbox to enable or disable sorting imports within individual import groups according to PEP 8.

Sort imported names in "from" imports

If this checkbox is selected, the imports in the from ... import ... statements are sorted alphabetically

# Selected
from sys import modules, path, version

# Not selected
from sys import version, path, modules

Sort plain and "from" imports separately within a group

If this checkbox is not selected, the imports from the same module, regardless of their type, are grouped together, but so that import statements go first, and from ... import ... statements go next. If this checkbox is selected, the imports are at first sorted by there type (first import, next from ... import ...), and then alphabetically.

# Selected
import os
import sys
from os import getenv
from sys import path

# Not selected
import os
from os import getenv
import sys
from sys import path

The option Sort plain and 'from' imports separately within a group corresponds to the option force_alphabetical_sort of the utility isort.

Also, this sorting order corresponds to Google Python Style Guide

Join "from" imports with the same source

If this checkbox is selected, the "from" imports of the same source are combined.

# Selected
from os.path import join, relpath, abspath, dirname, basename

# Not selected
from os.path import join
from os.path import relpath, abspath
from os.path import dirname, basename

The option Join "from" imports with the same source corresponds to the combine_as option of the the utility isort

LKI commented 6 years ago

thanks @labrys

I can't find the rules in official document neither. 😸

labrys commented 6 years ago

Personally I think it would be nice to be able to just add an option for all the styles that allows you to choose case sensitivity.