Skip to content

Commit

Permalink
[duplicate-code] Fix crash when analysing empty function with docstring
Browse files Browse the repository at this point in the history
Closes #4648
  • Loading branch information
Pierre-Sassoulas committed Jul 1, 2021
1 parent 2129086 commit 7ee9de5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
8 changes: 7 additions & 1 deletion pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
13 changes: 8 additions & 5 deletions tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
)
Expand All @@ -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()
)

Expand Down
3 changes: 3 additions & 0 deletions tests/input/similar5
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ def func1(
arg5: int = 5
) -> Ret1:
pass

def example():
"""Valid function definition with docstring only."""
3 changes: 3 additions & 0 deletions tests/input/similar6
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ async def func2(
arg5: int = 5
) -> Ret1:
pass

def example():
"""Valid function definition with docstring only."""

0 comments on commit 7ee9de5

Please sign in to comment.