自动文档字符串和注释拼写检查

ale*_*cxe 23 python code-analysis pylint static-code-analysis

请考虑以下示例代码:

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


def test():
    """Tets function"""
    return 10
Run Code Online (Sandbox Code Playgroud)

pylint给它10个10,flake8没有发现任何警告:

$ pylint test.py 
...
Global evaluation
-----------------
Your code has been rated at 10.00/10
...
$ flake8 test.py
$
Run Code Online (Sandbox Code Playgroud)

但是,正如您所看到的,test函数的文档字符串中存在拼写错误.并且,您的编辑器可能会自动突出显示它,例如,这是Pycharm如何做到的:

在此输入图像描述

多亏了/sf/ask/150591031/话题,现在我知道有一个叫相关的拼写检查库PyEnchant那可用于检测拼写错误.

我的最终目标是自动检测项目中的拼写错误,并使拼写检查成为持续构建,测试和代码质量检查运行的一部分.

有没有办法实现这一目标pylint?如果没有,我也会感谢任何关于在PyEnchant项目方面应用于文档字符串和注释的提示(在这种情况下,pylint或pyflakes插件可以由它制作).

如果我疯狂地关注代码质量,也请告诉我.

Ned*_*der 24

Pylint刚刚发布了1.4.0,其中包括一个spell-checker.这是最初的拉动请求.

请注意,要使检查器工作,您需要安装pyenchantpython模块并在enchant系统范围内安装库.在Mac上,它可以通过brew以下方式安装:

$ brew install enchant
Run Code Online (Sandbox Code Playgroud)

默认情况下,spellingpylint检查程序已关闭.您可以在pylint rc配置文件中或从命令行启用它:

$ 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)
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这会检查所有内容(变量、类等)。我希望它只对 py 文件中的文档字符串和注释进行。我想我做错了什么,但我不确定是什么。=) (3认同)

Syl*_*ain 5

在 Windows 上,安装 enchantpip install pyenchant

然后,为了确保拼写检查,请.pylintrc在文件的 部分中[SPELLING]添加spelling-dict=en_US.

我还建议您在文件中添加常用的首字母缩略词.txt,并在选项中给出其路径spelling-private-dict-file=。

例如:

[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
Run Code Online (Sandbox Code Playgroud)

并运行:pylint --rcfile=.pylintrc yourfile