Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes duplicate-code check with ignore-imports #9147

Merged
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
18 changes: 9 additions & 9 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,16 +598,16 @@ 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]
)
node.lineno: True
for node in tree.nodes_of_class((nodes.Import, nodes.ImportFrom))
}
line_begins_import.update(
{
node.lineno: isinstance(node, (nodes.Import, nodes.ImportFrom))
for node in tree.body
}
)
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
current_line_is_import = False
if ignore_signatures:

Expand Down
2 changes: 1 addition & 1 deletion tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,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
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,
)