summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2020-01-05 04:17:20 -0800
committerGitHub <noreply@github.com>2020-01-05 04:17:20 -0800
commitdd1e8ca431f137a0282cd3d325ffa84802183c18 (patch)
tree55db9a424b592473fa87bd870a0afb55782cb0b3
parent0e8f7a3ace269ad0229b6cef56ff6aef5e06a8b0 (diff)
parent55b9aac25e9527badd5ae7139be8ba08dfa7e58f (diff)
downloadisort-dd1e8ca431f137a0282cd3d325ffa84802183c18.tar.gz
Merge pull request #1079 from timothycrosley/feature/fix-issue-1065
Fix issue #1065, multiple noqa comments
-rw-r--r--isort/comments.py6
-rw-r--r--isort/parse.py3
-rw-r--r--tests/test_isort.py37
3 files changed, 40 insertions, 6 deletions
diff --git a/isort/comments.py b/isort/comments.py
index d033804d..b865b328 100644
--- a/isort/comments.py
+++ b/isort/comments.py
@@ -25,4 +25,8 @@ def add_to_line(
if not comments:
return original_string
else:
- return f"{parse(original_string)[0]}{comment_prefix} {'; '.join(comments)}"
+ unique_comments: List[str] = []
+ for comment in comments:
+ if comment not in unique_comments:
+ unique_comments.append(comment)
+ return f"{parse(original_string)[0]}{comment_prefix} {'; '.join(unique_comments)}"
diff --git a/isort/parse.py b/isort/parse.py
index 92bb46b5..8172808d 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -332,7 +332,8 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
categorized_comments["nested"].setdefault(import_from, {})[
import_name
] = associated_comment
- comments.pop(comments.index(associated_comment))
+ if associated_comment in comments:
+ comments.pop(comments.index(associated_comment))
if comments:
categorized_comments["from"].setdefault(import_from, []).extend(comments)
diff --git a/tests/test_isort.py b/tests/test_isort.py
index 0bed7f49..db982587 100644
--- a/tests/test_isort.py
+++ b/tests/test_isort.py
@@ -964,8 +964,7 @@ def test_force_single_line_imports() -> None:
)
test_input = (
- "from third_party import lib_a, lib_b, lib_d\n"
- "from third_party.lib_c import lib1\n"
+ "from third_party import lib_a, lib_b, lib_d\n" "from third_party.lib_c import lib1\n"
)
test_output = SortImports(
file_contents=test_input,
@@ -4617,9 +4616,39 @@ IF CEF_VERSION == 3:
def test_top_level_import_order() -> None:
- config = {"force_sort_within_sections": 1} # type: Dict[str, Any]
test_input = (
"from rest_framework import throttling, viewsets\n"
"from rest_framework.authentication import TokenAuthentication\n"
)
- assert SortImports(file_contents=test_input, **config).output == test_input
+ assert (
+ SortImports(file_contents=test_input, force_sort_within_sections=True).output == test_input
+ )
+
+
+def test_noqa_issue_1065() -> None:
+ test_input = """
+#
+# USER SIGNALS
+#
+
+from flask_login import user_logged_in, user_logged_out # noqa
+
+from flask_security.signals import ( # noqa
+ password_changed as user_reset_password, # noqa
+ user_confirmed, # noqa
+ user_registered, # noqa
+) # noqa
+
+from flask_principal import identity_changed as user_identity_changed # noqa
+"""
+ expected_output = """
+#
+# USER SIGNALS
+#
+from flask_login import user_logged_in, user_logged_out # noqa
+from flask_principal import identity_changed as user_identity_changed # noqa
+from flask_security.signals import password_changed as user_reset_password # noqa
+from flask_security.signals import user_confirmed # noqa
+from flask_security.signals import user_registered # noqa
+"""
+ assert SortImports(file_contents=test_input).output == expected_output