Flake8 未在缺少文档字符串或不遵循 PEP8 命名约定的代码上给出 errors/warnings
Flake8 not giving errors/warnings on missing docstring or code not following PEP8 naming convention
我正在尝试 运行 Flake8 用于我的 python 代码,但是我注意到它没有在一个简单的 class 上给我任何 PyDocStyle 错误,缺少文档字符串或警告关于我的 class 名字 cars
根据 PEP8 style guide
应该是 Cars
示例代码文件 (cars.py)
class cars:
def __init__(self, some_value):
self.some_value = some_value
当我 运行 flake8 cars.py
我得到以下输出:
cars.py:3:37: W292 no newline at end of file
我正在尝试将 Flake8 配置为 运行 一些常见的样式检查,但在 Flake8 documentation about how to enable PyDocStyle 错误代码中找不到任何帮助。
为了比较,我 运行 针对 Pylint 的同一个文件,这里是输出
************* Module code.cars
cars.py:3:0: C0304: Final newline missing (missing-final-newline)
cars.py:1:0: C0114: Missing module docstring (missing-module-docstring)
cars.py:1:0: C0103: Class name "cars" doesnt conform to PascalCase naming style (invalid-name)
cars.py:1:0: C0115: Missing class docstring (missing-class-docstring)
cars.py:1:0: R0903: Too few public methods (0/2) (too-few-public-methods)
------------------------------------
Your code has been rated at -6.67/10
我正在使用 python 3.7.6,flake8 3.7.9(mccabe:0.6.1,pycodestyle:2.5.0,pyflakes:2.1.1)CPython 3.7.6 on Linux
所以我发现默认情况下 Flake8 默认包装 pycodestyle: 2.5.0
,文档中说:
Among other things, these features are currently not in the scope of
the pycodestyle library:
- naming conventions: this kind of feature is supported through plugins.
Install flake8 and the pep8-naming extension to use this feature.
- docstring conventions: they are not in the scope of this library; see
the pydocstyle project.
- automatic fixing: see the section PEP8 Fixers
in the related tools page.
所以我安装了 pep8-naming
以及 flake8-docstrings
,在 运行 flake8 --version
之后我得到了下面的信息,显示它现在正在使用已安装的插件:
3.7.9 (flake8-docstrings: 1.5.0, pydocstyle: 5.0.2, mccabe: 0.6.1, naming: 0.8.2, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.7.6 on Darwin
我重新检查 flake8 cars.py
并得到以下输出:
cars.py:1:1: D100 Missing docstring in public module
cars.py:2:1: D101 Missing docstring in public class
cars.py:2:8: N801 class name 'cars' should use CapWords convention
cars.py:3:1: D107 Missing docstring in __init__
第一印象 - 在检查了 flake8
的 git 存储库和我必须安装的其他插件后,我对 Flake8 有点怀疑。原因是在撰写本文时,Pylint 似乎包含了我需要的行为,而且存在时间更长,这得益于稳定性和更多贡献者。与较新的 flake8
相比,要实现所需的行为,有必要安装第三方 plugins/libraries,这可能会在一两年内被放弃,或者在 flake8
升级时中断。如果您不小心在 CI 管道中提及特定的 version/builds,那么这两种情况都会有问题,后者会很麻烦。
我正在尝试 运行 Flake8 用于我的 python 代码,但是我注意到它没有在一个简单的 class 上给我任何 PyDocStyle 错误,缺少文档字符串或警告关于我的 class 名字 cars
根据 PEP8 style guide
Cars
示例代码文件 (cars.py)
class cars:
def __init__(self, some_value):
self.some_value = some_value
当我 运行 flake8 cars.py
我得到以下输出:
cars.py:3:37: W292 no newline at end of file
我正在尝试将 Flake8 配置为 运行 一些常见的样式检查,但在 Flake8 documentation about how to enable PyDocStyle 错误代码中找不到任何帮助。
为了比较,我 运行 针对 Pylint 的同一个文件,这里是输出
************* Module code.cars
cars.py:3:0: C0304: Final newline missing (missing-final-newline)
cars.py:1:0: C0114: Missing module docstring (missing-module-docstring)
cars.py:1:0: C0103: Class name "cars" doesnt conform to PascalCase naming style (invalid-name)
cars.py:1:0: C0115: Missing class docstring (missing-class-docstring)
cars.py:1:0: R0903: Too few public methods (0/2) (too-few-public-methods)
------------------------------------
Your code has been rated at -6.67/10
我正在使用 python 3.7.6,flake8 3.7.9(mccabe:0.6.1,pycodestyle:2.5.0,pyflakes:2.1.1)CPython 3.7.6 on Linux
所以我发现默认情况下 Flake8 默认包装 pycodestyle: 2.5.0
,文档中说:
Among other things, these features are currently not in the scope of the pycodestyle library:
- naming conventions: this kind of feature is supported through plugins. Install flake8 and the pep8-naming extension to use this feature.
- docstring conventions: they are not in the scope of this library; see the pydocstyle project.
- automatic fixing: see the section PEP8 Fixers in the related tools page.
所以我安装了 pep8-naming
以及 flake8-docstrings
,在 运行 flake8 --version
之后我得到了下面的信息,显示它现在正在使用已安装的插件:
3.7.9 (flake8-docstrings: 1.5.0, pydocstyle: 5.0.2, mccabe: 0.6.1, naming: 0.8.2, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.7.6 on Darwin
我重新检查 flake8 cars.py
并得到以下输出:
cars.py:1:1: D100 Missing docstring in public module
cars.py:2:1: D101 Missing docstring in public class
cars.py:2:8: N801 class name 'cars' should use CapWords convention
cars.py:3:1: D107 Missing docstring in __init__
第一印象 - 在检查了 flake8
的 git 存储库和我必须安装的其他插件后,我对 Flake8 有点怀疑。原因是在撰写本文时,Pylint 似乎包含了我需要的行为,而且存在时间更长,这得益于稳定性和更多贡献者。与较新的 flake8
相比,要实现所需的行为,有必要安装第三方 plugins/libraries,这可能会在一两年内被放弃,或者在 flake8
升级时中断。如果您不小心在 CI 管道中提及特定的 version/builds,那么这两种情况都会有问题,后者会很麻烦。