Is it possible to ignore one single specific line with pylint?

懵懂的女人 提交于 2019-12-17 15:35:14

问题


I have the following line in my header:

import config.logging_settings

This actually changes my python logging settings, but pylint thinks it is an unused import. I do not want to remove unused-import warnings in general so is it possible to just ignore this one specific line?

I wouldn't mind having a .pylintrc for this project so answers changing a config file will be accepted.

Otherwise, something like this will also be appreciated:

import config.logging_settings # pylint: disable-this-line-in-some-way

回答1:


Pylint message control is documented in the Pylint manual:

Is it possible to locally disable a particular message?

Yes, this feature has been added in Pylint 0.11. This may be done by adding
# pylint: disable=some-message,another-one
at the desired block level or at the end of the desired line of code

You can use the message code or the symbolic names.

For example

def test():
    # Disable all the no-member violations in this function
    # pylint: disable=no-member
    ...

The manual also has further examples.

There is a wiki that documents all pylint messages and their codes.




回答2:


import config.logging_settings # pylint: disable=W0611

That was simple and is specific for that line.

As sthenault kindly pointed out, you can and should use the more readable form:

import config.logging_settings # pylint: disable=unused-import



回答3:


I believe what you're looking for is...

import config.logging_settings  # @UnusedImport

Note the double space before the comment to avoid hitting other formatting warnings.

Also, depending on your IDE (if you're using one), there's probably an option to add the correct ignore rule (eg in eclipse pressing Ctrl1 while the cursor is over the warning will auto-suggest @UnusedImport




回答4:


Checkout the files in https://github.com/PyCQA/pylint/tree/master/pylint/checkers. I haven't found a better way to obtain the error name from a message than either Ctrl+F-ing those files or using the Github search feature:

If the message is "No name ... in module ...", use the search:

No name %r in module %r repo:PyCQA/pylint/tree/master path:/pylint/checkers

Or, to get less results:

"No name %r in module %r" repo:PyCQA/pylint/tree/master path:/pylint/checkers

Github will show you:

"E0611": (
    "No name %r in module %r",
    "no-name-in-module",
    "Used when a name cannot be found in a module.",

You can then do:

from collections import Sequence # pylint: disable=no-name-in-module


来源:https://stackoverflow.com/questions/28829236/is-it-possible-to-ignore-one-single-specific-line-with-pylint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!