isort icon indicating copy to clipboard operation
isort copied to clipboard

Sort by length doesn't really sort by length

Open scarere opened this issue 1 year ago • 2 comments

Please see the following issue that I raised with ruff: issue

The gist is I can't get isort to truly sort by length as for statements that use the from keyword to import modules, eg:

from package.subpackage.module import function

It does not count anything including or after the import statement towards the line length. For example, when sorting by line length the output would be something like this.

from package.short.path import more_functions, another_function
from package.with.long.path import fn

I'm not sure if this is intended but for me personally it seems to make more sense that the total line length should be taken into account, not just the length of the module.

P.S. As a side note, as a possible new feature, it would be really great if there was an option to import statements that span multiple lines at the bottom of the section, sort of in a subsection. I say subsection because ideally the main sections could have line breaks but the subsections wouldn't. Something like the following would be really cool

import x
from x import y
from x.subpackage.module import function1
from x.module import (
    function2,
    function3,
    function4,
    function5,
)

import k  # New section
from k.subpackage.module import j
from k.module import (
    fn1,
    fn2,
    fn3,
)

scarere avatar Nov 20 '24 15:11 scarere

It doesn't appear that isort sorts by line length. This may be difference between expectations and what isort supports.

Please close this issue to reduce the number of issues to address in the backlog. Thanks!

kurtmckee avatar Jan 06 '25 19:01 kurtmckee

Can we change this to a feature request then? I think most people would expect the same behaviour as I did for sort by length. Not sure in what instances the current behaviour of isort sort by length would be desired

scarere avatar Jan 07 '25 16:01 scarere