Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

python - Disable pylint message for a given module or directory

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
' + 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...