Isort: length_sort separately from regular imports and from...imports

Created on 17 Jun 2020  路  4Comments  路  Source: PyCQA/isort

A style I adopted long ago (from Guido IIRC) was to sort the regular imports by length first then alphabetical, while sorting from...imports alphabetically only. E.g.

import os
import sys
import datetime

from contextlib import ExitStack
from datetime import timedelta

However this doesn't seem possible, since length_sort is a single configuration for both sections. If I set length_sort = false I get:

# These are sorted in the wrong order.
import datetime
import os
import sys

# These are sorted correctly.
from contextlib import ExitStack
from datetime import timedelta

and if I set length_sort = true I get:

# These are sorted correctly.
import os
import sys
import datetime

# These are sorted in the wrong order.
from datetime import timedelta
from contextlib import ExitStack

Neither of which I actually want. Can you add a setting to split regular import sorting from from...import sorting?

Thanks for a great tool!

enhancement help wanted

All 4 comments

This also affects sorting within multiline from...imports:

I want this:

from flufl.lock._lockfile import (
    AlreadyLockedError, Lock, LockError, NotLockedError, SEP, TimeOutError)

But I get this:

from flufl.lock._lockfile import (
    SEP, Lock, LockError, TimeOutError, NotLockedError, AlreadyLockedError)

I'm def open to supporting this! Will have to be post 5.0.0 as I'm pretty heads down getting that out the door. Then I'll revisit. Thanks for the suggestion!

Sure thing! Thanks for the great tool!

This has been deployed in release 5.4.0

Thanks!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings