From 7ee9de59fd31c3fdbe7e820406db4e5fb7cd3350 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Thu, 1 Jul 2021 13:38:37 +0200 Subject: [duplicate-code] Fix crash when analysing empty function with docstring Closes #4648 --- ChangeLog | 4 ++++ pylint/checkers/similar.py | 8 +++++++- tests/checkers/unittest_similar.py | 13 ++++++++----- tests/input/similar5 | 3 +++ tests/input/similar6 | 3 +++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4b523e2eb..2c79a4c13 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,10 @@ Release date: TBA .. Put bug fixes that should not wait for a new minor version here +* Fix a crash that happened when analysing empty function with docstring + using in the ``similarity`` checker. + + Closes #4648 What's New in Pylint 2.9.2? diff --git a/pylint/checkers/similar.py b/pylint/checkers/similar.py index 967796b2b..5f421f054 100644 --- a/pylint/checkers/similar.py +++ b/pylint/checkers/similar.py @@ -218,7 +218,13 @@ def stripped_lines( if isinstance(n, (astroid.FunctionDef, astroid.AsyncFunctionDef)) ] signature_lines = set( - chain(*(range(func.fromlineno, func.body[0].lineno) for func in functions)) + chain( + *( + range(func.fromlineno, func.body[0].lineno) + for func in functions + if func.body + ) + ) ) strippedlines = [] diff --git a/tests/checkers/unittest_similar.py b/tests/checkers/unittest_similar.py index 731f6a3c5..752c32bb6 100644 --- a/tests/checkers/unittest_similar.py +++ b/tests/checkers/unittest_similar.py @@ -164,8 +164,8 @@ def test_ignore_signatures_fail(): assert ( output.getvalue().strip() == ( - """ -7 similar lines in 2 files + ''' +10 similar lines in 2 files ==%s:1 ==%s:8 arg1: int = 3, @@ -175,8 +175,11 @@ def test_ignore_signatures_fail(): arg5: int = 5 ) -> Ret1: pass -TOTAL lines=23 duplicates=7 percent=30.43 -""" + + def example(): + """Valid function definition with docstring only.""" +TOTAL lines=29 duplicates=10 percent=34.48 +''' % (SIMILAR5, SIMILAR6) ).strip() ) @@ -190,7 +193,7 @@ def test_ignore_signatures_pass(): assert ( output.getvalue().strip() == """ -TOTAL lines=23 duplicates=0 percent=0.00 +TOTAL lines=29 duplicates=0 percent=0.00 """.strip() ) diff --git a/tests/input/similar5 b/tests/input/similar5 index d87610b1e..b6e6b1d0e 100644 --- a/tests/input/similar5 +++ b/tests/input/similar5 @@ -6,3 +6,6 @@ def func1( arg5: int = 5 ) -> Ret1: pass + +def example(): + """Valid function definition with docstring only.""" diff --git a/tests/input/similar6 b/tests/input/similar6 index 0377e9284..d12c8841b 100644 --- a/tests/input/similar6 +++ b/tests/input/similar6 @@ -13,3 +13,6 @@ async def func2( arg5: int = 5 ) -> Ret1: pass + +def example(): + """Valid function definition with docstring only.""" -- cgit v1.2.1