A 'Python code analyzer' error checking pass should be a must before each commit

Registered by Borja López Soilán (NeoPolus)

A 'Python code analyzer' error checking pass should be *a must* before each commit.

There are several of such code analyzers for Python:
 * PyLint - http://pydev.sourceforge.net/pylint.html
 * PyChecker - http://pychecker.sourceforge.net/
 * PyFlakes - https://launchpad.net/pyflakes

They let "a dynamic language such as Python where not even a compiler checks the code" (https://blueprints.launchpad.net/openobject-server/+spec/openerp-requires-a-test-server) be a dynamic language with intensive code checking.

For example with PyLint (wich we use) you can get reports like this one on any module (audittrail module in the example):

Command (report errors only):
''
$ cd openerp-server-5.0.0-3/bin
$ pylint -e --include-ids=y --max-line-length=250 --class-rgx="[a-zA-Z_][a-zA-Z_0-9]+$" --argument-rgx="[a-z_][a-z0-9_]{1,30}$" --method-rgx="[a-z_][a-z0-9_]{1,30}$" --variable-rgx="[a-z_][a-z0-9_]{1,30}$" --dummy-variables-rgx="self$|db$|cr$|uid$|ids$|arg$|args$|context$|field_name$" --max-args=10 --max-public-methods=30 --min-similarity-lines=6 --ignore="__terp__.py" --disable-msg=R0201,W0142,W0232,W0403,W0702,W0102 addons/audittrail
''

Output:
''
************* Module addons.audittrail.audittrail
E1101: 92:audittrail_rule.unsubscribe: Module 'ir' has no 'ir_del' member
''

Command (report errors, warnings, conventions...):
''
$ cd openerp-server-5.0.0-3/bin
$ pylint --include-ids=y --max-line-length=250 --class-rgx="[a-zA-Z_][a-zA-Z_0-9]+$" --argument-rgx="[a-z_][a-z0-9_]{1,30}$" --method-rgx="[a-z_][a-z0-9_]{1,30}$" --variable-rgx="[a-z_][a-z0-9_]{1,30}$" --dummy-variables-rgx="self$|db$|cr$|uid$|ids$|arg$|args$|context$|field_name$" --max-args=10 --max-public-methods=30 --min-similarity-lines=6 --ignore="__terp__.py" --disable-msg=R0201,W0142,W0232,W0403,W0702,W0102 addons/audittrail
''

Output (just a bit of it as an example):
''
************* Module addons.audittrail
C0111: 1: Missing docstring
W0406: 22: Module import itself
************* Module addons.audittrail.audittrail
W0311:133: Bad indentation. Found 12 spaces, expected 4
W0311:134: Bad indentation. Found 12 spaces, expected 4
...
W0311:145: Bad indentation. Found 24 spaces, expected 16
W0311:146: Bad indentation. Found 28 spaces, expected 20
...
W0631: 90:audittrail_rule.unsubscribe: Using possibly undefined loop variable 'thisrule'
E1101: 92:audittrail_rule.unsubscribe: Module 'ir' has no 'ir_del' member
W0612: 92:audittrail_rule.unsubscribe: Unused variable 'res'
...
W0621:134:get_value_text: Redefining name 'obj' from outer scope (line 345)
W0622:132:get_value_text: Redefining built-in 'object'
W0622:153:get_value_text: Redefining built-in 'id'
...
W0212:164:get_value_text: Access to a protected member _rec_name of a client class
W0212:167:get_value_text: Access to a protected member _rec_name of a client class
R0912:132:get_value_text: Too many branches (13/12)
W0612:137:get_value_text: Unused variable 'model_object'
W0621:173:create_log_line: Redefining name 'obj' from outer scope (line 345)
W0621:180:create_log_line: Redefining name 'fields' from outer scope (line 25)
W0622:171:create_log_line: Redefining built-in 'object'
W0622:171:create_log_line: Redefining built-in 'id'
...
W0612:315:tmp_fct.execute: Unused variable 'model_object'
C0103:345: Invalid name "obj" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
W0611: 28: Unused import string
W0611: 29: Unused import copy
...

Report
======
254 statements analysed.

Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type |number |% |previous |difference |
+==========+=======+======+=========+===========+
|code |281 |62.17 |281 |= |
+----------+-------+------+---------+-----------+
|docstring |64 |14.16 |64 |= |
+----------+-------+------+---------+-----------+
|comment |85 |18.81 |85 |= |
+----------+-------+------+---------+-----------+
|empty |22 |4.87 |22 |= |
+----------+-------+------+---------+-----------+

Duplication
-----------

+-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines |0 |0 |= |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000 |= |
+-------------------------+------+---------+-----------+

External dependencies
---------------------
::

    ir (addons.audittrail.audittrail)
    wizard (addons.audittrail.wizard.wizard_view_log)
    osv
      \-fields (addons.audittrail.audittrail)
      \-osv (addons.audittrail.audittrail)
    pooler (addons.audittrail.audittrail,addons.audittrail.wizard.wizard_view_log)
    addons
      \-wizard (addons.audittrail)
      \-audittrail
        \-wizard_view_log (addons.audittrail.wizard)
    netsvc (addons.audittrail.audittrail)

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module |4 |4 |= |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|class |4 |4 |= |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|method |3 |3 |= |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|function |6 |6 |= |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+

Messages by category
--------------------

+-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention |110 |110 |= |
+-----------+-------+---------+-----------+
|refactor |6 |6 |= |
+-----------+-------+---------+-----------+
|warning |185 |185 |= |
+-----------+-------+---------+-----------+
|error |1 |1 |= |
+-----------+-------+---------+-----------+

% errors / warnings by module
-----------------------------

+-----------------------------------------+-------+--------+---------+-----------+
|module |error |warning |refactor |convention |
+=========================================+=======+========+=========+===========+
|addons.audittrail.audittrail |100.00 |97.30 |100.00 |91.82 |
+-----------------------------------------+-------+--------+---------+-----------+
|addons.audittrail.wizard.wizard_view_log |0.00 |2.16 |0.00 |6.36 |
+-----------------------------------------+-------+--------+---------+-----------+
|addons.audittrail |0.00 |0.54 |0.00 |0.91 |
+-----------------------------------------+-------+--------+---------+-----------+

Messages
--------

+-----------+-----------+
|message id |occurences |
+===========+===========+
|W0311 |140 |
+-----------+-----------+
|C0322 |73 |
+-----------+-----------+
|C0111 |17 |
+-----------+-----------+
|C0324 |14 |
+-----------+-----------+
...
+-----------+-----------+
|C0301 |1 |
+-----------+-----------+

Global evaluation
-----------------
Your code has been rated at -2.05/10 (previous run: -2.05/10)
''

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
New
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Whiteboard

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.