gpt4 book ai didi

python - Flake8 不会针对缺少文档字符串或不遵循 PEP8 命名约定的代码给出错误/警告

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:22 26 4
gpt4 key购买 nike

我正在尝试为我的 python 代码运行 Flake8,但是我注意到它没有在一个缺少文档字符串的简单类上给我任何 PyDocStyle 错误,或者关于我的类名 cars 的警告,这应该根据PEP8 style guide汽车

示例代码文件(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 中没有找到任何帮助。关于如何启用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

我在 Linux 上使用 python 3.7.6、flake8 3.7.9(mccabe:0.6.1、pycodestyle:2.5.0、pyflakes:2.1.1)CPython 3.7.6

最佳答案

所以我发现默认情况下 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 相比,为了实现所需的行为,有必要安装第三方插件/库,这些插件/库可能会在一两年内被放弃,或者在 flake8 时中断升级。如果您不小心在 CI 管道中提及特定版本/构建,这两种情况都会出现问题,而后者会很麻烦。

关于python - Flake8 不会针对缺少文档字符串或不遵循 PEP8 命名约定的代码给出错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60403545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com