Skip to content

Commit

Permalink
[duplicate-code] Fix ignored empty functions by similarities checker …
Browse files Browse the repository at this point in the history
…with ignore-signatures option enabled (#4669)

Co-authored-by: Maksym Humetskyi <mhumets@softserveinc.com>
  • Loading branch information
mhumetskyi and Maksym Humetskyi committed Jul 5, 2021
1 parent c2d03c6 commit fc3fe81
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,6 @@ contributors:
* Lorena Buciu (lorena-b): contributor

* Sergei Lebedev (superbobry): contributor

* Maksym Humetskyi (mhumetskyi): contributor
- Fixed ignored empty functions by similarities checker with "ignore-signatures" option enabled
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Release date: TBA

Closes #4657

* Fix ignored empty functions by similarities checker with "ignore-signatures" option enabled

Closes #4652


What's New in Pylint 2.9.3?
===========================
Expand Down
6 changes: 4 additions & 2 deletions pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ def stripped_lines(
signature_lines = set(
chain(
*(
range(func.fromlineno, func.body[0].lineno)
range(
func.fromlineno,
func.body[0].lineno if func.body else func.tolineno + 1,
)
for func in functions
if func.body
)
)
)
Expand Down
40 changes: 40 additions & 0 deletions tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
SIMILAR4 = str(INPUT / "similar4")
SIMILAR5 = str(INPUT / "similar5")
SIMILAR6 = str(INPUT / "similar6")
EMPTY_FUNCTION_1 = str(INPUT / "similar_empty_func_1.py")
EMPTY_FUNCTION_2 = str(INPUT / "similar_empty_func_2.py")
MULTILINE = str(INPUT / "multiline-import")
HIDE_CODE_WITH_IMPORTS = str(INPUT / "hide_code_with_imports.py")

Expand Down Expand Up @@ -199,6 +201,44 @@ def test_ignore_signatures_pass():
)


def test_ignore_signatures_empty_functions_fail():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run([EMPTY_FUNCTION_1, EMPTY_FUNCTION_2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== (
'''
6 similar lines in 2 files
==%s:1
==%s:1
arg1: int = 1,
arg2: str = "2",
arg3: int = 3,
arg4: bool = True,
) -> None:
"""Valid function definition with docstring only."""
TOTAL lines=14 duplicates=6 percent=42.86
'''
% (EMPTY_FUNCTION_1, EMPTY_FUNCTION_2)
).strip()
)


def test_ignore_signatures_empty_functions_pass():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-signatures", EMPTY_FUNCTION_1, EMPTY_FUNCTION_2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== """
TOTAL lines=14 duplicates=0 percent=0.00
""".strip()
)


def test_no_hide_code_with_imports():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
Expand Down
7 changes: 7 additions & 0 deletions tests/input/similar_empty_func_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def func1(
arg1: int = 1,
arg2: str = "2",
arg3: int = 3,
arg4: bool = True,
) -> None:
"""Valid function definition with docstring only."""
7 changes: 7 additions & 0 deletions tests/input/similar_empty_func_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def func2(
arg1: int = 1,
arg2: str = "2",
arg3: int = 3,
arg4: bool = True,
) -> None:
"""Valid function definition with docstring only."""

0 comments on commit fc3fe81

Please sign in to comment.