summaryrefslogtreecommitdiff
path: root/doc/development_guide/api/pylint.rst
blob: 000006ac82cd06ca0022be1a6567830dfc250522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
=======
 Pylint
=======

As you would launch the command line
------------------------------------

You can use the ``run_pylint`` function, which is the same function
called by the command line (using ``sys.argv``). You can supply
arguments yourself:

.. sourcecode:: python

    from pylint import run_pylint

    run_pylint(argv=["--disable=line-too-long", "myfile.py"])


Recover the result in a stream
------------------------------

You can also use ``pylint.lint.Run`` directly if you want to do something that
can't be done using only pylint's command line options. Here's the basic example:

.. sourcecode:: python

    from pylint.lint import Run

    Run(argv=["--disable=line-too-long", "myfile.py"])

With ``Run`` it is possible to invoke pylint programmatically with a
reporter initialized with a custom stream:

.. sourcecode:: python

    from io import StringIO

    from pylint.lint import Run
    from pylint.reporters.text import TextReporter

    pylint_output = StringIO()  # Custom open stream
    reporter = TextReporter(pylint_output)
    Run(["test_file.py"], reporter=reporter, exit=False)
    print(pylint_output.getvalue())  # Retrieve and print the text report

The reporter can accept any stream object as as parameter. In this example,
the stream outputs to a file:

.. sourcecode:: python

    from pylint.lint import Run
    from pylint.reporters.text import TextReporter

    with open("report.out", "w") as f:
        reporter = TextReporter(f)
        Run(["test_file.py"], reporter=reporter, exit=False)

This would be useful to capture pylint output in an open stream which
can be passed onto another program.

If your program expects that the files being linted might be edited
between runs, you will need to clear pylint's inference cache:

.. sourcecode:: python

    from pylint.lint import pylinter
    pylinter.MANAGER.clear_cache()