diff options
| -rw-r--r-- | .coveragerc | 1 | ||||
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | isort/output.py | 6 | ||||
| -rw-r--r-- | isort/parse.py | 8 | ||||
| -rw-r--r-- | tests/unit/test_isort.py | 75 |
5 files changed, 71 insertions, 21 deletions
diff --git a/.coveragerc b/.coveragerc index b9a74651..ba0e10c3 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,4 +1,5 @@ [run] +branch = True omit = isort/_future/* isort/_vendored/* diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cda3e3d..ff0c7725 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ - Fixed #1744: repeat noqa comments dropped when * import and non * imports exist from the same package. - Fixed #1721: repeat noqa comments on separate from lines with force-single-line set, sometimes get dropped. +#### Goal Zero (Tickets related to aspirational goal of achieving 0 regressions for remaining 5.0.0 lifespan): + - Implemented #1394: 100% branch coverage (in addition to line coverage) enforced. ### 5.8.0 March 20th 2021 - Fixed #1631: as import comments can in some cases be duplicated. diff --git a/isort/output.py b/isort/output.py index f53d81b7..9c2fe9ac 100644 --- a/isort/output.py +++ b/isort/output.py @@ -132,7 +132,7 @@ def sorted_imports( if config.dedup_headings: seen_headings.add(section_title) section_comment = f"# {section_title}" - if section_comment not in parsed.lines_without_imports[0:1]: + if section_comment not in parsed.lines_without_imports[0:1]: # pragma: no branch section_output.insert(0, section_comment) if pending_lines_before or not no_lines_before: @@ -173,7 +173,7 @@ def sorted_imports( next_construct = "" tail = formatted_output[imports_tail:] - for index, line in enumerate(tail): + for index, line in enumerate(tail): # pragma: no branch should_skip, in_quote, *_ = parse.skip_line( line, in_quote="", @@ -190,7 +190,7 @@ def sorted_imports( continue next_construct = line break - if in_quote: + if in_quote: # pragma: no branch next_construct = line break diff --git a/isort/parse.py b/isort/parse.py index 7a72497b..aec50874 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -187,7 +187,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte ) if line in config.section_comments and not skipping_line: - if import_index == -1: + if import_index == -1: # pragma: no branch import_index = index - 1 continue @@ -394,7 +394,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte direct_imports.remove("as") if nested_module == as_name and config.remove_redundant_aliases: pass - elif as_name not in as_map["from"][module]: + elif as_name not in as_map["from"][module]: # pragma: no branch as_map["from"][module].append(as_name) full_name = f"{nested_module} as {as_name}" @@ -403,7 +403,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte categorized_comments["nested"].setdefault(top_level_module, {})[ full_name ] = associated_comment - if associated_comment in comments: + if associated_comment in comments: # pragma: no branch comments.pop(comments.index(associated_comment)) else: module = just_imports[as_index - 1] @@ -453,7 +453,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte categorized_comments["nested"].setdefault(import_from, {})[ import_name ] = associated_comment - if associated_comment in comments: + if associated_comment in comments: # pragma: no branch comments.pop(comments.index(associated_comment)) if ( config.force_single_line diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 754d47f6..0599925e 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -1266,8 +1266,6 @@ def test_force_single_line_imports_and_sort_within_sections() -> None: "from third_party import lib_d\n" ) - # Ensure force_sort_within_sections can work with length sort - # See: https://github.com/pycqa/isort/issues/1038 test_input = """import sympy import numpy as np import pandas as pd @@ -1280,21 +1278,59 @@ from matplotlib import pyplot as plt def test_titled_imports() -> None: """Tests setting custom titled/commented import sections.""" - test_input = ( + # test_input = ( + # "import sys\n" + # "import unicodedata\n" + # "import statistics\n" + # "import os\n" + # "import myproject.test\n" + # "import django.settings" + # ) + # test_output = isort.code( + # code=test_input, + # known_first_party=["myproject"], + # import_heading_stdlib="Standard Library", + # import_heading_firstparty="My Stuff", + # ) + # assert test_output == ( + # "# Standard Library\n" + # "import os\n" + # "import statistics\n" + # "import sys\n" + # "import unicodedata\n" + # "\n" + # "import django.settings\n" + # "\n" + # "# My Stuff\n" + # "import myproject.test\n" + # ) + # test_second_run = isort.code( + # code=test_output, + # known_first_party=["myproject"], + # import_heading_stdlib="Standard Library", + # import_heading_firstparty="My Stuff", + # ) + # assert test_second_run == test_output + + test_input_lines_down = ( + "# comment 1\n" + "import django.settings\n" + "\n" + "# Standard Library\n" "import sys\n" "import unicodedata\n" "import statistics\n" "import os\n" "import myproject.test\n" - "import django.settings" ) - test_output = isort.code( - code=test_input, + test_output_lines_down = isort.code( + code=test_input_lines_down, known_first_party=["myproject"], import_heading_stdlib="Standard Library", import_heading_firstparty="My Stuff", ) - assert test_output == ( + assert test_output_lines_down == ( + "# comment 1\n" "# Standard Library\n" "import os\n" "import statistics\n" @@ -1306,13 +1342,6 @@ def test_titled_imports() -> None: "# My Stuff\n" "import myproject.test\n" ) - test_second_run = isort.code( - code=test_output, - known_first_party=["myproject"], - import_heading_stdlib="Standard Library", - import_heading_firstparty="My Stuff", - ) - assert test_second_run == test_output def test_balanced_wrapping() -> None: @@ -1501,6 +1530,7 @@ def test_combined_from_and_as_imports() -> None: "from translate.storage.placeables import general, parse as rich_parse\n" ) assert isort.code(test_input, combine_as_imports=True) == test_input + assert isort.code(test_input, combine_as_imports=True, only_sections=True) == test_input test_input = "import os \nimport os as _os" test_output = "import os\nimport os as _os\n" assert isort.code(test_input) == test_output @@ -4804,6 +4834,23 @@ from flask_security.signals import user_registered # noqa """ assert isort.code(test_input, line_length=100) == expected_output + test_input_2 = """ +# +# USER SIGNALS +# + +from flask_login import user_logged_in, user_logged_out # noqa + +from flask_security.signals import ( + password_changed as user_reset_password, # noqa + user_confirmed, # noqa + user_registered, # noqa +) + +from flask_principal import identity_changed as user_identity_changed # noqa +""" + assert isort.code(test_input_2, line_length=100) == expected_output + def test_single_line_exclusions(): test_input = """ |
