Isort: Float-to-top causes imports to appear above a module's docstring

Created on 25 Sep 2020  路  5Comments  路  Source: PyCQA/isort

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!

bug

Most helpful comment

@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

All 5 comments

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

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

@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

Thank you for fixing this!

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

Was this page helpful?
0 / 5 - 0 ratings