Skip to content

lines_before_import seems to not always work on files #1855

@robsdedude

Description

@robsdedude

Here is the minimal example is created:

import isort

str_in = """# comment
from foo import bar


def main():
    ...
"""

str_out = isort.code(
    str_in,
    lines_before_imports=1
)

with open("tmp2.py", "w") as fd:
    fd.write(str_in)
isort.file(
    "tmp2.py",
    lines_before_imports=1
)

print("In:")
print(str_in)
print("-" * 79)
print("Out String:")
print(str_out)
print("-" * 79)
print("Out file:")
with open("tmp2.py", "r") as fd:
    print(fd.read())

Which yields

In:
# comment
from foo import bar


def main():
    ...

-------------------------------------------------------------------------------
Out String:
# comment

from foo import bar


def main():
    ...

-------------------------------------------------------------------------------
Out file:
# comment
from foo import bar


def main():
    ...

So interestingly enough, it seems like lines_before_import does not affect files in this case. Is there a config option that I did not find?
Another observation I made, If I change the input sting in a way that isort wants to change it for other reasons than lines_before_import, files and strings behave the same way. E.g., changing from foo import bar to from foo import (bar,) makes the outputs be the same.

This might or might not be related to #1854

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions