This document was drawn from Python Standards Checking on the Trac/Wiki, with some light edits (including some re-formatting for Confluence). The content on this page should be verified before the Trac/Wiki page is removed.
Upon receiving a Standards Compliance report, the developer is expected to resolve the issues. Most LSST Rules may be 'broken' if the developer feels there are good reasons for not following the Standards. This caveat might be tightened up if the developers aren't primarily holding to the Standards.
Coding Standards Compliance Tool
LogiLab PyLint was selected as the most comprehensive Coding Standards Checker for Python available. An important feature is its design strategy allowing user-coded add-ons to the Rule base.
- PyLint User Manual: http://www.logilab.org/card/pylint_manual
- PyLint eror messages sorted by;
Currently the only changes to the LogiLab PyLint Rule base is the disabling of some Rules to better reflect the LSST Python Coding Standards. (Note: the
pylintrc startup file containing the list of disabled errors is missing.)
Suppressing an Infraction
PyLint provides a variety of methods to either permanently or temporarily turn-off an infraction report covering a single line or a block of lines. The option to permanently disable all infractions against specific Rules are also provided in the .pylintrc initialization script and has been used to refine the LSST Infraction Report to those Rules of interest.
Provided below is the PyLint example/tester on suppressing an infraction report within the user's code. If you need to inhibit an infraction on a single line, review meth3 shown below.
Suppressing Multiple Infractions
Multiple infractions on a single line are suppressed using the syntax:
Common or Curious Error Reports
"Statement seems to have no effect" Error
Python does not require a ';' at the end of most statements but it doesn't object should you gratuitously add one. On the other hand, Pylint reports such an occurrence is an error:
produced the following errors - as a result of the extra semi-colon:
Relative Import Error
The python source:
Note the leading period. The warning is about the new-to-python 2.5 feature of relative imports as described in PEP 328: <http://www.python.org/dev/peps/pep-0328/> and the forthcoming change in how the ambiguity of absolute imports will change. The old import scheme that most of us have used for years has a serious flaw. Suppose you have the following file hierarchy:
bar.py starts with:
This is fine unless you install a new package named "
foo" and you want to access it from
bar.py. Then it's a headache because the local
foo shields the main-level package
foo FOR NOW. In a few generations of Python the main-level foo will shadow the local
foo instead!!! Thus the warning.
You should use the new relative import syntax:
You can use more dots as needed:
You can even get the new absolute import syntax (preferring global to local module names) but you have to use "
from future import absolute_import" This may only be necessary if you really do want to access a global module whose name is shadowed by a local module. I suspect any code that does not will start raising deprecation warnings when we switch to Python 2.6 and in the long run such code could break.