Hi, Where can i find a bare minimum pylint checker that will successfully be registered and called? I've tried to write a checker like so: $ cat mychecker.py from pylint import checkers import pdb class Foo(checkers.BaseChecker): name = "FooBar" def visit_module(self, node): self.add_message('blaha-name', node=node) pdb.set_trace() def visit_importfrom(self, node): self.add_message('foo-name', node=node) pdb.set_trace() def visit_import(self, node): self.add_message('bar-name', node=node) pdb.set_trace() def register(linter): linter.register_checker(Foo(linter)) print "Mychecker registered successfully it seems" I get the print from register but neither of my visit_* methods seems to get called, i cannot see why. I run it like so: PYTHONPATH=. pylint --load-plugins=mychecker -rn main.py testmodule What am i missing? Regards, Patrik Iselind
On Sat, Aug 13, 2016 at 11:31 AM, mrx <patrik.mrx@gmail.com> wrote:
Hi,
Where can i find a bare minimum pylint checker that will successfully be registered and called?
I've tried to write a checker like so: $ cat mychecker.py from pylint import checkers import pdb
class Foo(checkers.BaseChecker):
name = "FooBar"
def visit_module(self, node): self.add_message('blaha-name', node=node) pdb.set_trace()
def visit_importfrom(self, node): self.add_message('foo-name', node=node) pdb.set_trace()
def visit_import(self, node): self.add_message('bar-name', node=node) pdb.set_trace()
def register(linter): linter.register_checker(Foo(linter)) print "Mychecker registered successfully it seems"
I get the print from register but neither of my visit_* methods seems to get called, i cannot see why.
I run it like so: PYTHONPATH=. pylint --load-plugins=mychecker -rn main.py testmodule
What am i missing?
Hi, You will have to provide as well the message dictionary that you checker is going to emit. I just documented this here: https://docs.pylint.org/en/latest/custom_checkers.html#writing-your-own-chec... This part needs more documentation work though, so if you notice any other inconsistency, let me know. Claudiu
Thanks, i got it to work now. I have some feedback on the page you referred to. I was missing the implements line as well. This was not highlighted at all. Bullet two concerning priority. How checkers are ordered (internally?) doesn't help me to decide if i should set a negative priority close to zero or very far from zero in order for the checker to be first or at least among the first. I interpret this page as a resource you'd give programmers new to pylint checkers to get them started, awesome start! I think it would be really helpful if the page gave a overview of how the AST is buid and how i'm meant to access for example function variables in a function or the identifiers defined in a module. Where can i find a list of all the visit_* methods that i can use? I cannot find any class defining them all, are they dynamically called somehow? In examples directory referred to, looking at custom_raw.py. It implements process_module() instead of visit_*. What is the difference? When should i choose one over the other? Thanks a lot for your assistance in getting my first checker running. // Patrik Den 2016-08-14 kl. 15:32, skrev Claudiu Popa:
On Sat, Aug 13, 2016 at 11:31 AM, mrx <patrik.mrx@gmail.com <mailto:patrik.mrx@gmail.com>> wrote:
Hi,
Where can i find a bare minimum pylint checker that will successfully be registered and called?
I've tried to write a checker like so: $ cat mychecker.py from pylint import checkers import pdb
class Foo(checkers.BaseChecker):
name = "FooBar"
def visit_module(self, node): self.add_message('blaha-name', node=node) pdb.set_trace()
def visit_importfrom(self, node): self.add_message('foo-name', node=node) pdb.set_trace()
def visit_import(self, node): self.add_message('bar-name', node=node) pdb.set_trace()
def register(linter): linter.register_checker(Foo(linter)) print "Mychecker registered successfully it seems"
I get the print from register but neither of my visit_* methods seems to get called, i cannot see why.
I run it like so: PYTHONPATH=. pylint --load-plugins=mychecker -rn main.py testmodule
What am i missing?
Hi,
You will have to provide as well the message dictionary that you checker is going to emit. I just documented this here: https://docs.pylint.org/en/latest/custom_checkers.html#writing-your-own-chec...
This part needs more documentation work though, so if you notice any other inconsistency, let me know.
Claudiu
On Wed, Aug 17, 2016 at 10:37 PM, Patrik Iselind <patrik.mrx@gmail.com> wrote:
Thanks, i got it to work now.
I have some feedback on the page you referred to.
I was missing the implements line as well. This was not highlighted at all.
Bullet two concerning priority. How checkers are ordered (internally?) doesn't help me to decide if i should set a negative priority close to zero or very far from zero in order for the checker to be first or at least among the first.
I interpret this page as a resource you'd give programmers new to pylint checkers to get them started, awesome start! I think it would be really helpful if the page gave a overview of how the AST is buid and how i'm meant to access for example function variables in a function or the identifiers defined in a module. Where can i find a list of all the visit_* methods that i can use? I cannot find any class defining them all, are they dynamically called somehow?
In examples directory referred to, looking at custom_raw.py. It implements process_module() instead of visit_*. What is the difference? When should i choose one over the other?
Thanks a lot for your assistance in getting my first checker running.
// Patrik
Hi Patrik, These are all really good questions! Would you mind adding your feedback over this issue (https://github.com/PyCQA/pylint/issues/1071), where I am tracking the documentation effort for writing a new checker? In the mean time, some answers for your questions: - probably you shouldn't care that much about the priority, I don't know what its initial purpose was, so I presume it could be removed at some point. For what I care, each checker should not care about its order of execution, especially when considering calling them in a parallel environment. - sure, we need to document these as well. Basically, any node from the builtin ast module should be available in pylint as well, under a visit_<node name> method. - the process_module method is called whenever the checker is also a raw checker. The checkers can be AST checkers, operating on trees, token checkers, operating on tokens and raw checkers, operating on the file content itself. A checker can be all of these at the same time, if you declare the right __implements__. process_module is usually used when you need the entire content of the file for some reason. Of course, this can also be implemented with a visit_module() and getting the string representation of the module node, although we have some problems now with that, since we can't respect the invariant that ast.to_string() == file content.
Done, thanks again! Patrik Iselind On Wed, Aug 17, 2016 at 11:05 PM, Claudiu Popa <pcmanticore@gmail.com> wrote:
On Wed, Aug 17, 2016 at 10:37 PM, Patrik Iselind <patrik.mrx@gmail.com> wrote:
Thanks, i got it to work now.
I have some feedback on the page you referred to.
I was missing the implements line as well. This was not highlighted at all.
Bullet two concerning priority. How checkers are ordered (internally?) doesn't help me to decide if i should set a negative priority close to zero or very far from zero in order for the checker to be first or at least among the first.
I interpret this page as a resource you'd give programmers new to pylint checkers to get them started, awesome start! I think it would be really helpful if the page gave a overview of how the AST is buid and how i'm meant to access for example function variables in a function or the identifiers defined in a module. Where can i find a list of all the visit_* methods that i can use? I cannot find any class defining them all, are they dynamically called somehow?
In examples directory referred to, looking at custom_raw.py. It implements process_module() instead of visit_*. What is the difference? When should i choose one over the other?
Thanks a lot for your assistance in getting my first checker running.
// Patrik
Hi Patrik,
These are all really good questions!
Would you mind adding your feedback over this issue (https://github.com/PyCQA/pylint/issues/1071), where I am tracking the documentation effort for writing a new checker?
In the mean time, some answers for your questions:
- probably you shouldn't care that much about the priority, I don't know what its initial purpose was, so I presume it could be removed at some point. For what I care, each checker should not care about its order of execution, especially when considering calling them in a parallel environment.
- sure, we need to document these as well. Basically, any node from the builtin ast module should be available in pylint as well, under a visit_<node name> method.
- the process_module method is called whenever the checker is also a raw checker. The checkers can be AST checkers, operating on trees, token checkers, operating on tokens and raw checkers, operating on the file content itself. A checker can be all of these at the same time, if you declare the right __implements__. process_module is usually used when you need the entire content of the file for some reason. Of course, this can also be implemented with a visit_module() and getting the string representation of the module node, although we have some problems now with that, since we can't respect the invariant that ast.to_string() == file content.
participants (3)
-
Claudiu Popa
-
mrx
-
Patrik Iselind