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

lines_before_import seems to not always work on files #1855

Open
robsdedude opened this issue Nov 22, 2021 · 0 comments
Open

lines_before_import seems to not always work on files #1855

robsdedude opened this issue Nov 22, 2021 · 0 comments

Comments

@robsdedude
Copy link

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

robsdedude added a commit to robsdedude/isort that referenced this issue Aug 21, 2022
When checking if a file got altered, leading and trailing white space is infact
relevant. Especially considering the existence of config options like
`lines_before_imports` and `lines_after_imports`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant