PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.53k stars 583 forks source link

Sort by length doesn't really sort by length #2304

Open scarere opened 4 days ago

scarere commented 4 days ago

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,
)