Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: Tools/Scripts/webkitpy/thirdparty/pylint/doc/manual.txt

Issue 18418010: Check in the thirdparty libs needed for webkitpy. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 ==================
2 Pylint User Manual
3 ==================
4
5 :Author: Sylvain Thénault
6 :Author: Alexandre Fayolle
7 :Organization: Logilab
8
9 .. contents::
10
11
12 This document is meant to be the reference user manual for Pylint_. This is a
13 work in progress so some sections or parts may be missing (sometimes marked by a
14 XXX). If you think it's lacking some important information, please talk about
15 it on the python-projects mailing list (see the `Mailing lists`_ section for
16 more information about the list).
17
18 .. _Pylint: http://www.logilab.org/project/name/pylint
19
20
21 Introduction
22 ============
23
24 What is pylint?
25 ---------------
26
27 Pylint is a tool that checks for errors in python code, tries to enforce a
28 coding standard and looks for smelling code. This is similar but nevertheless
29 different from what pychecker_ provides, especially since pychecker explicitly
30 does not bother with coding style. The default coding style used by pylint is
31 close to `PEP 008`_ (aka `Guido's style guide`_). For more information about
32 code smells, refer to Martin Fowler's `refactoring book`_
33
34 One important thing to note is that Pylint isn't smarter than you are: it may
35 warn you about things that you have conscientiously done. That's for example
36 because it tries to detect things that may be dangerous in a context, but maybe
37 not in others, or because it checks for some things that you don't care
38 about. Generally, you shouldn't expect pylint to be totally quiet about your
39 code, so don't necessarily be alarmed if it gives you a hell lot of messages for
40 your proudly(XXX) project ;)
41
42 Pylint will display a number of messages as it analyzes the code, as well as
43 some statistics about the number of warnings and errors found in different
44 files. The messages are classified under various categories such as errors and
45 warnings (more below). If you run pylint twice, it will display the statistics
46 from the previous run together with the ones from the current run, so that you
47 can see if the code has improved or not.
48
49 Last but not least, the code is given an overall mark, based on the number an
50 severity of the warnings and errors. This has proven to be very motivating for
51 programmers.
52
53 .. _pychecker: http://pychecker.sf.net
54 .. _`PEP 008`: http://www.python.org/dev/peps/pep-0008/
55 .. _`Guido's style guide`: http://www.python.org/doc/essays/styleguide.html
56 .. _`refactoring book`: http://www.refactoring.com/
57
58 Installation
59 ------------
60
61 Dependencies
62 ''''''''''''
63 Pylint requires the latest `logilab-astng`_ and `logilab-common`_
64 packages. It should be compatible with any python version >= 2.5.
65
66 .. _`logilab-astng`: http://www.logilab.org/project/name/astng
67 .. _`logilab-common`: http://www.logilab.org/project/name/common
68
69
70 Distributions
71 '''''''''''''
72 The source tarball is available at ftp://download.logilab.org/pub/pylint.
73
74 You may apt-get a well-tested debian or ubuntu package by adding one of::
75
76 deb http://download.logilab.org/production unstable/
77 deb http://download.logilab.org/production sid/
78 deb http://download.logilab.org/production squeeze/
79 deb http://download.logilab.org/production lenny/
80
81 to your */etc/apt/sources.list* file. Pylint is also available in the
82 standard Debian distribution (but add our public debian repository
83 anyway if you want to get the latest releases and upgrades earlier)
84
85 Pylint is also available in Gentoo, Fedora 4, Ubuntu, FreeBSD, Darwin
86 (and maybe others, if you know about more OSes, please drop us a note!).
87
88
89 Source distribution installation
90 ''''''''''''''''''''''''''''''''
91 From the source distribution, extract the tarball, go to the extracted
92 directory and simply run ::
93
94 python setup.py install
95
96 You'll have to install dependencies in a similar way.
97
98 Windows users may get valuable information about pylint installation on
99 `this page`_.
100
101 .. _`this page`: http://thinkhole.org/wp/2006/01/16/installing-pylint-on-windows /
102
103
104 Note for Windows users
105 ''''''''''''''''''''''
106
107 On Windows, once you have installed pylint, the command line usage is ::
108
109 pylint.bat [options] module_or_package
110
111 But this will only work if *pylint.bat* is either in the current
112 directory, or on your system path. (*setup.py* install install *python.bat*
113 to the *Scripts* subdirectory of your Python installation -- e.g.
114 C:\Python24\Scripts.) You can do any of the following to solve this:
115
116 1. change to the appropriate directory before running pylint.bat
117
118 2. add the Scripts directory to your path statement in your autoexec.bat
119 file (this file is found in the root directory of your boot-drive)
120
121 3. create a 'redirect' batch file in a directory actually on your
122 systems path
123
124 To effect (2), simply append the appropriate directory name to the PATH=
125 statement in autoexec.bat. Be sure to use the Windows directory
126 separator of ';' between entries. Then, once you have rebooted (this is
127 necessary so that the new path statement will take effect when
128 autoexec.bat is run), you will be able to invoke PyLint with
129 pylint.bat on the command line.
130
131 (3) is the best solution. Once done, you can call pylint at the command
132 line without the .bat, just as do non-Windows users by typing: ::
133
134 pylint [options] module_or_package
135
136 To effect option (3), simply create a plain text file pylint.bat with
137 the single line: ::
138
139 C:\PythonDirectory\Scripts\pylint.bat
140
141 (where PythonDirectory is replaced by the actual Python installation
142 directory on your system -- e.g. C:\Python24\Scripts\pylint.bat).
143
144
145 Invoking pylint
146 ---------------
147
148 Pylint is meant to be called from the command line. The usage is ::
149
150 pylint [options] module_or_package
151
152 You should give pylint the name of a Python package or module. Pylint
153 will ``import`` this package or module, so you should pay attention to
154 your ``PYTHONPATH``, since it is a common error to analyze an
155 installed version of a module instead of the development version.
156
157 It is also possible to analyze python files, with a few
158 restriction. The thing to keep in mind is that pylint will try to
159 convert the file name to a module name, and only be able to process
160 the file if it succeeds. ::
161
162 pylint mymodule.py
163
164 should always work since the current working
165 directory is automatically added on top of the python path ::
166
167 pylint directory/mymodule.py
168
169 will work if "directory" is a python package (i.e. has an __init__.py
170 file) or if "directory" is in the python path.
171
172 For more details on this see the Frequently Asked Questions.
173
174 You can also start a thin gui around pylint (require TkInter) by
175 typing ::
176
177 pylint-gui
178
179 This should open a window where you can enter the name of the package
180 or module to check, at pylint messages will be displayed in the user
181 interface.
182
183 It is also possible to call Pylint from an other Python program,
184 thanks to ``py_run()`` function in ``lint`` module,
185 assuming Pylint options are stored in ``pylint_options`` string, as ::
186
187 from pylint import epylint as lint
188 lint.py_run( pylint_options)
189
190 To silently run Pylint on a ``module_name.py`` module,
191 and get its standart output and error::
192
193 from pylint import epylint as lint
194 (pylint_stdout, pylint_stderr) = lint.py_run( 'module_name.py', True)
195
196
197 Pylint output
198 -------------
199
200 The default format for the output is raw text. But passing pylint the
201 ``--output-format=html`` or ``-h y`` or ``-o html`` option will produce an HTML document.
202
203 There are several sections in pylint's output.
204
205 Source code analysis section
206 ''''''''''''''''''''''''''''
207 For each python module,
208 pylint will first display a few '*' characters followed by the name
209 of the module. Then, a number of messages with the following
210 format: ::
211
212 MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
213
214 You can get another output format, useful since it's recognized by
215 most editors or other development tools using the ``--parseable=y``
216 option.
217
218 The message type can be:
219
220 * [R]efactor for a "good practice" metric violation
221 * [C]onvention for coding standard violation
222 * [W]arning for stylistic problems, or minor programming issues
223 * [E]rror for important programming issues (i.e. most probably bug)
224 * [F]atal for errors which prevented further processing
225
226 Sometimes the line of code which caused the error is displayed with
227 a caret pointing to the error. This may be generalized in future
228 versions of pylint.
229
230 Example (extracted from a run of pylint on itself...):
231
232 ::
233
234 ************* Module pylint.checkers.format
235 W: 50: Too long line (86/80)
236 W:108: Operator not followed by a space
237 print >>sys.stderr, 'Unable to match %r', line
238 ^
239 W:141: Too long line (81/80)
240 W: 74:searchall: Unreachable code
241 W:171:FormatChecker.process_tokens: Redefining built-in (type)
242 W:150:FormatChecker.process_tokens: Too many local variables (20/15)
243 W:150:FormatChecker.process_tokens: Too many branches (13/12)
244
245
246 Reports section
247 '''''''''''''''
248 Following the analysis message, pylint will display a set of reports,
249 each one focusing on a particular aspect of the project, such as number
250 of messages by categories, modules dependencies...
251
252 For instance, the metrics report displays summaries gathered from the
253 current run.
254
255 * the number of processed modules
256 * for each module, the percentage of errors and warnings
257 * the total number of errors and warnings
258 * percentage of classes, functions and modules with docstrings, and
259 a comparison from the previous run
260 * percentage of classes, functions and modules with correct name
261 (according to the coding standard), and a comparison from the
262 previous run
263 * a list of external dependencies found in the code, and where they appear
264
265 Also, a global evaluation for the code is computed, and an
266 optional witty comment is displayed (if ``--comment=y`` was
267 specified on the command line).
268
269
270
271 Command line options
272 --------------------
273
274 First of all, we have two basic (but useful) options.
275
276 --version show program's version number and exit
277 -h, --help show help about the command line options
278
279 Pylint is architectured around several checkers. By default all
280 checkers are enabled. You can disable a specific checker or some of its
281 messages or messages categories by specifying
282 ``--disable=<id>``. A more general disable can be enabled with
283 or disable all checkers using
284 ``--enable=w<id>``. See the list of available features_ for a
285 description of provided checkers with their functionalities.
286 The ``--disable`` and ``--enable`` options can be used with comma separated list s
287 mixing checkers, message ids and categories like ``-d C,W,E0611,design``
288
289 Each checker has some specific options, which can take either a yes/no
290 value, an integer, a python regular expression, or a comma separated
291 list of values (which are generally used to override a regular
292 expression in special cases). For a full list of options, use ``--help``
293
294 Specifying all the options suitable for your setup and coding
295 standards can be tedious, so it is possible to use a rc file to
296 specify the default values. Pylint looks for /etc/pylintrc and
297 ~/.pylintrc. The ``--generate-rcfile`` option will generate a
298 commented configuration file according to the current configuration on
299 standard output and exit. You can put other options before this one to
300 use them in the configuration, or start with the default values and
301 hand tune the configuration.
302
303 Other useful global options include:
304
305 --zope Initialize Zope products before starting
306 --ignore=file Add <file> (may be a directory) to the black
307 list. It should be a base name, not a path.
308 You may set this option multiple times.
309 --statistics=y_or_n Compute statistics on collected data.
310 --persistent=y_or_n Pickle collected data for later comparisons.
311 --comment=y_or_n Add a comment according to your evaluation note.
312 --parseable=y_or_n Use a parseable output format.
313 --html=y_or_n Use HTML as output format instead of text.
314 --list-msgs Generate pylint's messages.
315 --full-documentation Generate pylint's full documentation, in reST format.
316
317 .. _features: features.html
318
319 Daily pylint usage
320 ------------------
321 What pylint says is not to be taken as gospel. While getting as
322 few false positives for errors as possible is a goal for us -- and
323 python makes it hard enough, it is not the case for warnings.
324
325 :Quoting Alexandre:
326 My usage pattern for pylint is to generally run pylint -e quite often to
327 get stupid errors flagged before launching an application (or before
328 committing). I generally run pylint with all the bells and whistles
329 activated some time before a release, when I want to cleanup the code.
330 And when I do that I simply ignore tons of the false warnings (and I
331 can do that without being driven mad by this dumb program which is not
332 smart enough to understand the dynamicity of Python because I only run
333 it once or twice a week in this mode)
334
335 :Quoting Marteen Ter Huurne:
336 In our project we just accepted that we have to make some modifications in our
337 code to please PyLint:
338
339 - stick to more naming conventions (unused variables ending in underscores,
340 mix-in class names ending in "Mixin")
341 - making all abstract methods explicit (rather than just not defining them in
342 the superclass)
343 - for messages which are useful in general, but not in a specific case: add "#
344 pylint: disable=X0123" comments
345 - for PyLint bugs: add "#pylint: disable=X0123" comments
346 - for PyLint limitations: add "#pylint: disable=X0123" comments
347 (for instance Twisted's modules create a lot of definitions dynamically so
348 PyLint does not know about them)
349
350 The effort is worth it, since PyLint helps us a lot in keeping the code clean
351 and finding errors early. Although most errors found by PyLint would also be
352 found by the regression tests, by fixing them before committing, we save time.
353 And our regression tests do not cover all code either, just the most complex
354 parts.
355
356
357 Bug reports, feedback
358 ---------------------
359 You think you have found a bug in Pylint? Well, this may be the case
360 since Pylint is under development. Please take the time to send a bug
361 report to python-projects@logilab.org if you've not found it already reported on
362 the `tracker page`_. This mailing list is also a nice place to
363 discuss Pylint issues, see below for more information about pylint's related
364 lists.
365
366 You can check for already reported bugs, planned features on pylint's tracker
367 web page: http://www.logilab.org/project/name/pylint
368
369 Notice that if you don't find something you have expected in pylint's
370 tracker page, it may be on the tracker page of one of its dependencies, namely
371 astng and common:
372
373 * http://www.logilab.org/project/name/logilab-astng
374 * http://www.logilab.org/project/name/logilab-common
375
376 .. _`tracker page`: http://www.logilab.org/project/name/pylint
377
378 Mailing lists
379 -------------
380 Use the python-projects@logilab.org mailing list for anything related
381 to Pylint. This is in most cases better than sending an email directly
382 to the author, since others will benefit from the exchange, and you'll
383 be more likely answered by someone subscribed to the list. This is a
384 moderated mailing list, so if you're not subscribed email you send will have to
385 be validated first before actually being sent on the list.
386
387 You can subscribe to this mailing list at
388 http://lists.logilab.org/mailman/listinfo/python-projects
389
390 Archives are available at
391 http://lists.logilab.org/pipermail/python-projects/
392
393 If you prefer speaking french instead of english, you can use the
394 generic forum-fr@logilab.org mailing list:
395
396 * (un)subscribe: http://lists.logilab.org/mailman/listinfo/forum-fr
397 * archives: http://lists.logilab.org/pipermail/forum-fr
398
399 Notice though that this list has a very low traffic since most pylint related
400 discussions are done on the python-projects mailing list.
401
402
403
404 Advanced usage
405 ==============
406
407 Base configuration
408 ------------------
409
410 To be written...
411
412 Environment
413 -----------
414
415 To be written...
416
417 Messages control
418 ----------------
419
420 An example available from the examples directory:
421
422 .. sourcecode:: python
423
424 """pylint option block-disable"""
425
426 __revision__ = None
427
428 class Foo(object):
429 """block-disable test"""
430
431 def __init__(self):
432 pass
433
434 def meth1(self, arg):
435 """this issues a message"""
436 print self
437
438 def meth2(self, arg):
439 """and this one not"""
440 # pylint: disable=W0613
441 print self\
442 + "foo"
443
444 def meth3(self):
445 """test one line disabling"""
446 # no error
447 print self.bla # pylint: disable=E1101
448 # error
449 print self.blop
450
451 def meth4(self):
452 """test re-enabling"""
453 # pylint: disable=E1101
454 # no error
455 print self.bla
456 print self.blop
457 # pylint: enable=E1101
458 # error
459 print self.blip
460
461 def meth5(self):
462 """test IF sub-block re-enabling"""
463 # pylint: disable=E1101
464 # no error
465 print self.bla
466 if self.blop:
467 # pylint: enable=E1101
468 # error
469 print self.blip
470 else:
471 # no error
472 print self.blip
473 # no error
474 print self.blip
475
476 def meth6(self):
477 """test TRY/EXCEPT sub-block re-enabling"""
478 # pylint: disable=E1101
479 # no error
480 print self.bla
481 try:
482 pylint: enable=E1101
483 # error
484 print self.blip
485 except UndefinedName: # pylint: disable=E0602
486 # no error
487 print self.blip
488 # no error
489 print self.blip
490
491 def meth7(self):
492 """test one line block opening disabling"""
493 if self.blop: # pylint: disable=E1101
494 # error
495 print self.blip
496 else:
497 # error
498 print self.blip
499 # error
500 print self.blip
501
502
503 def meth8(self):
504 """test late disabling"""
505 # error
506 print self.blip
507 # pylint: disable=E1101
508 # no error
509 print self.bla
510 print self.blop
511
512
513
514 About analysis
515 ==============
516
517 Pylint heuristics
518 -----------------
519
520 To be written...
521
522 About astng inference
523 ---------------------
524
525 To be written...
526
527
528
529 Enhancing Pylint
530 ================
531
532 Writing your own checker
533 ------------------------
534 You can find some simple examples in the examples
535 directory of the distribution (custom.py and custom_raw.py). I'll try to
536 quickly explain the essentials here.
537
538 First, there are two kinds of checkers :
539 * raw checkers, which are analysing each module as a raw file stream
540 * ast checkers, which are working on an ast representation of the module
541
542 The ast representation used is an extension of the one provided with the
543 standard python distribution in the `compiler package`_. The extension
544 adds additional information and methods on the tree nodes to ease
545 navigation and code introspection.
546
547 An AST checker is a visitor, and should implement
548 visit_<lowered class name>
549 leave_<lowered class name>
550 methods for the nodes it's interested in. To get description of the different
551 classes used in an ast tree, look at the `compiler.ast documentation`.
552 Checkers are ordered by priority. For each module, pylint's engine:
553
554 1. give the module source file as a stream to raw checkers
555 2. get an ast representation for the module
556 3. make a depth first descent of the tree, calling visit_<> on each AST
557 checker when entering a node, and living_<> on the back traversal
558
559 Notice that the source code is probably the best source of
560 documentation, it should be clear and well documented. Don't hesitate to
561 ask for any information on the python-projects mailing list.
562
563 .. _`compiler package`: http://docs.python.org/library/compiler
564 .. _`compiler.ast documentation`: http://docs.python.org/library/compiler#module -compiler.ast
565
566
567 Contribute !
568 ------------
569 All our software is developped using the mercurial_ version control
570 system. This is a very cool distributed vcs and its usage is very similar to
571 other ones such as cvs or subversion (though the distributed feature introduced
572 some different usage patterns). See mercurial home page for installation on
573 your computer and basic usage. Note that it's very easy to send us patches using
574 `hg email` command ;).
575
576 You can get the in-development pylint source code from our public mercurial_
577 repository:
578
579 http://www.logilab.org/src/pylint
580
581 The same is true for pylint dependencies (if you use pylint code from the
582 repository, you should usually use code from the repository as well for astng
583 and common):
584
585 http://www.logilab.org/src/logilab/astng
586 http://www.logilab.org/src/logilab/common
587
588 .. _mercurial: http://www.selenic.com/mercurial/
589
590 Contribution Instructions
591 --------------------------
592 Got a patch for pylint? There a few steps you must take to make sure your
593 patch gets accepted.
594
595 * Test your code
596 * Pylint keeps a set of unit tests in the /test directory. To get your
597 patch accepted you must write (or change) a test input file and message
598 file in the appropriate input and messages folders.
599 * In the test folder of pylint run ./fulltest.sh (python version), make sure
600 all tests pass before submitting a patch
601 * Create a diff file
602 * To create a diff from the command line invoke (from a directory under
603 version control) ::
604
605 hg diff > <yourname>.diff
606
607 * E-mail the mailing list with your diff file
608
609 Other information
610 =================
611
612 IDE integration
613 ---------------
614
615 To use pylint with emacs, see http://www.emacswiki.org/emacs/PythonProgrammingIn Emacs#toc8
616
617 To use pylint with vim, see
618 http://www.vim.org/scripts/script.php?script_id=891
619
620 To use pylint with eclipse, see http://pydev.org
621
622 To use pylint with komodo_, see
623 http://mateusz.loskot.net/2006/01/15/running-pylint-from-komodo/
624
625 To use pylint with gedit_, see
626 http://live.gnome.org/Gedit/PylintPlugin
627
628 To use pylint with WingIDE_, see
629 http://www.wingware.com/doc/edit/pylint
630
631 Pylint is integrated in Eric_ IDE, see the `Project > Check` menu.
632
633 Pylint is integrated in spyder_, see http://packages.python.org/spyder/pylint.ht ml
634
635 Pylint is integrated in pyscripter_, see the `Tool -> Tools` menu.
636
637 .. _Eric: http://eric-ide.python-projects.org/
638 .. _pyscripter: http://code.google.com/p/pyscripter/
639 .. _pydev: http://pydev.org
640 .. _komodo: http://www.activestate.com/Products/Komodo/
641 .. _gedit: http://www.gnome.org/projects/gedit/
642 .. _WingIDE: http://www.wingware.com/
643 .. _spyder: http://code.google.com/p/spyderlib/
644
645
646 Some projects using Pylint
647 --------------------------
648 The following projects are known to use pylint to help develop better
649 code:
650
651 * OSAF Chandler (http://www.osafoundation.org/)
652 * Xen (http://www.xensource.com/)
653 * CPS (http://www.nuxeo.org)
654 * ERP5 (http://www.erp5.org/)
655 * pyxmpp (http://pyxmpp.jabberstudio.org/)
656 * mercurial
657 * eXe (http://exelearning.org/)
658 * PrimaGIS (http://www.primagis.org)
659 * python-cdd (https://projetos.ossystems.com.br/projects/python-cdd)
660 * CDSWare (http://cdsware.cern.ch/)
661 * ASE (http://dcwww.camp.dtu.dk/campos/ASE/intro.html)
662 * RunJob (http://projects.fnal.gov/runjob/)
663 * Slugathon (http://slugathon.python-hosting.com/)
664 * Topographica (http://topographica.org/Home/index.html) (at least they intend t o do so)
665 * http://browsershots.org
666 * many more...
667
668 Also notice that the CheeseCake_ kwalitee reporting tool uses pylint to
669 analyze the source code.
670
671 .. _CheeseCake: http://cheesecake.sourceforge.net/
672
673
674
675
OLDNEW
« no previous file with comments | « Tools/Scripts/webkitpy/thirdparty/pylint/doc/flymake.txt ('k') | Tools/Scripts/webkitpy/thirdparty/pylint/doc/quickstart.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698