Python 可以自定义Pylint错误检查吗?

Python 可以自定义Pylint错误检查吗?,python,pydev,pylint,Python,Pydev,Pylint,我正在使用pydev,我已经在其中设置了pylint。 问题是,即使在注释中,pylint也会报告警告。我希望在任何行或块注释中禁用任何类型的检查。 此外,我希望在代码中遵循camelCase命名约定,而不是变量和参数的下划线。 有没有办法指定这样一个规则而不插入带有任何pylint:disable comments的代码?您可以使用 pylint --disable=W1234 或者使用特殊的PyLint配置文件 pylint --rcfile=/path/to/config.file 下

我正在使用pydev,我已经在其中设置了pylint。 问题是,即使在注释中,pylint也会报告警告。我希望在任何行或块注释中禁用任何类型的检查。 此外,我希望在代码中遵循camelCase命名约定,而不是变量和参数的下划线。
有没有办法指定这样一个规则而不插入带有任何pylint:disable comments的代码?

您可以使用

pylint --disable=W1234
或者使用特殊的PyLint配置文件

pylint --rcfile=/path/to/config.file
下面给出了一个示例配置文件:

[MESSAGES CONTROL]
# C0111 Missing docstring 
# I0011 Warning locally suppressed using disable-msg
# I0012 Warning locally suppressed using disable-msg
# W0704 Except doesn't do anything Used when an except clause does nothing but "pass" and there is no "else" clause
# W0142 Used * or * magic* Used when a function or method is called using *args or **kwargs to dispatch arguments.
# W0212 Access to a protected member %s of a client class
# W0232 Class has no __init__ method Used when a class has no __init__ method, neither its parent classes.
# W0613 Unused argument %r Used when a function or method argument is not used.
# W0702 No exception's type specified Used when an except clause doesn't specify exceptions type to catch.
# R0201 Method could be a function
# W0614 Unused import XYZ from wildcard import
# R0914 Too many local variables
# R0912 Too many branches
# R0915 Too many statements
# R0913 Too many arguments
# R0904 Too many public methods
disable=C0111,I0011,I0012,W0704,W0142,W0212,W0232,W0613,W0702,R0201,W0614,R0914,R0912,R0915,R0913,R0904,R0801

参见。

正如cfedermann所说,您可以在
~/.pylintrc
文件中指定要禁用的消息(注意,如果您不想使用内联注释,可以使用
pylint--generate rcfile
生成存根文件


您还将在生成的文件的[BASIC]部分看到“method rgx”、“function rgx”等选项,您可以根据需要配置这些选项,以支持驼峰格样式而不是pep8下划线样式。

虽然这是一个老问题,但应该提到的是,现在可以指定它们的名称

然后,您的正则表达式将与camel case匹配,如下所示:

[a-z][a-zA-Z0-9]{2,30}$
,及

我遇到了一个与您类似的问题。以下代码是我的解决方案。我自定义了一个检查器以禁止导入
datetime。现在
。您可以将其作为参考:

class TestChecker(BaseChecker):
    """
    find the check type in the following url:
    https://github.com/PyCQA/pylint/blob/63eb8c4663a77d0caf2a842b716e4161f9763a16/pylint/checkers/typecheck.py
    """
    __implements__ = IAstroidChecker

    name = 'test-checker'
    priority = -1
    msgs = {
        'W0001': (
            'You should not import "%s"',
            'import-datetime-returns',
            'Should not import datetime'
        ),
    }

    def __init__(self, linter):
        super().__init__(linter)
        # I use original pylint's ImportsChecker as a property
        # from  import **
        self.forbidden_import = ['datetime.datetime.now']
        self.forbidden_import_from = ['datetime.now', 'now']
        self.forbidden_import_attribute = ['datetime.now', 'now', 'datetime.datetime.now']

    #the function will be rewrited
    def visit_importfrom(self, node):
        names = [name for name, _alias in node.names]
        for item in names:
            for check in self.forbidden_import_from:
                if item == check:
                    self.add_message('W0001', node=node, args=item)

    def visit_import(self, node):
        names = [name for name, _ in node.names]
        for item in names:
            for check in self.forbidden_import:
                if check == item:
                    self.add_message('W0001', node=node, args=item)

    def visit_attribute(self, node):
        for check_attr in self.forbidden_import_attribute:
            if check_attr == node.as_string():
                self.add_message('W0001', node=node, args=check_attr)


def register(linter):
    linter.register_checker(TestChecker(linter))

我有两种自定义pylint的方法

使用配置文件 第一条路是你要去的地方

  • 调用pylint生成模板配置文件
  • 然后根据您的需要定制配置文件
  • 然后将配置文件放在默认的pylint配置文件位置,或者始终调用pylint并指定配置文件路径
使用包装器脚本 第二种方法是创建一个调用pylint的包装器脚本,在包装器脚本中有一系列行,如下所示:

pylint \
       ${options_here} \
       --disable=xyz1 \
       --disable=xyz_2 \
       ${more_options} \
       --disable=xyz_N \
       --disable=abc \
       $@

目前我正在使用包装脚本方法,因为我希望问题按行号排序,并且我编写了一些shell脚本以获得排序顺序。

关于pradyunsg的上述答案,下面是CamelCase的另一个正则表达式:

^([a-z]\w+[A-Z]+\w+)

(取自PyLint的
spelling.py
检查器,位于:
%APPDATA%-本地-程序-Python-[PythonVersion]-库-站点包-PyLint-检查器
文件夹)

请不要使用camelCase。虽然您的评论是合理的,但我与一个主要由java开发人员组成的团队在一起,为了提高其可维护性/可接受性,我不得不偏离pythonic代码。在某些情况下,camelCase是合理的。例如,在使用PySide或PyQt时。将Qt-ish camel-case与python-s混合使用看起来很糟糕style.@SumitBisht战胜所有代码风格规则的规则是“保持一致”。因此,你做了正确的事情:)。是的,混合风格是一个很大的“不”,实际上是代码气味的一个标志。在任何代码库中保持一致性都是关键。答案很好,但还有更多。如果您希望跨多个项目和更复杂的场景对pylint进行细粒度控制,请参阅我的答案。我在终端上尝试了
pylint--disable=C0111
,得到了
用法:pylint[options]模块或软件包
。我错过了什么吗?@MelvicYbanez是的,你确实错过了什么;)您有
pylint[options]
部分,但没有
module\u或\u包
部分。