Skip to content

Commit 23c6d37

Browse files
aatlePierre-Sassoulas
authored andcommitted
Rewrote stripped_lines() to fix possibly-used-before-assignment
1 parent ccb92f2 commit 23c6d37

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

pylint/checkers/similar.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -596,46 +596,48 @@ def stripped_lines(
596596
the line
597597
:return: the collection of line/line number/line type tuples
598598
"""
599-
# pylint: disable=possibly-used-before-assignment
599+
ignore_lines: set[int] = set()
600600
if ignore_imports or ignore_signatures:
601601
tree = astroid.parse("".join(lines))
602-
if ignore_imports:
603-
import_lines = {}
604-
for node in tree.nodes_of_class((nodes.Import, nodes.ImportFrom)):
605-
for lineno in range(node.lineno, (node.end_lineno or node.lineno) + 1):
606-
import_lines[lineno] = True
607-
if ignore_signatures:
608-
609-
def _get_functions(
610-
functions: list[nodes.NodeNG], tree: nodes.NodeNG
611-
) -> list[nodes.NodeNG]:
612-
"""Recursively get all functions including nested in the classes from the
613-
tree.
614-
"""
615-
for node in tree.body:
616-
if isinstance(node, (nodes.FunctionDef, nodes.AsyncFunctionDef)):
617-
functions.append(node)
618-
619-
if isinstance(
620-
node,
621-
(nodes.ClassDef, nodes.FunctionDef, nodes.AsyncFunctionDef),
622-
):
623-
_get_functions(functions, node)
624-
625-
return functions
626-
627-
functions = _get_functions([], tree)
628-
signature_lines = set(
629-
chain(
630-
*(
602+
if ignore_imports:
603+
ignore_lines.update(
604+
chain.from_iterable(
605+
range(node.lineno, (node.end_lineno or node.lineno) + 1)
606+
for node in tree.nodes_of_class((nodes.Import, nodes.ImportFrom))
607+
)
608+
)
609+
if ignore_signatures:
610+
611+
def _get_functions(
612+
functions: list[nodes.NodeNG], tree: nodes.NodeNG
613+
) -> list[nodes.NodeNG]:
614+
"""Recursively get all functions including nested in the classes from
615+
the.
616+
617+
tree.
618+
"""
619+
for node in tree.body:
620+
if isinstance(node, (nodes.FunctionDef, nodes.AsyncFunctionDef)):
621+
functions.append(node)
622+
623+
if isinstance(
624+
node,
625+
(nodes.ClassDef, nodes.FunctionDef, nodes.AsyncFunctionDef),
626+
):
627+
_get_functions(functions, node)
628+
629+
return functions
630+
631+
functions = _get_functions([], tree)
632+
ignore_lines.update(
633+
chain.from_iterable(
631634
range(
632635
func.lineno,
633636
func.body[0].lineno if func.body else func.tolineno + 1,
634637
)
635638
for func in functions
636639
)
637640
)
638-
)
639641

640642
strippedlines = []
641643
docstring = None
@@ -657,13 +659,9 @@ def _get_functions(
657659
if line.endswith(docstring):
658660
docstring = None
659661
line = ""
660-
if ignore_imports:
661-
current_line_is_import = import_lines.get(lineno, False)
662-
if current_line_is_import:
663-
line = ""
664662
if ignore_comments:
665663
line = line.split("#", 1)[0].strip()
666-
if ignore_signatures and lineno in signature_lines:
664+
if lineno in ignore_lines:
667665
line = ""
668666
if line:
669667
strippedlines.append(

0 commit comments

Comments
 (0)