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

--float-to-top has no effect if no import is already at the top #1382

Closed
pmeier opened this issue Aug 13, 2020 · 3 comments · Fixed by #1452
Closed

--float-to-top has no effect if no import is already at the top #1382

pmeier opened this issue Aug 13, 2020 · 3 comments · Fixed by #1452
Labels
bug Something isn't working
Milestone

Comments

@pmeier
Copy link

pmeier commented Aug 13, 2020

Consider the following snippet:

def foo():
    pass

import a

def bar():
    pass

If you run isort --float-to-top nothing is changed. If you insert import b above the definition of foo, the same command correctly results in

import a
import b


def foo():
    pass


def bar():
    pass
@timothycrosley timothycrosley modified the milestones: 5.3.0, 5.5.0 Aug 15, 2020
@sztamas
Copy link
Collaborator

sztamas commented Aug 26, 2020

@timothycrosley I had a look at this.

Currently the --float-to-top is achieved by this upfront parse and sort that rearranges the input lines and floats up the imports to the top.
However this works only if there are any imports at the top to start with.

I think that when config.float_to_top is True, import_index returned by parse could be modified to return an index at the top of the file.

However, the top of the file probably shouldn't be 0, we should skip over any docstrings and comments?

From PEP8:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

What do you think?

Thanks,

Tamas

@timothycrosley
Copy link
Member

That's right! And indeed back before isort supported multiple sections, it did do exactly this - so there may be some prior work to pull from

@timothycrosley timothycrosley added the bug Something isn't working label Aug 28, 2020
timothycrosley added a commit that referenced this issue Sep 3, 2020
@timothycrosley
Copy link
Member

A fix for this issue has just been deployed to PyPI in version 5.5.0 of isort.

Thanks!

~Timothy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants