[code-quality] Question about pylint plugin
Claudiu Popa
pcmanticore at gmail.com
Tue Oct 20 05:52:54 EDT 2015
On Tue, Oct 20, 2015 at 11:38 AM, Michel RENON <renon at mr-consultant.net> wrote:
> Hi,
>
> I'm trying to write a pylint checker and I have a problem.
> Based on several examples found on internet, I wrote a checker skeleton.
> It is loaded, but it never get called to check.
> In the following example, the '__init__()' method is called, but other
> methods 'open()', 'visit_class()', 'visit_function()' and 'visit_callfunc()'
> are never called.
>
> As the checker is loaded, it's not a problem in the pylint command.
>
> Do you have any clues to solve that problem ?
>
>
>
>
> Here are the 3 files (all stored in the same folder):
> first, the checker : "fields_checker.py"
> 8<-----------------------------------------
> import astroid
> from pylint.interfaces import IAstroidChecker
> from pylint.checkers import BaseChecker
>
> class MyFieldsChecker(BaseChecker):
> """Checks for my fields
> """
>
> __implements__ = (IAstroidChecker,)
>
> name = 'my-fields-checker'
> msgs = {}
>
> def __init__(self, linter=None):
> BaseChecker.__init__(self, linter)
> print("MyFieldsChecker.__init__()")
>
> def open(self):
> print("MyFieldsChecker.open() ")
>
> def visit_attribute(self, node):
> print("MyFieldsChecker.visit_Attribute : {}".format(dir(node)))
> self.generic_visit(node)
>
>
> def visit_class (self, node):
> print("MyFieldsChecker.visit_class : {}".format(dir(node)))
> self.generic_visit(node)
>
> def visit_function(self, node):
> print("MyFieldsChecker.visit_function : {}".format(dir(node)))
> self.generic_visit(node)
>
>
> def visit_callfunc(self, node):
>
> print("MyFieldsChecker.visit_callfunc : {}".format(dir(node)))
> self.generic_visit(node)
>
>
> def register(linter):
> """required method to auto register this checker """
> print("DEBUG register MyFieldsChecker")
> linter.register_checker(MyFieldsChecker(linter))
> 8<-----------------------------------------
>
> then the pylint command, executed on Ubuntu 14.04 : "do.sh"
> 8<-----------------------------------------
> PYTHONPATH=${PYTHONPATH}:$(pwd)
> export PYTHONPATH
>
> pylint --load-plugins=fields_checker -r n test.py
> 8<-----------------------------------------
>
>
> and just fyi, the tested python file : "test.py"
>
> 8<-----------------------------------------
> def method1(p, q, r):
> print p, q, r
>
> method1('a', 'b', 'c')
>
> class Test(object):
>
> def __init__(self):
> self.f1 = "a"
>
>
> _myfields = {
> 'f2' : "azerty",
> }
>
> t = Test()
> 8<-----------------------------------------
>
> and the result :
> 8<-----------------------------------------
> $ sh do.sh
> DEBUG register MyFieldsChecker
> MyFieldsChecker.__init__()
> ************* Module test
> C: 1, 0: Missing module docstring (missing-docstring)
> C: 3, 0: Invalid argument name "p" (invalid-name)
> C: 3, 0: Invalid argument name "q" (invalid-name)
> C: 3, 0: Invalid argument name "r" (invalid-name)
> C: 3, 0: Missing function docstring (missing-docstring)
> C: 12, 8: Invalid attribute name "f1" (invalid-name)
> C: 9, 0: Missing class docstring (missing-docstring)
> R: 9, 0: Too few public methods (0/2) (too-few-public-methods)
> C: 21, 0: Invalid constant name "t" (invalid-name)
> 8<-----------------------------------------
>
>
>
> Thanks,
>
> Michel
> _______________________________________________
> code-quality mailing list
> code-quality at python.org
> https://mail.python.org/mailman/listinfo/code-quality
Hi Michel,
Your checker needs to define at least one message in order
to be executed. For instance, adding a dummy msg such as
msgs = {'E9999': ('a', 'b', 'c')} makes it run as expected.
Hope this helps,
Claudiu.
More information about the code-quality
mailing list