2014-02-27 08:55:33 +00:00
|
|
|
.. -*- coding: utf-8 -*-
|
|
|
|
|
2017-07-12 04:45:16 +00:00
|
|
|
How To Write a Pylint Plugin
|
|
|
|
============================
|
|
|
|
|
|
|
|
Pylint provides support for writing two types of extensions.
|
|
|
|
First, there is the concept of **checkers**,
|
|
|
|
which can be used for finding problems in your code.
|
|
|
|
Secondly, there is also the concept of **transform plugin**,
|
|
|
|
which represents a way through which the inference and
|
|
|
|
the capabilities of Pylint can be enhanced
|
2016-08-14 13:24:55 +00:00
|
|
|
and tailored to a particular module, library of framework.
|
2016-07-03 17:39:13 +00:00
|
|
|
|
2016-08-14 13:24:55 +00:00
|
|
|
In general, a plugin is a module which should have a function ``register``,
|
|
|
|
which takes an instance of ``pylint.lint.PyLinter`` as input.
|
2016-07-03 17:39:13 +00:00
|
|
|
|
2016-06-29 16:49:50 +00:00
|
|
|
So a basic hello-world plugin can be implemented as:
|
2014-02-27 08:55:33 +00:00
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
# Inside hello_plugin.py
|
|
|
|
def register(linter):
|
|
|
|
print 'Hello world'
|
|
|
|
|
2016-08-14 17:51:46 +00:00
|
|
|
We can run this plugin by placing this module in the PYTHONPATH and invoking
|
|
|
|
**pylint** as:
|
2014-02-27 08:55:33 +00:00
|
|
|
|
|
|
|
.. sourcecode:: bash
|
|
|
|
|
2016-08-14 13:24:55 +00:00
|
|
|
$ pylint -E --load-plugins hello_plugin foo.py
|
2014-02-27 08:55:33 +00:00
|
|
|
Hello world
|
|
|
|
|
2016-08-14 13:24:55 +00:00
|
|
|
Depending if we need a **transform plugin** or a **checker**, this might not
|
|
|
|
be enough. For the former, this is enough to declare the module as a plugin,
|
|
|
|
but in the case of the latter, we need to register our checker with the linter
|
|
|
|
object, by calling the following inside the ``register`` function::
|
2014-02-27 08:55:33 +00:00
|
|
|
|
2016-08-14 13:24:55 +00:00
|
|
|
linter.register_checker(OurChecker(linter))
|
2014-02-27 08:55:33 +00:00
|
|
|
|
2017-10-11 03:15:46 +00:00
|
|
|
For more information on writing a checker see :ref:`write_a_checker`.
|