Can Pylint error checking be customized?

爷,独闯天下 提交于 2020-05-24 17:09:48

问题


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?


回答1:


You can globally disable warnings of a certain class using

pylint --disable=W1234

or by using a special PyLint configuration file

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.




回答2:


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.

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.




回答3:


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

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

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



回答4:


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



回答5:


There are two ways I customize pylint.

Using a config file

The first way is where you

  • call pylint to generate a template config file
  • then you tailor the config file to your needs/wants
  • then you put the config file in your the default pylint config file location or always call pylint and specify the config file path

Using a wrapper script

The second way is where you create a wrapper script that calls pylint and in the wrapper script you have a bunch of lines that look like:

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

Currently I am using the wrapper script approach because I want the issues to be sorted by line number and I did some shell scripting to get that sorting order.




回答6:


Further to the above answer from pradyunsg, here is another regex for CamelCase:

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

(Taken from PyLint's spelling.py checker, found in: %APPDATA% - Local - Programs - Python - [PythonVersion] - Lib - site-packages - pylint - checkers folder)



来源:https://stackoverflow.com/questions/10138917/can-pylint-error-checking-be-customized

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