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 causes imports to appear above a module's docstring #1499

Closed
willfrey opened this issue Sep 25, 2020 · 5 comments · Fixed by #1501, hypothesis/viahtml#107 or wwade/jobrunner#63
Labels
bug Something isn't working

Comments

@willfrey
Copy link

With default settings only float-to-top enabled, imports end up getting placed above a module's docstring.

For example, I don't expect the following code block to change when I run isort over it with float-to-top enabled.

"""I'm a module-level docstring."""

import this

However, the code snippet will be reformatted with the import above the docstring.

import this

"""I'm a module-level docstring."""

Single-line comments starting with a # appear to be unaffected and will remain at the top, so shebangs and encoding comments are safe.

Is this intentional behavior? If so, is there any way to disable this?

Thank you!

@char101
Copy link

char101 commented Sep 25, 2020

Temporary fix is to add

and not line.strip().startswith('"""')

after line 208 in https://github.com/PyCQA/isort/blob/develop/isort/parse.py#L208

@char101
Copy link

char101 commented Sep 25, 2020

Also I think it would be better if there is an empty line after the module comment and the imports.

These change seems to handle it (it preserve existing empty lines):

        if skipping_line:
            out_lines.append(line)
            continue
        elif (
            config.float_to_top
            and import_index == -1
            and line
            and not in_quote
            and not line.strip().startswith("#")
            and not line.strip().startswith('"""')
            and not line.strip() != ""
        ):
            import_index = index - 1
            while import_index and not in_lines[import_index - 1]:
                import_index -= 1

@timothycrosley
Copy link
Member

@willfrey Thank you for reporting! This is fixed in develop and will be released in the 5.6.0 release of isort slated for early October.

~Timothy

@willfrey
Copy link
Author

Thank you for fixing this!

@timothycrosley
Copy link
Member

Update: this has just been released to PyPI in 5.6.0 release of isort: https://pycqa.github.io/isort/CHANGELOG/#560-october-7-2020

Thanks!

~Timothy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment