Skip to content

Allow disabling duplicate-code with a disable comment #5446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ Release date: TBA

Closes #4955

* Allow disabling ``duplicate-code`` with a disable comment when running through
pylint.

Closes #214

..
Insert your changelog randomly, it will reduce merge conflicts
(Ie. not necessarily at the end)
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ Other Changes

Closes #5323

* Allow disabling ``duplicate-code`` with a disable comment when running through
pylint.

Closes #214

* Fix false positive for ``undefined-variable`` when ``namedtuple`` class
attributes are used as return annotations.

Expand Down
11 changes: 10 additions & 1 deletion pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,19 @@ def append_stream(
else:
readlines = stream.readlines # type: ignore[assignment] # hint parameter is incorrectly typed as non-optional
try:
active_lines: List[str] = []
if hasattr(self, "linter"):
# Remove those lines that should be ignored because of disables
for index, line in enumerate(readlines()):
if self.linter._is_one_message_enabled("R0801", index + 1): # type: ignore[attr-defined]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty clever.

active_lines.append(line)
else:
active_lines = readlines()

self.linesets.append(
LineSet(
streamid,
readlines(),
active_lines,
self.ignore_comments,
self.ignore_docstrings,
self.ignore_imports,
Expand Down
11 changes: 11 additions & 0 deletions tests/regrtest_data/duplicate_code/raw_strings_all/third.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# pylint: disable=duplicate-code
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# pylint: disable=duplicate-code
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# pylint: disable=duplicate-code
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1 # pylint: disable=duplicate-code
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1 # pylint: disable=duplicate-code
yyyy = 2 # pylint: disable=duplicate-code
zzzz = 3 # pylint: disable=duplicate-code
wwww = 4 # pylint: disable=duplicate-code
vvvv = xxxx + yyyy + zzzz + wwww # pylint: disable=duplicate-code
return vvvv # pylint: disable=duplicate-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv # pylint: disable=duplicate-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3 # pylint: disable=duplicate-code
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
r"""A raw docstring.
"""


def look_busy():
# pylint: disable=duplicate-code
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
r"""A raw docstring.
"""


def look_busy():
# pylint: disable=duplicate-code
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
r"""A raw docstring.
"""


def look_busy():
# pylint: disable=duplicate-code
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
r"""A raw docstring.
"""


def look_busy():
# pylint: disable=duplicate-code
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv


def look_busy_two():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
r"""A raw docstring.
"""


def look_busy():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv


def look_busy_two():
xxxx = 1
yyyy = 2
zzzz = 3
wwww = 4
vvvv = xxxx + yyyy + zzzz + wwww
return vvvv
8 changes: 0 additions & 8 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,14 +1121,6 @@ def test_jobs_score(self) -> None:
expected = "Your code has been rated at 7.50/10"
self._test_output([path, "--jobs=2", "-ry"], expected_output=expected)

def test_duplicate_code_raw_strings(self) -> None:
path = join(HERE, "regrtest_data", "duplicate_data_raw_strings")
expected_output = "Similar lines in 2 files"
self._test_output(
[path, "--disable=all", "--enable=duplicate-code"],
expected_output=expected_output,
)

def test_regression_parallel_mode_without_filepath(self) -> None:
# Test that parallel mode properly passes filepath
# https://github.com/PyCQA/pylint/issues/3564
Expand Down
Loading