From 55b9aac25e9527badd5ae7139be8ba08dfa7e58f Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sun, 5 Jan 2020 04:05:35 -0800 Subject: Fix issue #1065, multiple noqa comments --- isort/comments.py | 6 +++++- isort/parse.py | 3 ++- tests/test_isort.py | 37 +++++++++++++++++++++++++++++++++---- 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 -- cgit v1.2.1