Isort: Regression with top_level import and force_sort_within_sections

Created on 11 Oct 2019  路  5Comments  路  Source: PyCQA/isort

With 4.3.21 this is not changed.

from rest_framework import throttling, viewsets
from rest_framework.authentication import TokenAuthentication

But with develop the order is swapped.

This is due to this commit https://github.com/timothycrosley/isort/commit/752c0614c410bb609db97adc6b42c25957c9074c.

@stpierre I don't understand why this order is desired.

from third_party import lib_a
from third_party import lib_b
from third_party.lib_c import lib1
from third_party import lib_d

I would like all third_party imports to be before third_party.lib_c.

It isn't be sorting by the package or module that is being imported from. It's not sorting by the import itself.

This is not the output with force_sort_within_sections=False.

Should there be another config value to sort by this method?

All 5 comments

The goal of that order is to sort lexicographically. This corresponds to the Google Python style guide (https://github.com/google/styleguide/blob/gh-pages/pyguide.md#313-imports-formatting), which states:

Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module's full package path.

(Emphasis mine.)

This also corresponds to the OpenStack style guide (https://docs.openstack.org/hacking/latest/user/hacking.html); although it's not made explicit by their examples, it is enforced by their flake8 plugin (specifically rule H306).

I'm fine with adding another option, but the sort order that my change presents is the sort order preferred by the most-used Python style guides.

Closing this as both behaviours are supported now in develop branch which will be released soon as 5.0.0

Thanks!

~Timothy

Awesome! What is the new flag for this behaviour?

lexicographical

See example usage in OpenStack profile here: https://github.com/timothycrosley/isort/blob/develop/isort/profiles.py#L22

Eagerly

I mean eagerly

EAGERLY

Awaiting 5.0.0

:slightly_smiling_face:

Was this page helpful?
0 / 5 - 0 ratings