isort icon indicating copy to clipboard operation
isort copied to clipboard

`lines_before_import` seems to not always work on files

Open robsdedude opened this issue 2 years ago • 0 comments

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 https://github.com/PyCQA/isort/issues/1854

robsdedude avatar Nov 22 '21 14:11 robsdedude