是否可以定制Pylint错误检查?

时间:2022-04-01 16:46:36

I am using pydev where I have set up pylint. The problem is that even inside the comments, pylint reports warnings. I was looking to disable any sort of checking inside any line or a block comment. Also, I wish to follow camelCase naming convention instead of underscores for variables and arguments in my code. Is there any way to specify such a rule without inserting my code with any pylint: disable comments?

我正在使用pydev,我在那里设置了pylint。问题是,即使在评论中,pylint也会报告警告。我想禁用任何行或块注释中的任何检查。此外,我希望遵循camelCase命名约定,而不是在代码中为变量和参数下划线。有什么方法可以在不使用任何pylint:禁用注释的情况下指定这样的规则吗?

4 个解决方案

#1


40  

You can globally disable warnings of a certain class using

您可以使用全局禁用某个类的警告。

pylint --disable=W1234

or by using a special PyLint configuration file

或者使用一个特殊的PyLint配置文件

pylint --rcfile=/path/to/config.file

A sample config file is given below:

配置文件示例如下:

[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

See the documentation over at Pylint's dedicated site.

请参阅Pylint专用站点上的文档。

#2


18  

As said by cfedermann, you can specify messages to be disabled in a ~/.pylintrc file (notice you can generate a stub file using pylint --generate-rcfile if you don't want to use inline comments.

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

You'll also see in the generated file, in the [BASIC] section, options like "method-rgx", "function-rgx", etc. which you can configure as you like to support camel cases style rather than pep8 underscore style.

您还可以在生成的文件中看到,在[BASIC]部分中,可以选择“method-rgx”、“function-rgx”等选项,您可以将其配置为支持camel case风格,而不是pep8下划线样式。

#3


3  

Although this is an old question, it should be mentioned one can now specify their own regex for matching with names.

尽管这是一个老问题,但应该提到的是,现在可以指定自己的regex来与名称匹配。

Then your regex to match camel case would be something like:

那么您的regex将匹配骆驼案例如下:

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

#4


0  

Here is the custom check example, and another example easier to understand.

这里是自定义检查示例,另一个示例更容易理解。

I was facing a problem similar to you. The following code is my solution. I customized one checker to forbidden import datetime.now. You can take it for reference :

我遇到了一个和你类似的问题。下面的代码是我的解决方案。我为禁止导入日期时间定制了一个检查器。你可以把它作为参考:

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

#1


40  

You can globally disable warnings of a certain class using

您可以使用全局禁用某个类的警告。

pylint --disable=W1234

or by using a special PyLint configuration file

或者使用一个特殊的PyLint配置文件

pylint --rcfile=/path/to/config.file

A sample config file is given below:

配置文件示例如下:

[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

See the documentation over at Pylint's dedicated site.

请参阅Pylint专用站点上的文档。

#2


18  

As said by cfedermann, you can specify messages to be disabled in a ~/.pylintrc file (notice you can generate a stub file using pylint --generate-rcfile if you don't want to use inline comments.

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

You'll also see in the generated file, in the [BASIC] section, options like "method-rgx", "function-rgx", etc. which you can configure as you like to support camel cases style rather than pep8 underscore style.

您还可以在生成的文件中看到,在[BASIC]部分中,可以选择“method-rgx”、“function-rgx”等选项,您可以将其配置为支持camel case风格,而不是pep8下划线样式。

#3


3  

Although this is an old question, it should be mentioned one can now specify their own regex for matching with names.

尽管这是一个老问题,但应该提到的是,现在可以指定自己的regex来与名称匹配。

Then your regex to match camel case would be something like:

那么您的regex将匹配骆驼案例如下:

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

#4


0  

Here is the custom check example, and another example easier to understand.

这里是自定义检查示例,另一个示例更容易理解。

I was facing a problem similar to you. The following code is my solution. I customized one checker to forbidden import datetime.now. You can take it for reference :

我遇到了一个和你类似的问题。下面的代码是我的解决方案。我为禁止导入日期时间定制了一个检查器。你可以把它作为参考:

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