Automated docstring and comments spell check

2019-03-17 22:01发布

问题:

Consider the following sample code:

# -*- coding: utf-8 -*-
"""Test module."""


def test():
    """Tets function"""
    return 10

pylint gives it 10 of 10, flake8 doesn't find any warnings:

$ pylint test.py 
...
Global evaluation
-----------------
Your code has been rated at 10.00/10
...
$ flake8 test.py
$

But, as you may see, there is a typo in the test function's docstring. And, your editor would probably highlight it automagically, for example, here's how Pycharm does it:

Thanks to the https://stackoverflow.com/questions/2151300/whats-the-best-way-to-spell-check-python-source-code topic, now I know that there is a relevant spell-checking library called PyEnchant that can be used to detect typos.

My end goal is to automatically detect typos in the project and make the spell check a part of a continuous build, test and code-quality check run.

Is there a way to achieve that with pylint? If not, I would also appreciate any hints on applying PyEnchant to docstrings and comments project-wise (in this case, pylint or pyflakes plugin could be made out of it).

Please, also, let me know if I'm getting insanely concerned about the code quality.

回答1:

Pylint just released 1.4.0, which includes a spell-checker. Here is the initial pull-request.

Note that, to make the checker work, you need to install pyenchant python module and have an enchant library installed system-wide. On mac, it can be installed via brew:

$ brew install enchant

By default, the spelling pylint checker is turned off. You can enable it either in the pylint rc configuration file, or from the command-line:

$ cat test.py
# I am the tyop

$ pylint --disable all --enable spelling --spelling-dict en_US test.py
C:  1, 0: Wrong spelling of a word 'tyop' in a comment:
# I am the tyop
           ^^^^
Did you mean: 'typo' or 'top' or 'tip' or 'topi'? (wrong-spelling-in-comment)