~ubuntu-branches/ubuntu/trusty/pylint/trusty

« back to all changes in this revision

Viewing changes to doc/manual.txt

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Thénault
  • Date: 2006-09-25 16:46:40 UTC
  • mfrom: (1.2.1 upstream) (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20060925164640-obkb6g34gqtyk20n
new uptream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
==================
 
2
Pylint User Manual
 
3
==================
 
4
 
 
5
:Author: Sylvain Th�nault
 
6
:Author: Alexandre Fayolle
 
7
:Organization: Logilab:Version: $Revision: 1.10 $
 
8
:Date: $Date: 2005-04-15 10:40:17 $
 
9
 
 
10
.. contents::
 
11
 
 
12
 
 
13
This document is meant to be the reference user manual for Pylint_.
 
14
This is a work in progress so some sections or parts may be missing
 
15
(sometimes marked by a XXX).  If you think it's lacking some important
 
16
information, please talk about it on the python-projects mailing list.
 
17
 
 
18
.. _Pylint: http://www.logilab.org/projects/pylint
 
19
 
 
20
Introduction
 
21
============
 
22
 
 
23
What is pylint?
 
24
---------------
 
25
 
 
26
Pylint is a tool that checks for errors in python code, tries to
 
27
enforce a coding standard and looks for smelling code . This is
 
28
similar but nevertheless different from what pychecker_ provides,
 
29
especially since pychecker explicitely does not bother with coding
 
30
style. The default coding style used by pylint is close to `PEP 008`_
 
31
(aka `Guido's style guide`_). For more information about code smells,
 
32
refer to Martin Fowler's `refactoring book`_
 
33
 
 
34
One important thing to note is that Pylint isn't smarter than you are:
 
35
it may warn you about things that you have consciensiously
 
36
done. That's for example because it tries to detect things that may be
 
37
dangerous in a context, but maybe not in others, or because it checks
 
38
for some things that you don't care about. Generally, you shouldn't
 
39
expect to be pylint totally quiet about your code, so don't
 
40
necessarily be alarmed if it give you a hell lot of messages for your
 
41
proudly(XXX) project ;)
 
42
 
 
43
Pylint will display a number of messages as it analyzes the code, as
 
44
well as some statistics about the number of warnings and errors found
 
45
in different files. The messages are classified under various
 
46
categories such as errors and warnings (more below). If you run pylint
 
47
twice, it will display the statistics from the previous run together
 
48
with the ones from the current run, so that you can see if the code
 
49
has improved or not.
 
50
 
 
51
Last but not least, the code is given an overall mark, based on the
 
52
number an severity of the warnings and errors. This has proven to
 
53
be very motivating for programmers.
 
54
 
 
55
.. _pychecker: http://pychecker.sf.net
 
56
.. _`PEP 008`: http://www.python.org/dev/peps/pep-0008/
 
57
.. _`Guido's style guide`: http://www.python.org/doc/essays/styleguide.html
 
58
.. _`refactoring book`: http://www.refactoring.com/
 
59
 
 
60
Installation
 
61
------------
 
62
 
 
63
Dependancies
 
64
''''''''''''
 
65
Pylint requires the latest `logilab-astng`_ and `logilab-common`_
 
66
packages. It should be compatible with any python version greater than
 
67
2.2.0 (python 2.2 users will have to install the optik_ package).
 
68
 
 
69
.. _`logilab-astng`: http://www.logilab.org/projects/astng
 
70
.. _`logilab-common`: http://www.logilab.org/projects/common
 
71
.. _optik: http://optik.sourceforge.net/
 
72
 
 
73
 
 
74
Distributions
 
75
'''''''''''''
 
76
The source tarball is available at ftp://ftp.logilab.fr/pub/pylint.
 
77
 
 
78
You may apt-get a debian package by adding ::
 
79
 
 
80
    deb ftp://ftp.logilab.org/pub/debian unstable/
 
81
 
 
82
to your */etc/apt/sources.list* file. Pylint is also available in the 
 
83
standard Debian distribution (but add our public debian repository
 
84
anyway if you want to get the latest releases and upgrades earlier)
 
85
 
 
86
Contributed RPM packages for pylint and logilab-common are available at
 
87
ftp://ftp.nest.pld-linux.org/test.
 
88
 
 
89
Pylint is also available in Gentoo, Fedora 4, Ubuntu, FreeBSD, Darwin
 
90
(and maybe other, if si drop us a note please!). 
 
91
 
 
92
 
 
93
Source distribution installation
 
94
''''''''''''''''''''''''''''''''
 
95
From the source distribution, extract the tarball, go to the extracted
 
96
directory and simply run ::
 
97
 
 
98
    python setup.py install
 
99
 
 
100
You'll have to install dependancies in a similar way.
 
101
 
 
102
 
 
103
Note for Windows users
 
104
''''''''''''''''''''''
 
105
On Windows, once you have installed pylint, the command line usage is
 
106
::
 
107
 
 
108
  pylint.bat [options] module_or_package
 
109
 
 
110
But this will only work if *pylint.bat* is either in the current
 
111
directory, or on your system path. (*setup.py* install install *python.bat*
 
112
to the *Scripts* subdirectory of your Python installation -- e.g.
 
113
C:\Python24\Scripts.) You can do any of the following to solve this:
 
114
 
 
115
1. change to the appropriate directory before running pylint.bat
 
116
 
 
117
2. add the Scripts directory to your path statement in your autoexec.bat
 
118
   file (this file is found in the root directory of your boot-drive)
 
119
 
 
120
3. create a 'redirect' batch file in a directory actually on your
 
121
   systems path
 
122
 
 
123
To effect (2), simply append the appropriate directory name to the PATH=
 
124
statement in autoexec.bat. Be sure to use the Windows directory
 
125
separator of ';' between entries. Then, once you have rebooted (this is
 
126
necessary so that the new path statement will take effect when
 
127
autoexec.bat is run), you will be able to invoke PyLint with
 
128
pylint.bat on the command line.
 
129
 
 
130
(3) is the best solution. Once done, you can call pylint at the command
 
131
line without the .bat, just as do non-Windows users by typing: ::
 
132
 
 
133
  pylint [options] module_or_package
 
134
 
 
135
To effect option (3), simply create a plain text file pylint.bat with
 
136
the single line: ::
 
137
 
 
138
  C:\PythonDirectory\Scripts\pylint.bat
 
139
 
 
140
(where PythonDirectory is replaced by the actual Python installation
 
141
directory on your system -- e.g. C:\Python24\Scripts\pylint.bat).
 
142
 
 
143
 
 
144
Invoking pylint
 
145
---------------
 
146
 
 
147
Pylint is meant to be called from the commant line. The usage is ::
 
148
 
 
149
   pylint [options] module_or_package
 
150
 
 
151
You should give pylint the name of a Python package or module. Pylint
 
152
will ``import`` this package or module, so you should pay attention to
 
153
your ``PYTHONPATH``, since it is a common error to analyze an
 
154
installed version of a module instead of the development version.
 
155
 
 
156
It is also possible to analyze python files, with a few
 
157
restriction. The thing to keep in mind is that pylint will try to 
 
158
convert the file name to a module name, and only be able to process
 
159
the file if it succeeds.  ::
 
160
 
 
161
  pylint mymodule.py 
 
162
 
 
163
should always works since the current working
 
164
directory is automatically added on top  of the python path ::
 
165
 
 
166
  pylint directory/mymodule.py
 
167
 
 
168
will work if "directory" is a python package (i.e. has an __init__.py
 
169
file) or if "directory" is in the python path.
 
170
 
 
171
For more details on this see the FAQ_.
 
172
 
 
173
You can also start a thin gui around pylint (require TkInter) by
 
174
typing ::
 
175
 
 
176
  pylint-gui
 
177
 
 
178
This should open a window where you can enter the name of the package
 
179
or module to check, at pylint messages will be displayed in the user
 
180
interface.
 
181
 
 
182
 
 
183
Pylint output
 
184
-------------
 
185
 
 
186
The default format for the output is raw text. But passing pylint the
 
187
``--html`` option will produce an HTML document.
 
188
 
 
189
There are several sections in pylint's output. 
 
190
 
 
191
Source code analysis section
 
192
''''''''''''''''''''''''''''
 
193
For each python module,
 
194
pylint will first display a few '*' characters followed by the name
 
195
of the module. Then, a number of messages with the following
 
196
format: ::
 
197
 
 
198
  MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
 
199
 
 
200
You can get another output format, useful since it's recognized by
 
201
most editors or other development tools using the ``--parseable=y``
 
202
option.
 
203
 
 
204
The message type can be:
 
205
 
 
206
  * [R]efactor for a "good practice" metric violation
 
207
  * [C]onvention for coding standard violation
 
208
  * [W]arning for stylistic problems, or minor programming issues
 
209
  * [E]rror for important programming issues (i.e. most probably bug)
 
210
  * [F]atal for errors which prevented further processing
 
211
 
 
212
Sometimes the line of code which caused the error is displayed with
 
213
a caret pointing to the error. This may be generalized in future
 
214
versions of pylint.
 
215
 
 
216
Example (extracted from a run of pylint on itself...): 
 
217
 
 
218
::
 
219
 
 
220
  ************* Module pylint.checkers.format
 
221
  W: 50: Too long line (86/80)
 
222
  W:108: Operator not followed by a space
 
223
       print >>sys.stderr, 'Unable to match %r', line
 
224
              ^
 
225
  W:141: Too long line (81/80)
 
226
  W: 74:searchall: Unreachable code
 
227
  W:171:FormatChecker.process_tokens: Redefining built-in (type)
 
228
  W:150:FormatChecker.process_tokens: Too many local variables (20/15)
 
229
  W:150:FormatChecker.process_tokens: Too many branchs (13/12)
 
230
 
 
231
 
 
232
Reports section
 
233
''''''''''''''' 
 
234
Following the analysis message, pylint will display a set of report,
 
235
each one focusing on a particular aspect of the project, such as number
 
236
of messages by categories, modules dependancies...
 
237
 
 
238
For instance, the metrics report displays summaries gathered from the
 
239
current 
 
240
run. 
 
241
 
 
242
  * the number of processed modules
 
243
  * for each module, the percentage of errors and warnings
 
244
  * the total number of errors and warnings
 
245
  * percentage of classes, functions and modules with docstrings, and
 
246
    a comparison from the previous run 
 
247
  * percentage of classes, functions and modules with correct name
 
248
  * (according the the coding standard), and a comparison from the
 
249
    previous run 
 
250
  * a list of external dependencies found in the code, and where they appear
 
251
 
 
252
Also, a global evaluation for the code is computed, and an
 
253
optional witty comment is displayed (if ``--comment=y`` was
 
254
specified on the command line).
 
255
 
 
256
 
 
257
 
 
258
Command line options
 
259
--------------------
 
260
 
 
261
First of all, we have two basic (but useful) options. 
 
262
 
 
263
--version             show program's version number and exit
 
264
-h, --help            show help about the command line options
 
265
 
 
266
Pylint is architectured around several checkers. By default all
 
267
checkers are enabled. You can disable a specific checker by specifying
 
268
``--enable-<checker>=n``, or disable all checkers using
 
269
``--disable-all`` and afterwards enable specific checkers with
 
270
``--enable-<checker>=y``. See the list of available features_ for a
 
271
description of provided checkers with their functionalities.
 
272
 
 
273
Each checker has some specific options, which can take either a yes/no
 
274
value, an integer, a python regular expression, or a comma separated
 
275
list of values (which are generally used to override a regular
 
276
expression in special cases). For a full list of options, use ``--help``
 
277
 
 
278
Specifying all the options suitable for your setup and coding
 
279
standards can be tedious, so it is possible to use a rc file to
 
280
specify the default values. Pylint looks for /etc/pylintrc and
 
281
~/.pylintrc. The ``--generate-rcfile`` option will generate a
 
282
commented configuration file according to the current configuration on
 
283
standard output and exit. You can put other options before this one to
 
284
use them in the configuration, or start with the default values and
 
285
hand tune the configuration.
 
286
 
 
287
Other useful global options include:
 
288
 
 
289
--zope                  Initialize Zope products before starting
 
290
--ignore=file           Add <file> (may be a directory) to the black
 
291
                          list. It should be a base name, not a path. 
 
292
                          You may set this option multiple times.
 
293
--statistics=y_or_n     Compute statistics on collected data.
 
294
--persistent=y_or_n     Pickle collected data for later comparisons.
 
295
--comment=y_or_n        Add a comment according to your evaluation note.
 
296
--parseable=y_or_n      Use a parseable output format.
 
297
--html=y_or_n           Use HTML as output format instead of text.
 
298
--enable-msg=msgids     Enable the given messages.
 
299
--disable-msg=msgids    Disable the given messages.
 
300
--enable-msg-cat=cats   Enable all messages in the given categories.
 
301
--disable-msg-cat=cats  Disable all messages in the given categories.
 
302
--errors-only           Enable only checkers from the error category.
 
303
 
 
304
.. _features: features.html
 
305
 
 
306
Bug reports
 
307
-----------
 
308
XXX tracker
 
309
You think you have found a bug in Pylint? Well, this may be the case
 
310
since Pylint is under development. Please take the time to send a bug
 
311
report to python-projects@logilab.org. This mailing list is also a
 
312
nice place to discuss Pylint issues.
 
313
 
 
314
 
 
315
Comments, feedback, support...
 
316
------------------------------
 
317
Use the python-projects@logilab.org mailing list for anything related
 
318
to Pylint. This is in most case better than sending an email directly
 
319
to the author, since others will benefit from the exchange, and you'll
 
320
be more likely answered by someone subscribed to the list.
 
321
 
 
322
You can subscribe to this mailing list at
 
323
http://www.logilab.org/mailinglists/python_projects/mailinglist_register_form
 
324
 
 
325
Archives are available at 
 
326
http://lists.logilab.org/pipermail/python-projects/
 
327
 
 
328
If you prefer speaking french instead of english, you can use the
 
329
generic forum-fr@logilab.org mailing list.
 
330
 
 
331
 
 
332
Advanced usage
 
333
==============
 
334
 
 
335
Base configuration
 
336
------------------
 
337
 
 
338
Environment
 
339
-----------
 
340
 
 
341
Messages control
 
342
----------------
 
343
 
 
344
An example available from the examples directory::
 
345
 
 
346
    """pylint option block-disable-msg"""
 
347
 
 
348
    __revision__ = None
 
349
 
 
350
    class Foo(object):
 
351
        """block-disable-msg test"""
 
352
 
 
353
        def __init__(self):
 
354
            pass
 
355
 
 
356
        def meth1(self, arg):
 
357
            """this issues a message"""
 
358
            print self
 
359
 
 
360
        def meth2(self, arg):
 
361
            """and this one not"""
 
362
            # pylint: disable-msg=W0613
 
363
            print self\
 
364
                  + "foo"
 
365
 
 
366
        def meth3(self):
 
367
            """test one line disabling"""
 
368
            # no error
 
369
            print self.bla # pylint: disable-msg=E1101
 
370
            # error
 
371
            print self.blop 
 
372
 
 
373
        def meth4(self):
 
374
            """test re-enabling"""
 
375
            # pylint: disable-msg=E1101
 
376
            # no error
 
377
            print self.bla
 
378
            print self.blop 
 
379
            # pylint: enable-msg=E1101
 
380
            # error
 
381
            print self.blip
 
382
 
 
383
        def meth5(self):
 
384
            """test IF sub-block re-enabling"""
 
385
            # pylint: disable-msg=E1101
 
386
            # no error
 
387
            print self.bla
 
388
            if self.blop:
 
389
                # pylint: enable-msg=E1101
 
390
                # error
 
391
                print self.blip
 
392
            else:
 
393
                # no error
 
394
                print self.blip
 
395
            # no error
 
396
            print self.blip
 
397
 
 
398
        def meth6(self):
 
399
            """test TRY/EXCEPT sub-block re-enabling"""
 
400
            # pylint: disable-msg=E1101
 
401
            # no error
 
402
            print self.bla
 
403
            try:
 
404
                # pylint: enable-msg=E1101
 
405
                # error
 
406
                print self.blip
 
407
            except UndefinedName: # pylint: disable-msg=E0602
 
408
                # no error
 
409
                print self.blip
 
410
            # no error
 
411
            print self.blip
 
412
 
 
413
        def meth7(self):
 
414
            """test one line block opening disabling"""
 
415
            if self.blop: # pylint: disable-msg=E1101
 
416
                # error
 
417
                print self.blip
 
418
            else:
 
419
                # error
 
420
                print self.blip
 
421
            # error
 
422
            print self.blip
 
423
 
 
424
 
 
425
        def meth8(self):
 
426
            """test late disabling"""
 
427
            # error
 
428
            print self.blip
 
429
            # pylint: disable-msg=E1101
 
430
            # no error
 
431
            print self.bla
 
432
            print self.blop 
 
433
 
 
434
 
 
435
About analysis
 
436
==============
 
437
 
 
438
Pylint heuristics
 
439
-----------------
 
440
 
 
441
To be written...
 
442
 
 
443
About astng inference
 
444
---------------------
 
445
 
 
446
To be written...
 
447
 
 
448
Enhancing Pylint
 
449
================
 
450
 
 
451
Writing your own checker
 
452
------------------------
 
453
You can find some simple examples in the examples
 
454
directory of the distribution (custom.py and custom_raw.py). I'll try to
 
455
quickly explain the essential here.
 
456
 
 
457
First, there is two kinds of checker :
 
458
* raw checkers, which are analysing each module as a raw file stream
 
459
* ast checkers, which are working on an ast representation of the module
 
460
 
 
461
The ast representation used is an extension of the one provided with the
 
462
standard python distribution in the `compiler package`_. The extension
 
463
adds additional information and methods on the tree nodes to ease
 
464
navigation and code introspection.
 
465
 
 
466
An AST checker is a visitor, and should implements
 
467
visit_<lowered class name>
 
468
leave_<lowered class name>
 
469
methods for the nodes it's interested in. To get description of the different
 
470
classes used in an ast tree, look at the `compiler.ast documentation`. 
 
471
Checkers are ordered by priority. For each module, pylint's engine:
 
472
 
 
473
1. give the module source file as a stream to raw checkers
 
474
2. get an ast representation for the module
 
475
3. make a depth first descent of the tree, calling visit_<> on each AST
 
476
   checker when entering a node, and living_<> on the back traversal
 
477
 
 
478
Notice that the source code is probably the best source of
 
479
documentation, it should be clear and well documented. Don't hesitate to
 
480
ask for any information on the python-projects mailing list.
 
481
 
 
482
.. _`compiler package`: http://python.org/doc/current/lib/module-compiler.html
 
483
.. _`compiler.ast documentation`: http://www.python.org/doc/current/lib/module-compiler.ast.html
 
484
 
 
485
 
 
486
Contribute !
 
487
------------
 
488
feedback, bug reports, patch, svn...
 
489
 
 
490
 
 
491
Other information
 
492
=================
 
493
 
 
494
IDE integration
 
495
---------------
 
496
Pylint is integrated in the following editors/IDEs:
 
497
 
 
498
* emacs (of course)
 
499
* eric3
 
500
* eclipse (using the pydev_ plugin, see also
 
501
  http://msdl.cs.mcgill.ca/MSDL/people/denis/meetings/pythonDev) 
 
502
 
 
503
To use pylint from within vim, see
 
504
http://www.gonzo.kiev.ua/projects/pylint.vim 
 
505
 
 
506
.. _pydev: http://pydev.sourceforge.net
 
507
 
 
508
 
 
509
Some projects using Pylint
 
510
--------------------------
 
511
The following projects are known to use pylint to help develop better
 
512
code: 
 
513
 
 
514
* OSAF Chandler (http://www.osafoundation.org/)
 
515
* Xen (http://www.xensource.com/)
 
516
* CPS (http://www.nuxeo.org)
 
517
* ERP5 (http://www.erp5.org/)
 
518
* pyxmpp (http://pyxmpp.jabberstudio.org/)
 
519
* mercurial 
 
520
* eXe (http://exelearning.org/)
 
521
* PrimaGIS (http://www.primagis.org)
 
522
* python-cdd (http://projetos.ossystems.com.br/python-cdd/)
 
523
* CDSWare (http://cdsware.cern.ch/)
 
524
* ASE (http://dcwww.camp.dtu.dk/campos/ASE/intro.html)
 
525
* RunJob (http://projects.fnal.gov/runjob/)
 
526
* Slugathon (http://slugathon.python-hosting.com/)
 
527
* Topographica (http://topographica.org/Home/index.html) (at least they intend to do so)
 
528
* http://browsershots.org 
 
529
* many more... 
 
530
 
 
531
Also notice that the CheeseCake_ kwalitee reporting tool uses pylint to
 
532
analyze the source code.
 
533
 
 
534
.. _CheeseCake: http://cheesecake.sourceforge.net/
 
535
 
 
536
 
 
537
 
 
538
.. include:: FAQ.txt
 
539
 
 
540
 
 
541
 
 
542