Skip to content

Add support for PyCharm style alphabetic sorting (edited from: all upper cases name is sorted top most) #890

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

Closed
yyscamper opened this issue Mar 8, 2019 · 8 comments
Labels
enhancement New feature or request

Comments

@yyscamper
Copy link

Below is the sorted result using my isort configuration, you could see the both 'BALL' and RC is placed at the top most. The name Action is placed below 'BALL' and 'RC' is not correct for me:

from pkg import BALL
from pkg import RC
from pkg import Action
from pkg import Bacoo
from pkg import RCNewCode
from pkg import actual
from pkg import rc
from pkg import recorder

What I expected is:

from pkg import Action
from pkg import BALL
from pkg import Bacoo
from pkg import RC
from pkg import RCNewCode
from pkg import actual
from pkg import rc
from pkg import recorder

That means following the default python string odering:

>>> 'BALL' < 'RC' < 'Action'
False
>>> 'Action' < 'BALL' < 'RC'
True

So I wonder whether there is any configuration can make it?

Below is my isort configuration:

[isort]
line_length = 80
indent = '  '
force_single_line = True
known_first_party=test
@yyscamper
Copy link
Author

Seems I find the root cause from #370, @timothycrosley replied:

When doing from imports isort wants to separate CONSTANTS, Classes, and variables from each-other

So whether isort provides any configuration to disable such kind of separation?

@timothycrosley
Copy link
Member

Hi @yyscamper!

Yes, you can configure this! The default setting which causes this to happen is:

order_by_type

which defaults to True. Simply setting order_by_type to False in a config file or doing --dont-order-by-type on the command line should give you the behavior that you want.

Thanks!

~Timothy

@timothycrosley
Copy link
Member

Also, you can find a reasonably complete definition of all these settings here: https://github.com/timothycrosley/isort/wiki/isort-Settings.

Thanks!

~Timothy

@yyscamper
Copy link
Author

yyscamper commented Mar 8, 2019

this is the result after disabling order_by_type, the order for Bacoo and BALL , RC, rc, RCNewBase is still not correct. Any reason? @timothycrosley

from pkg import Action
from pkg import actual
from pkg import Bacoo
from pkg import BALL
from pkg import RC
from pkg import rc
from pkg import RCNewCode
from pkg import recorder

@timothycrosley
Copy link
Member

Currently, when sorting without type ordering, isort ignores the casing of the strings, and gives you a fully alphabetic sort. Is your intention to have an non-typed, but case aware sort?

@yyscamper
Copy link
Author

@timothycrosley yes, that's what I want. Most people in my team uses PyCharm, this is the default behavior for PyCharm.

@timothycrosley timothycrosley reopened this Mar 8, 2019
@timothycrosley timothycrosley changed the title all upper cases name is sorted top most Add support for PyCharm style alphabetic sorting (edited from: all upper cases name is sorted top most) Mar 8, 2019
timothycrosley added a commit that referenced this issue Mar 8, 2019
timothycrosley added a commit that referenced this issue Mar 8, 2019

Verified

This commit was signed with the committer’s verified signature. The key has expired.
danielleadams Danielle Adams
Fix issue #890
timothycrosley added a commit that referenced this issue Mar 8, 2019

Verified

This commit was signed with the committer’s verified signature.
richardlau Richard Lau
@timothycrosley
Copy link
Member

Hi @yyscamper,

I've added support for this in the lasted version of isort via the case_sensitive setting, as shown in the test uploaded to the pull request:

https://github.com/timothycrosley/isort/pull/892/files#diff-e82bf52c272f12dee456325bfcb553d8R2850

Thanks!

~Timothy

@timothycrosley timothycrosley added the enhancement New feature or request label Mar 8, 2019
@yyscamper
Copy link
Author

@timothycrosley I just tested using latest version, it works. Thank for you quick action on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants