PyCQA / PyCQA/isort

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

Open
#1,483 5 comments 0 reactions 1 assignee View on GitHub

@anirudnits is already working on this.

Since Oct 12, 2020.

enhancement
Dominant language
Python
Stars
7k
Forks
687
Avg merge
4h 56m
Merged PRs (30d)
2

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 2ba782fb1a74294f2a67cd64fe4b6bf98dddba30

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 f5ef546ede9450b19d7c615ab7e7c833f88a7dcf (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 (2ba782fb1a74294f2a67cd64fe4b6bf98dddba30), 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 f5ef546ede9450b19d7c615ab7e7c833f88a7dcf to be consistent because it adds a newline at the end of the output in both cases.
However, commit 2ba782fb1a74294f2a67cd64fe4b6bf98dddba30 (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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.