Skip to content

Commit

Permalink
Fixed #1279: Fixed NOQA comment regression.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 10, 2020
1 parent c04215f commit 66ac6c8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.0.7 July 9, 2020
- Fixed #1306: unexpected --diff behavior.
- Fixed #1279: Fixed NOQA comment regression.

### 5.0.6 July 8, 2020
- Fixed #1302: comments and --trailing-comma can generate invalid code.
Expand Down
6 changes: 6 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ def _build_arg_parser() -> argparse.ArgumentParser:
action="store_true",
help="See isort's determined config, as well as sources of config options.",
)
parser.add_argument(
"--honor-noqa",
dest="honor_noqa",
action="store_true",
help="Tells isort to honor noqa comments to enforce skipping those comments.",
)

# deprecated options
parser.add_argument(
Expand Down
8 changes: 5 additions & 3 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def _normalize_line(raw_line: str) -> Tuple[str, str]:
return (line, raw_line)


def import_type(line: str) -> Optional[str]:
def import_type(line: str, config: Config = DEFAULT_CONFIG) -> Optional[str]:
"""If the current line is an import line it will return its type (from or straight)"""
if "isort:skip" in line or "isort: skip" in line or "NOQA" in line:
if config.honor_noqa and line.lower().rstrip().endswith("noqa"):
return None
elif "isort:skip" in line or "isort: skip" in line:
return None
elif line.startswith(("import ", "cimport ")):
return "straight"
Expand Down Expand Up @@ -195,7 +197,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
(line.strip() for line in line.split(";")) if ";" in line else (line,) # type: ignore
):
line, raw_line = _normalize_line(line)
type_of_import = import_type(line) or ""
type_of_import = import_type(line, config) or ""
if not type_of_import:
out_lines.append(raw_line)
continue
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class _Config:
ensure_newline_before_comments: bool = False
directory: str = ""
profile: str = ""
honor_noqa: bool = False
src_paths: FrozenSet[Path] = frozenset()
old_finders: bool = False

Expand Down
50 changes: 31 additions & 19 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3773,26 +3773,38 @@ def test_all_imports_from_single_module() -> None:


def test_noqa_issue_679() -> None:
# Test to ensure that NOQA notation is being observed as expected
test_input = (
"import os\n"
"\n"
"import requestsss\n"
"import zed # NOQA\n"
"import ujson # NOQA\n"
"\n"
"import foo"
)
test_output = (
"import os\n"
"\n"
"import foo\n"
"import requestsss\n"
"\n"
"import zed # NOQA\n"
"import ujson # NOQA\n"
)
"""Test to ensure that NOQA notation is being observed as expected
if honor_noqa is set to `True`
"""
test_input = """
import os
import requestsss
import zed # NOQA
import ujson # NOQA
import foo"""
test_output = """
import os
import foo
import requestsss
import ujson # NOQA
import zed # NOQA
"""
test_output_honor_noqa = """
import os
import foo
import requestsss
import zed # NOQA
import ujson # NOQA
"""
assert isort.code(test_input) == test_output
assert isort.code(test_input.lower()) == test_output.lower()
assert isort.code(test_input, honor_noqa=True) == test_output_honor_noqa
assert isort.code(test_input.lower(), honor_noqa=True) == test_output_honor_noqa.lower()


def test_extract_multiline_output_wrap_setting_from_a_config_file(tmpdir: py.path.local) -> None:
Expand Down

0 comments on commit 66ac6c8

Please sign in to comment.