Skip to content

Commit

Permalink
Implemented #1394: 100% branch coverage (in addition to line coverage…
Browse files Browse the repository at this point in the history
…) enforced.
  • Loading branch information
timothycrosley committed Jun 17, 2021
1 parent 9bc8c9a commit 5c0f120
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
1 change: 1 addition & 0 deletions .coveragerc
@@ -1,4 +1,5 @@
[run]
branch = True
omit =
isort/_future/*
isort/_vendored/*
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions isort/output.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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="",
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions isort/parse.py
Expand Up @@ -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

Expand Down Expand Up @@ -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}"
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down
75 changes: 61 additions & 14 deletions tests/unit/test_isort.py
Expand Up @@ -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
Expand All @@ -1280,21 +1278,59 @@ def test_force_single_line_imports_and_sort_within_sections() -> None:

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"
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4804,6 +4834,23 @@ def test_noqa_issue_1065() -> None:
"""
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 = """
Expand Down

0 comments on commit 5c0f120

Please sign in to comment.