Skip to content

Commit

Permalink
Fixes duplicate-code check with ignore-imports (#9147) (#9176)
Browse files Browse the repository at this point in the history
(cherry picked from commit 67f20bd)

Co-authored-by: theirix <[email protected]>
  • Loading branch information
github-actions[bot] and theirix authored Oct 22, 2023
1 parent 792da24 commit 31aca81
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8914.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixes ignoring conditional imports with ``ignore-imports=y``.

Closes #8914
21 changes: 6 additions & 15 deletions pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from collections.abc import Callable, Generator, Iterable, Sequence
from getopt import getopt
from io import BufferedIOBase, BufferedReader, BytesIO
from itertools import chain, groupby
from itertools import chain
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -598,17 +598,10 @@ def stripped_lines(
if ignore_imports or ignore_signatures:
tree = astroid.parse("".join(lines))
if ignore_imports:
node_is_import_by_lineno = (
(node.lineno, isinstance(node, (nodes.Import, nodes.ImportFrom)))
for node in tree.body
)
line_begins_import = {
lineno: all(is_import for _, is_import in node_is_import_group)
for lineno, node_is_import_group in groupby(
node_is_import_by_lineno, key=lambda x: x[0]
)
}
current_line_is_import = False
import_lines = {}
for node in tree.nodes_of_class((nodes.Import, nodes.ImportFrom)):
for lineno in range(node.lineno, (node.end_lineno or node.lineno) + 1):
import_lines[lineno] = True
if ignore_signatures:

def _get_functions(
Expand Down Expand Up @@ -664,9 +657,7 @@ def _get_functions(
docstring = None
line = ""
if ignore_imports:
current_line_is_import = line_begins_import.get(
lineno, current_line_is_import
)
current_line_is_import = import_lines.get(lineno, False)
if current_line_is_import:
line = ""
if ignore_comments:
Expand Down
7 changes: 6 additions & 1 deletion tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest

from pylint.checkers import similar
from pylint.constants import IS_PYPY, PY39_PLUS
from pylint.lint import PyLinter
from pylint.testutils import GenericTestReporter as Reporter

Expand Down Expand Up @@ -130,6 +131,10 @@ def test_multiline_imports() -> None:
)


@pytest.mark.skipif(
IS_PYPY and not PY39_PLUS,
reason="Requires accurate 'end_lineno' value",
)
def test_ignore_multiline_imports() -> None:
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
Expand Down Expand Up @@ -295,7 +300,7 @@ def test_no_hide_code_with_imports() -> None:
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-imports"] + 2 * [HIDE_CODE_WITH_IMPORTS])
assert ex.value.code == 0
assert "TOTAL lines=32 duplicates=16 percent=50.00" in output.getvalue()
assert "TOTAL lines=32 duplicates=0 percent=0.00" in output.getvalue()


def test_ignore_nothing() -> None:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import typing

x = 1
if typing.TYPE_CHECKING:
import os
import sys
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import typing

x = 1
if typing.TYPE_CHECKING:
import os
import sys
15 changes: 15 additions & 0 deletions tests/test_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,18 @@ def test_useless_suppression() -> None:
exit=False,
)
assert not runner.linter.stats.by_msg

def test_conditional_imports(self) -> None:
"""Tests enabling ignore-imports with conditional imports works correctly."""
path = join(DATA, "ignore_conditional_imports")
expected_output = "==ignore_conditional_imports.file_one:[2:4]"
self._test_output(
[
path,
"-e=duplicate-code",
"-d=unused-import,C",
"--ignore-imports=y",
"--min-similarity-lines=1",
],
expected_output=expected_output,
)

0 comments on commit 31aca81

Please sign in to comment.