diff --git a/pylint/checkers/similar.py b/pylint/checkers/similar.py index e2372423e3..cfe649b180 100644 --- a/pylint/checkers/similar.py +++ b/pylint/checkers/similar.py @@ -596,38 +596,41 @@ def stripped_lines( the line :return: the collection of line/line number/line type tuples """ - # pylint: disable=possibly-used-before-assignment + ignore_lines: set[int] = set() if ignore_imports or ignore_signatures: tree = astroid.parse("".join(lines)) - if ignore_imports: - 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( - functions: list[nodes.NodeNG], tree: nodes.NodeNG - ) -> list[nodes.NodeNG]: - """Recursively get all functions including nested in the classes from the - tree. - """ - for node in tree.body: - if isinstance(node, (nodes.FunctionDef, nodes.AsyncFunctionDef)): - functions.append(node) - - if isinstance( - node, - (nodes.ClassDef, nodes.FunctionDef, nodes.AsyncFunctionDef), - ): - _get_functions(functions, node) - - return functions - - functions = _get_functions([], tree) - signature_lines = set( - chain( - *( + if ignore_imports: + ignore_lines.update( + chain.from_iterable( + range(node.lineno, (node.end_lineno or node.lineno) + 1) + for node in tree.nodes_of_class((nodes.Import, nodes.ImportFrom)) + ) + ) + if ignore_signatures: + + def _get_functions( + functions: list[nodes.NodeNG], tree: nodes.NodeNG + ) -> list[nodes.NodeNG]: + """Recursively get all functions including nested in the classes from + the. + + tree. + """ + for node in tree.body: + if isinstance(node, (nodes.FunctionDef, nodes.AsyncFunctionDef)): + functions.append(node) + + if isinstance( + node, + (nodes.ClassDef, nodes.FunctionDef, nodes.AsyncFunctionDef), + ): + _get_functions(functions, node) + + return functions + + functions = _get_functions([], tree) + ignore_lines.update( + chain.from_iterable( range( func.lineno, func.body[0].lineno if func.body else func.tolineno + 1, @@ -635,7 +638,6 @@ def _get_functions( for func in functions ) ) - ) strippedlines = [] docstring = None @@ -657,13 +659,9 @@ def _get_functions( if line.endswith(docstring): docstring = None line = "" - if ignore_imports: - current_line_is_import = import_lines.get(lineno, False) - if current_line_is_import: - line = "" if ignore_comments: line = line.split("#", 1)[0].strip() - if ignore_signatures and lineno in signature_lines: + if lineno in ignore_lines: line = "" if line: strippedlines.append(