36

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:

enter image description here

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.

wim
  • 338,267
  • 99
  • 616
  • 750
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    OP literally already referenced that in his question. – takendarkk Nov 27 '14 at 02:53
  • @user3426575 this is exactly why I've mentioned the topic. – alecxe Nov 27 '14 at 02:54
  • 5
    *"Please, also, let me know if I'm getting insanely concerned about the code quality."* - Not in the slightest! :) If I have one pet peeve, it is documentation with spelling and/or grammatical errors. Programmers execute code; they read documentation. Thus, documentation that leaves you going "What?!" defeats its sole purpose for being there in the first place. –  Nov 27 '14 at 03:26
  • @iCodez phew, glad we believe and follow the same principles. Thank you. – alecxe Nov 27 '14 at 03:57

2 Answers2

39

Pylint includes a spell-checker since 1.4.0.

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)
Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Thanks for adding the instructions! I tried it on real code, and it's kind of difficult: it doesn't know how to ignore code examples, URLs, and other ReST markup, so variable names are marked as misspellings, etc. – Ned Batchelder Nov 27 '14 at 15:16
  • Yeah, there could a very-very long list of words to ignore (`spelling-ignore-words` setting) which can be eventually difficult to maintain. I'll give it a try on a real project anyway. Thanks. – alecxe Nov 27 '14 at 16:56
  • For non-mac users `pip install -U pyenchant` should do the job. – Steve Barnes Mar 28 '16 at 16:18
  • 6
    For me this checks everything (variables, classes etc.). I would like it to be done only for docstrings and comments in a py file. I suppose I'm doing something wrong but I'm not sure what. =) – Giampaolo Rodolà Nov 29 '16 at 02:34
  • I seem to be in the opposite situation of @GiampaoloRodolà, where spelling checker _only_ checks docstrings and comments and I am unable to find a checker to check spelling everywhere. – Swirle13 Jan 12 '23 at 19:17
5

On Windows, install enchant with pip install pyenchant

Then, to ensure spellchecking, in your .pylintrc file, in the section [SPELLING], add spelling-dict=en_US.

I also suggest you to add your commonly used acronyms in a .txt file and give its path in the option spelling-private-dict-file=.

For example:

[SPELLING]
# Spelling dictionary name. Available dictionaries: none. To make it working
# install python-enchant package.
spelling-dict=en_US
# A path to a file that contains private dictionary; one word per line.
spelling-private-dict-file=private-dictionary.txt

And run with : pylint --rcfile=.pylintrc yourfile

Sylvain
  • 679
  • 9
  • 13