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

Relative import sorting order #417

Closed
lee-kagiso opened this issue Apr 1, 2016 · 4 comments
Closed

Relative import sorting order #417

lee-kagiso opened this issue Apr 1, 2016 · 4 comments
Labels
enhancement New feature or request

Comments

@lee-kagiso
Copy link

Is there a setting to change the sort order of relative imports from

from .. import x
from . import y

to

from . import y
from ..import x

so that one can comply with the Google style guide as per this linter https://github.com/public/flake8-import-order#configuration?

@timothycrosley
Copy link
Member

Hi @lee-kagiso,

Thanks for bringing this to my attention! This is not something isort currently supports, but I'll work on adding support for this in the next release.

Thanks!

~Timothy

@timothycrosley
Copy link
Member

This is fixed as the default (no config necessary) and will be pushed into a new release today.

Thanks!

~Timothy

@jpgrayson
Copy link
Contributor

Let me start by saying thank you for isort. It has made a positive impact on my personal and team's workflows!

Although the referenced flake8-import-order plugin may reject the descending order for relative imports, AFAICT the Google Python Style Guide makes no assertions about order of relative imports. In fact, it prohibits using relative imports at all:

Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice.

To be clear, by descending order, I mean:

from ...baz import x
from ..foo import y
from .bar import z

There are at least two arguments for isort to default to descending order for relative imports:

  1. The new ascending order is a regression from isort's previous behavior for imports of the form from ..module import name, which more common in my projects than the from .. import module form.
  2. The spirit of PEP-8 is to order imports from "least local" to "most local". Imports from ... are less local than those from .., and imports from . are most local. Descending relative imports achieves this ordering.

The original intent of this issue was to add an option to select the order of relative imports. I suggest that such an option remains a valid request for isort.

@jaap3
Copy link

jaap3 commented Feb 27, 2019

I agree with @jpgrayson, with the recent release of isort it suddenly started reversing imports that were perfectly fine before.

jpgrayson added a commit to jpgrayson/isort that referenced this issue Mar 1, 2019
This new option changes the sort order of relative imports. The new default
sort order is from furthest to closest (most dots to least dots):

    from ... import a
    from ...foo import x
    from .. import b
    from ..bar import y
    from . import c
    from .baz import z

With the new -rr/--reverse-relative option, the order of the relative
import *levels* (inter-order) is reversed, but the ordering within each
relative import level (intra-order) remains the same (i.e. normal lexical
order).

    from . import c
    from .baz import z
    from .. import b
    from ..bar import y
    from ... import a
    from ...foo import x

Signed-off-by: Peter Grayson <pete@jpgrayson.net>
timothycrosley added a commit that referenced this issue Mar 2, 2019
Add -rr/--reverse-relative option (issue #417)
timothycrosley pushed a commit that referenced this issue Mar 3, 2019
liZe referenced this issue in Kozea/WeasyPrint Mar 10, 2019
ansible-zuul bot pushed a commit to ansible/ansible-navigator that referenced this issue Jan 24, 2022
Change the order of relative imports

This modifies the order of relative import statements.
Fixes: #780
Prior to this change the order was
import .sibling
import ..parent
import ...grandparent

after this change
import ...grandparent
import ..parent
import .sibling

The resulting order is the default for isort which decreases the amount of custom configuration within the repo and results in a sorting order starting with "least local" to "most local".
This follow on work after #777, which was intended to minimize the changes in the repo related to the cleanup. #779 was also implemented to help detect any issues arising from a change in order.
Some conversation and history about the ordering can be found here: PyCQA/isort#417

Reviewed-by: Sviatoslav Sydorenko <webknjaz+github/profile@redhat.com>
Reviewed-by: None <None>
cidrblock added a commit to cidrblock/ansible-navigator that referenced this issue Jan 24, 2022
Change the order of relative imports

This modifies the order of relative import statements.
Fixes: ansible#780
Prior to this change the order was
import .sibling
import ..parent
import ...grandparent

after this change
import ...grandparent
import ..parent
import .sibling

The resulting order is the default for isort which decreases the amount of custom configuration within the repo and results in a sorting order starting with "least local" to "most local".
This follow on work after ansible#777, which was intended to minimize the changes in the repo related to the cleanup. ansible#779 was also implemented to help detect any issues arising from a change in order.
Some conversation and history about the ordering can be found here: PyCQA/isort#417

Reviewed-by: Sviatoslav Sydorenko <webknjaz+github/profile@redhat.com>
Reviewed-by: None <None>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants