The duplicate-code (R0801) can't be disabled
pylint-bot opened this issue Β· 52 comments
Originally reported by: Anonymous
It's seems like it's not possible to disable the duplicate code check on portions of a file. Looking at the source, I can see why as it's not a trivial thing to do (if you want to maintain the same scope semantics as other #pylint:enable/disable comments. This would be nice to have though (or I guess I could just cleanup my duplicate code).
Original comment by Radek HolΓ½ (BitBucket: PyDeq, GitHub: @PyDeQ?):
Pylint marks even import blocks as duplicates. In my case, it is:
#!python
import contextlib
import io
import itertools
import os
import subprocess
import tempfile
I doubt it is possible to clean up/refactor the code to prevent this, thus it would be nice if it would ignore imports or if it would be possible to use the disable comment.
Original comment by Buck Evan (BitBucket: bukzor, GitHub: @bukzor?):
I've run into this today.
My project has several setup.py files, which of course look quite similar, but can't really share code.
I tried to # pylint:disable=duplicate-code
just the line in question (setup()
), but it did nothing.
I'll have to turn the checker off entirely I think.
any news on this issue ?
No one is currently working on this. This would be nice to have fixed, but unfortunately I didn't have time to look into it. A pull request would be appreciated though and would definitely move it forward.
Is this related to duplicate-except? it cannot be disabled either.
sorry, Seems it is able to be disabled.
@PCManticore as you mentioned, there is probably no good way to fix this issue for this version; What about fixing this in a "bad way" for this version, and later on in the planned version 3 you can utilize a better engineered two-phase design ?
While sailing, sometimes the only way to fix your ship is ugly patching, and it's better than not fixing.
A functioning tool is absolutely better than a "perfectly engineered" tool.
A functioning tool is absolutely better than a "perfectly engineered" tool.
It's better because it exists, unlike perfect engineering :D
Is this issue resolved? I'm using pylint 2.1.1 and cannot seem to disable the warning with comments in the files which are printed in the warning.
Hi @levsa
I have created a custom patch that you can use locally from:
We are using it 2.5 years ago and it is working fine.
The main issue is that the state_lines
are not propagated for the methods used.
This patch just propagates them correctly.
You can use:
wget -q https://raw.githubusercontent.com/Vauxoo/pylint-conf/master/conf/pylint_pr1055.patch -O pylint_pr1055.patch
patch -f -p0 $(python -c "from __future__ import print_function; from pylint.checkers import similar; print(similar.__file__.rstrip('c'))") -i pylint_pr1055.patch
@moylop260 Your fix doesn't work. Even if I have # pylint: disable=duplicate-code
in each file with duplicate code, I still get errors.
You have duplicated comments too.
Check answer #1055 (comment)
Still have this issue on pylint = "==2.3.1"
The issue still there for latest python 2 pylint version 1.9.4
# pylint: disable=duplicate-code
does not work, while # pylint: disable=all` can disable the dup check.
Also have this issue with pylint==2.3.1
Even if i add the the pragma in every file with duplicate code like @filips123, i still get the files with the pragma reported ....
Any progress on this? I don't want to # pylint: disable=all
for all affected files ...
I am using pylint 2.4.0 for python 3 and I can see that disable=duplicate-code
is working.
I'm using pylint 2.4.4 and # pylint: disable=duplicate-code
is not working for me.
Also on 2.4.4
And on 2.4.2
another confirmation of this issue on 2.4.4
Not working for me # pylint: disable=duplicate-code
at 2.4.4 version python 3.8
Also not woking at 2.4.4 with python 3.7.4 and # pylint: disable=R0801
or # pylint: disable=duplicate-code
Also doesn't work. pylint 2.4.4, astroid 2.3.3, Python 3.8.2
I found a solution to partially solve this.
Use pylintrc. Try changing the min-similarity-lines
in the similarities section of your pylintrc config file.
[SIMILARITIES]
# Minimum lines number of a similarity.
min-similarity-lines=4
# Ignore comments when computing similarities.
ignore-comments=yes
# Ignore docstrings when computing similarities.
ignore-docstrings=yes
# Ignore imports when computing similarities.
ignore-imports=no
+1
Happened to me on a overloaded method declaration, which is obviously identical as its parent class, and in my case its sister class, so pylint reports "Similar lines in 3 files" with the def
and its 4 arguments (spanning on 6 lines, I have one argument per line due to type anotations).
It think this could be mitigated by whitelisting import statements and method declaration.
Just been bitten by this issue today.
pylint 2.6.0
astroid 2.4.2
Python 3.9.1 (default, Jan 20 2021, 00:00:00)
Increasing min-similarity-lines
is not actually a solution, as it will turn off duplicate code evaluation.
In my particular case I had two separate classes with similar inputs in the def __init__
As a workaround and what worked for me was to include the following in our pyproject.toml (which is the SIMILARITIES section if you use pylinr.rc):
[tool.pylint.SIMULARITIES]
ignore-comments = "no"
Doing this then allows pylint to not disregard the comments on lines. From there we added the # pylint: disable=duplicate-code
to the end of one of the lines which now made this "unique" (although you could in practice use any comment text)
This is a better temporary workaround than the min-similarity-lines
option as that:
- misses other instance where you'd want that check
- also doesnt return with a valid exit code even though rating is 10.00/10
it is possible now with file pylint.rc with disable key and value duplicate-code, this issue can be closed and solve in my opnion #214
it is possible now with file pylint.rc with disable key and value duplicate-code, this issue can be closed and solve in my opnion #214
The main issue is not being able to use # pylint: disable=duplicate-code
to exclude particular blocks of code. Disabling duplicate-code
all together is not an acceptable solution.
Since the issue was opened we added multiple option to ignore import, ignore docstrings and ignore signatures, so there is less and less reasons to want to disable it. But the problem still exists.
So any idea how to exclude particular blocks of code from the duplicated code checks without disabling the whole feature?
So any idea how to exclude particular blocks of code from the duplicated code checks without disabling the whole feature?
Right now there isn't, this require a change in pylint.
any news about this? It looks like many people have been bitten by this and it's marked as high prio
thanks
I have openend a PR which includes a possible solution to this problem. I can't promise it will be merged though as various previous approaches have been rejected.
@DanielNoord was your approach accepted? Will we see that working anytime soon?
@dothebart No I think it ended up in PR heaven
but it is still scheduled for the next release.
I think if I ask @Pierre-Sassoulas nicely he might find some time to do a review π
@dothebart Thanks for the reminder, @Pierre-Sassoulas got to work quickly and we just merged the proposed PR in main
. We're trying to release ASAP, so expect this to be available somewhere in the coming days/weeks π
This is so great to hear, and I know how pleasing it is to close a Bug / Feature request after such a long time ;-)
Where will be the usage of this feature documented?
some of these took similar long... https://github.com/collectd/collectd/pulls?q=is%3Apr+author%3Adothebart+is%3Aclosed
Where will be the usage of this feature documented?
This works the same as any other disable in pylint
. So if you want to disable the message for a particular line you can add:
pylint: disable=duplicate-code
. I'd refer to this page in the documentation which explains this a little more!
I am getting [E0011(unrecognized-inline-option), ] Unrecognized file option 'ignore'
from pylint 2.13.8 when adding # pylint: ignore=duplicate-code
on top of a file and/or a line of code. Am I missing something?
@famura Can you try # pylint: disable=duplicate-code
instead?
@JulienPalard your suggestion (+ me putting it on top of the file) made it work. The latter was necessary since pylint got triggered by the import statements, too.
Thank you.
You might want to have a look at --ignore-imports
: https://pylint.pycqa.org/en/latest/user_guide/configuration/all-options.html#ignore-imports
# pylint: disable=duplicate-code
or # pylint: disable=R0801
isn't working for me when running pylint 2.15.5
Could you add a reproducer and open a new bug issue @duanyutong, please ?
I get flagged by R0801, when I have code like this in two files:
return_value_0,
return_value_1,
return_value_2,
return_value_3 = \
my_function(
parameter_0,
parameter_1,
parameter_2,
parameter_3,
)
Note that while there are 10 lines in this example, it only contains a single statement, namely a single function call. Maybe R0801 should consider duplicate statements rather than duplicate lines?
@Bengt I think it could be an option like --ignore-multiline-calls
(like other similarities checker options https://pylint.pycqa.org/en/latest/user_guide/configuration/all-options.html#similarities-checker) could you open another issue please ?
Iβm a little unclear on how to disable the message for select occurrences, while keeping the option enabled in general. The docs donβt mention that detail either.
For example, suppose I have
************* Module foo.bar
path/to/foo/bar.py:1:0: R0801: Similar lines in 2 files
==foo.other.nip:[32:89]
==foo.bar:[37:97]
a = b
b = more()
some_other_code() (duplicate-code)
Adding a # pylint: disable=duplicate-code
to lines 32 and 37 in both files, respectively, doesnβt work. How can I then silence the warning?