Place Ghantoos

Continuous integration – Python comments spellchecks with pylint, pyenchant and tox

Tagged: , , , , ,

As part of our continuous integration efforts, we always think about code quality and sometimes forget about the comments. To improve readability, the least we could do is spellcheck the comments to make sure we are not introducing new words into this world. Here is how I added spellchecks as part of my Python tox test suite of lshell, using pylint and pyenchant.

To test this locally, I had to install pyenchant and pylint. On Debian:

sudo apt-get install python-enchant pylint

As some of my comments included words that were not in the default en_US dictionary (mostly variable names, app names, etc.), I first ran pylint asking it to store all unknown words in a separate file. This can be done by specifying the argument: –spelling-store-unknown-words=y.

pylint --disable=all --enable=spelling --spelling-dict=en_US \
       --ignore-comments=no --spelling-store-unknown-words=y \
       --spelling-private-dict-file=test/words \
       path/to/code path/to/other/code

Argument explanation:

  • –disable all : disable all checks (code/syntax checks)
  • –enable spelling: speaks for itself
  • –spelling-dict en_US: use the US English dictionary
  • –ignore-comments no: enable checks on comments (only)
  • –spelling-private-dict-file=test/words: file containing a list of words/exceptions to add to your private dictionary
  • –spelling-store-unknown-words=y: save all unknown words to the file specified in –spelling-private-dict-file=

Once this is done, I reviewed the “test/words”, kept the acronyms and removed the actual spelling mistakes. Note that this file should only contain lower-case words, even if they appear in upper-case in your comments, otherwise, pyenchant seems to ignore them.

You can find an example of such a file here.

I then ran the following command, until all spelling mistakes were corrected.

pylint --disable=all --enable=spelling --spelling-dict=en_US \
       --ignore-comments=no --spelling-private-dict-file=test/words \
       path/to/code path/to/other/code

In order to integrate this to your tox tests to run this automatically with the rest of your test suite, add the following to your tox.ini file:

envlist = py27,py34,pep8,pycomments
minversion = 1.6
skipsdist = True


commands = pylint --disable=all --enable=spelling --spelling-dict=en_US --ignore-comments=no --spelling-private-dict-file=test/words path/to/code path/to/other/code

If you are integrated with Travis-CI, you will need to install some packages to ensure that the en_US dictionary and required enchant dependencies are installed.

language: python
  - pip install tox
  - sudo apt-get install python-enchant
  - tox
  - TOXENV=pycomments

There must be some other way to achieve this, but I couldn’t find anything that parsed only comments (not the actual code). This procedure did help me correct many spelling mistakes. :)

You’ll find the actual changes done on the lshell project.

Tagged: , , , , ,

Leave a Reply