Skip to content

Newline not always added at the end when input has no newline #1483

Open
@glostis

Description

Hi,

First of all, thanks for the great tool which I have been using daily for the past 2 years! Thanks in particular for the 5.X release with the --profile black option which is very handy!

I have been observing what I think is an inconsistency in the output of isort when run with a string from stdin.
A git bisect shows that this inconsistency was introduced with commit 2ba782f

Consider the following snippet of code:

import isort

str1 = "import os\nprint(os.environ)"
str2 = "import os\ndef main():\n    print(os.environ)"
for string in [str1, str2]:
    print("Input:")
    print(repr(string))
    print("Output:")
    print(repr(isort.SortImports(file_contents=string).output))
    print("----")

When run at commit f5ef546 (i.e. just before the offending commit), it prints:

Input:
'import os\nprint(os.environ)'
Output:
'import os\n\nprint(os.environ)\n'  <- newline added
----
Input:
'import os\ndef main():\n    print(os.environ)'
Output:
'import os\n\n\ndef main():\n    print(os.environ)\n' <- newline added
----

When run at the offending commit (2ba782f), it prints:

Input:
'import os\nprint(os.environ)'
Output:
'import os\n\nprint(os.environ)\n' <- newline added
----
Input:
'import os\ndef main():\n    print(os.environ)'
Output:
'import os\n\n\ndef main():\n    print(os.environ)' <- no newline added
----

I consider commit f5ef546 to be consistent because it adds a newline at the end of the output in both cases.
However, commit 2ba782f (up til the HEAD of develop as of writing) is inconsistent because it adds a newline in the str1 case, but not in the str2 case.

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions