Is there a way to disable Pylint's duplicate-code
message just for test files? All of the tests in our project are DAMP so the duplicated code is by design. I understand we can add # pylint: disable=duplicate-code
throughout our tests, but would rather add some sort of rule that says all files under a test/
folder will have this rule disabled. Is there a way to do this?
To be more specific, I'm looking for something different from a 'run it twice' solution (which is what I've already fallen back on).
There is the --disable
or -d
message control flag that can be used to selectively disable messages when called. You could therefore disable this message for all files under the test folder by running pylint on those files from within the project folder:
pylint -d duplicate-code test/
I was able to verify that I can cut out specific messages from all files in the directory, although I was not getting duplicate code errors and so could not check for that message.
You could also put this into a script that you run from your project's main directory. Something like:
#!/bin/bash
pylint src/
pylint -d duplicate-code test/
Alternatively, you could add # pylint: disable=duplicate-code
to the top of each of the files for which you want to exclude these messages. It looks like that is about as far as file-wise selective exclusion flags goes for pylint.
It can be achieved with pylint plugin and some hack.
Assume we have following directory structure:
pylint_plugin.py
app
├── __init__.py
└── mod.py
test
├── __init__.py
└── mod.py
content of mod.py:
def f():
1/0
content of pylint_plugin.py:
from astroid import MANAGER
from astroid import scoped_nodes
def register(linter):
pass
def transform(mod):
if 'test.' not in mod.name:
return
c = mod.stream().read()
# change to the message-id you need
c = b'# pylint: disable=pointless-statement\n' + c
# pylint will read from `.file_bytes` attribute later when tokenization
mod.file_bytes = c
MANAGER.register_transform(scoped_nodes.Module, transform)
without plugin, pylint will report:
************* Module tmp.exp_pylint.app.mod
W: 2, 4: Statement seems to have no effect (pointless-statement)
************* Module tmp.exp_pylint.test.mod
W: 2, 4: Statement seems to have no effect (pointless-statement)
with plugin loaded:
PYTHONPATH=. pylint -dC,R --load-plugins pylint_plugin app test
yields:
************* Module tmp.exp_pylint.app.mod
W: 2, 4: Statement seems to have no effect (pointless-statement)
pylint read comments by tokenizing source file, this plugin change file content on the fly, to cheat pylint when tokenization.
Note that to simplify demonstration, here I constructed a "pointless-statement" warning, disable other types of message is trivial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With