summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-12-26 23:04:46 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2020-12-26 23:04:46 -0800
commit509af74d51438442aa22def564cf7ecd6efe5a2d (patch)
tree81d2a297e29bd3f8594133f9ad98a775d57c2afe
parentc48fd911e4afd8f542f561490b16aeaaaabe9fae (diff)
parenta19e364f1464f1683c22394d9910f21df05b9a5e (diff)
downloadisort-509af74d51438442aa22def564cf7ecd6efe5a2d.tar.gz
Merge branch 'develop' of https://github.com/timothycrosley/isort into develop
-rw-r--r--CHANGELOG.md1
-rw-r--r--docs/configuration/black_compatibility.md2
-rw-r--r--isort/wrap.py13
-rw-r--r--tests/unit/test_regressions.py30
4 files changed, 44 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ac6d209c..7e377fd2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
Find out more about isort's release policy [here](https://pycqa.github.io/isort/docs/major_releases/release_policy/).
### 5.7.0 December TBD
+ - Fixed #1612: In rare circumstances an extra comma is added after import and before comment.
- Implemented #1596: Provide ways for extension formatting and file paths to be specified when using streaming input from CLI.
- Implemented #1583: Ability to output and diff within a single API call to `isort.file`.
- Implemented #1562, #1592 & #1593: Better more useful fatal error messages.
diff --git a/docs/configuration/black_compatibility.md b/docs/configuration/black_compatibility.md
index c81989a9..a5775425 100644
--- a/docs/configuration/black_compatibility.md
+++ b/docs/configuration/black_compatibility.md
@@ -11,6 +11,8 @@ All that's required to use isort alongside black is to set the isort profile to
For projects that officially use both isort and black, we recommend setting the black profile in a config file at the root of your project's repository.
This way independent to how users call isort (pre-commit, CLI, or editor integration) the black profile will automatically be applied.
+For instance, your _pyproject.toml_ file would look something like
+
```ini
[tool.isort]
profile = "black"
diff --git a/isort/wrap.py b/isort/wrap.py
index 11542fa0..e993ae0f 100644
--- a/isort/wrap.py
+++ b/isort/wrap.py
@@ -77,7 +77,13 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
line_parts = re.split(exp, line_without_comment)
if comment and not (config.use_parentheses and "noqa" in comment):
_comma_maybe = (
- "," if (config.include_trailing_comma and config.use_parentheses) else ""
+ ","
+ if (
+ config.include_trailing_comma
+ and config.use_parentheses
+ and not line_without_comment.rstrip().endswith(",")
+ )
+ else ""
)
line_parts[
-1
@@ -92,13 +98,16 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
content = next_line.pop()
cont_line = _wrap_line(
- config.indent + splitter.join(next_line).lstrip(), line_separator, config
+ config.indent + splitter.join(next_line).lstrip(),
+ line_separator,
+ config,
)
if config.use_parentheses:
if splitter == "as ":
output = f"{content}{splitter}{cont_line.lstrip()}"
else:
_comma = "," if config.include_trailing_comma and not comment else ""
+
if wrap_mode in (
Modes.VERTICAL_HANGING_INDENT, # type: ignore
Modes.VERTICAL_GRID_GROUPED, # type: ignore
diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py
index c25ed19c..89fa0927 100644
--- a/tests/unit/test_regressions.py
+++ b/tests/unit/test_regressions.py
@@ -1485,3 +1485,33 @@ print(CCCCCCCCC)
show_diff=True,
multi_line_output=9,
)
+
+
+def test_isort_adding_second_comma_issue_1621():
+ """Ensure isort doesnt add a second comma when very long comment is present
+ See: https://github.com/PyCQA/isort/issues/1621.
+ """
+ assert isort.check_code(
+ """from .test import (
+ TestTestTestTestTestTest2 as TestTestTestTestTestTest1, """
+ """# Some really long comment bla bla bla bla bla
+)
+""",
+ profile="black",
+ show_diff=True,
+ )
+ assert (
+ isort.code(
+ """from .test import (
+ TestTestTestTestTestTest2 as TestTestTestTestTestTest1 """
+ """# Some really long comment bla bla bla bla bla
+)
+""",
+ profile="black",
+ )
+ == """from .test import (
+ TestTestTestTestTestTest2 as TestTestTestTestTestTest1, """
+ """# Some really long comment bla bla bla bla bla
+)
+"""
+ )