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

Adds length_sort_straight option #1373

Merged

Conversation

sztamas
Copy link
Collaborator

@sztamas sztamas commented Aug 11, 2020

Does this look OK as a solution to resolve #1232?

Added a new option that length sorts just the straight imports. It is off by default.
See it in action below with the examples of the OP:

▶ cat t.py            
from datetime import timedelta
from contextlib import ExitStack

import os
import datetime
import sys

▶ poetry run isort -d t.py --length-sort-straight
import os
import sys
import datetime
from contextlib import ExitStack
from datetime import timedelta

def test_length_sort_section() -> None:
"""Test setting isort to sort on length instead of alphabetically for a specific section."""
test_input = (
"import medium_sizeeeeeeeeeeeeee\n"
"import shortie\n"
"import datetime\n"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this test as os and sys sort the same in both alphabetical and length sort mode. Added datetime that would be first in alphabetical, but last in length sort mode.

@codecov-commenter
Copy link

Codecov Report

Merging #1373 into develop will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##           develop     #1373   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           36        36           
  Lines         2475      2477    +2     
=========================================
+ Hits          2475      2477    +2     

@timothycrosley
Copy link
Member

This looks like a great solution to me! Simple, effective, and solves the ask completely.

Thank you!

@timothycrosley timothycrosley merged commit 45fdfe5 into PyCQA:develop Aug 12, 2020
@warsaw
Copy link

warsaw commented Aug 12, 2020

Looks great to me too[*], thank you!

[*] With a blank line between the straights and the froms.

@sztamas sztamas deleted the issue/1232/length_sort_straight branch August 12, 2020 15:33
@timothycrosley
Copy link
Member

@warsaw to get a blank line between the two this can be combined with the lines-between-types option in isort 5.4.0+:

Example based on above:

isort -d t.py --length-sort-straight --lines-between-types 1

@warsaw
Copy link

warsaw commented Aug 14, 2020

It's exactly what I want. I'm ecstatic! Here's my setup.cfg for isort:

[tool:isort]
length_sort_straight = true
lines_after_imports = 2
lines_between_types = 1
multi_line_output = 4
order_by_type = false
skip = conf.py
known_first_party = flufl

You rock @timothycrosley Thank you!

@warsaw
Copy link

warsaw commented Aug 14, 2020

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

Successfully merging this pull request may close these issues.

length_sort separately from regular imports and from...imports
4 participants