问题:如何在Pylint的文件级别禁用“缺少文档字符串”警告?

Pylint引发错误,指出某些文件缺少文档字符串。我尝试为每个类,方法和函数添加docstring,但是Pylint似乎还在检查文件的开头是否应该有docstring。我可以以某种方式禁用它吗?我想在类,函数或方法中缺少文档字符串的通知,但对于具有文档字符串的文件,这不是强制性的。

(是否在专有源文件的开头经常出现法律术语?是否有任何示例?我不知道单独发布这样一个琐碎的问题是否可以。)

Pylint throws errors that some of files are missing docstrings. I try and add docstrings to each class, method and function but it seems that Pylint also checks that files should a docstring at the beginning of it. Can i disable this somehow? I would like to be notified of a docstring is missing inside a class, function or method but it shouldn’t be mandatory for a file to have a docstring.

(Is there a term of the legal jargon often found at the beginning of a proprietary source file? Any examples? I don’t know whether it is a okay to post such a trivial question separately.)


回答 0

Python模块最好有一个文档字符串,解释该模块的功能,提供的内容以及使用类的示例。这与您在提供版权和许可信息的文件开头经常看到的注释不同,IMO不应在文档字符串中添加注释(有些人甚至认为它们应该完全消失,请参见例如http:// hackerboss。 com / get-rid-of-templates /

在pylint 2.4及更高版本中,您可以missing-docstring通过使用以下三个子消息来区分各种类型:

  • C0114missing-module-docstring
  • C0115missing-class-docstring
  • C0116missing-function-docstring

因此以下.pylintrc文件应该工作:

[MASTER]
disable=
    C0114, # missing-module-docstring

对于以前的Pylint版本,它没有针对文档字符串可能出现的各个位置的单独代码,因此您只能禁用C0111。问题是,如果您在模块范围内禁用此功能,那么它将在模块中的所有位置都被禁用(即,由于缺少函数/类/方法文档字符串,您将不会得到任何C行。这可能不太好。

因此,我建议添加一个小的缺少的文档字符串,例如:

"""
high level support for doing this and that.
"""

很快,您将发现有用的东西,例如提供一些示例,说明如何使用模块的各种类/函数,这些类/函数不一定属于类/函数的各个文档字符串(例如这些互动或类似的快速入门指南)。

It is nice for a Python module to have a docstring, explaining what the module does, what it provides, examples of how to use the classes. This is different from the comments that you often see at the beginning of a file giving the copyright and license information, which IMO should not go in the docstring (some even argue that they should disappear altogether, see eg. http://hackerboss.com/get-rid-of-templates/)

With pylint 2.4 and above you can differentiate between the various missing-docstring by using the three following sub-messages:

  • C0114 (missing-module-docstring)
  • C0115 (missing-class-docstring)
  • C0116 (missing-function-docstring)

So the following .pylintrc file should work:

[MASTER]
disable=
    C0114, # missing-module-docstring

For previous versions of Pylint, it does not have a separate code for the various place where docstrings can occur, so all you can do is disable C0111. The problem is that if you disable this at module scope, then it will be disabled everywhere in the module (i.e you won’t get any C line for missing function / class / method docstring. Which arguably is not nice.

So what I suggest is adding that small missing docstring, saying something like:

"""
high level support for doing this and that.
"""

Soon enough, you’ll be finding useful things to put in there, such as providing examples of how to use the various classes / functions of the module which do not necessarily belong to the individual docstrings of the classes / functions (such as how these interact, or something like a quick start guide).


回答 1

已经很晚了,但我仍然觉得它很有用。所以分享。在这里找到这个。

您可以为pylint添加“ –errors-only”标志,以禁用警告。

为此,请转到设置。编辑以下行:

"python.linting.pylintArgs": []

"python.linting.pylintArgs": ["--errors-only"]

而且你很好!

It’s late, but still I found it useful. So sharing. Found this here.

You can add “–errors-only” flag for pylint to disable warnings.

To do this, go to settings. Edit the following line:

"python.linting.pylintArgs": []

As

"python.linting.pylintArgs": ["--errors-only"]

And you are good to go!


回答 2

我认为在不禁用此功能的情况下,修复相对容易。

def kos_root():
    """Return the pathname of the KOS root directory."""
    global _kos_root
    if _kos_root: return _kos_root

您需要做的就是在每个函数中添加三重双引号字符串。

I think the fix is relative easy without disabling this feature.

def kos_root():
    """Return the pathname of the KOS root directory."""
    global _kos_root
    if _kos_root: return _kos_root

All you need to do is add the triple double quotes string in every function.


回答 3

我来找答案是因为,正如@cerin所说,在Django项目中,向django在创建新应用时自动生成的每个文件中添加模块文档字符串是麻烦且多余的。

因此,作为pylint不允许您指定文档字符串类型不同的事实的解决方法,您可以执行以下操作:

pylint */*.py --msg-template='{path}: {C}:{line:3d},{column:2d}: {msg}' | grep docstring | grep -v module

您必须更新msg-template,以便在grep时仍会知道文件名。这将返回除模块以外的所有其他缺少文档字符串类型。

然后,您可以修复所有这些错误,然后运行:

pylint */*.py --disable=missing-docstring

I came looking for an answer because, as @cerin said, in Django projects it is cumbersome and redundant to add module docstrings to every one of the files that django automatically generates when creating a new app.

So, as a workaround for the fact that pylint doesn’t let you specify a difference in docstring types, you can do this:

pylint */*.py --msg-template='{path}: {C}:{line:3d},{column:2d}: {msg}' | grep docstring | grep -v module

You have to update the msg-template so that when you grep you will still know the file name. This returns all the other missing-docstring types excluding modules.

Then you can fix all of those errors, and afterwards just run:

pylint */*.py --disable=missing-docstring

回答 4

。Pylint当前不允许您区分文档字符串警告。

但是,您可以将flake8用于所有python代码检查以及doc-string扩展名,以忽略此警告。

使用pip安装doc字符串扩展名(内部使用pydocstyle)。

pip install flake8_docstrings

然后,您可以只使用--ignore D100开关。例如flake8 file.py --ignore D100

No. Pylint doesn’t currently let you discriminate between doc-string warnings.

However, you can use flake8 for all python code checking along with the doc-string extension to ignore this warning.

Install the doc-string extension with pip (Internally, It uses pydocstyle).

pip install flake8_docstrings

You can then just use the --ignore D100 switch. For example flake8 file.py --ignore D100


回答 5

只需将以下行放在要禁用这些警告的任何文件的开头。

# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

Just put the following lines at the beginning of any file you want to disable these warnings.

# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

回答 6

在pylint 2.4及更高版本中,您可以missing-docstring通过使用以下三个子消息来区分各种类型:

  • C0114missing-module-docstring
  • C0115missing-class-docstring
  • C0116missing-function-docstring

因此以下.pylintrc文件应该工作:

[MASTER]
disable=
    C0114, # missing-module-docstring

With pylint 2.4 and above you can differentiate between the various missing-docstring by using the three following sub-messages:

  • C0114 (missing-module-docstring)
  • C0115 (missing-class-docstring)
  • C0116 (missing-function-docstring)

So the following .pylintrc file should work:

[MASTER]
disable=
    C0114, # missing-module-docstring

回答 7

编辑“ C:\ Users \您的User \ AppData \ Roaming \ Code \ User \ settings.json”,并将这些python.linting.pylintArgs行添加到末尾,如下所示:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}

Edit “C:\Users\Your User\AppData\Roaming\Code\User\settings.json” and add these python.linting.pylintArgs lines at the end as shown below:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}

回答 8

(1)CTRL + SHIFT + P(2)然后键入并单击> preferences:配置特定于语言的设置(3),然后在代码后键入python

{
"python.linting.pylintArgs": [
    "--load-plugins=pylint_django","--errors-only"
],

}

(1) CTRL+SHIFT+P (2)Then type and click on >preferences:configure language specific settings (3)and then type python after that past the code

{
"python.linting.pylintArgs": [
    "--load-plugins=pylint_django","--errors-only"
],

}

回答 9

就我而言,使用pylint 2.6.0,即使显式禁用了missing-module-docstringmissing-class-docstring并且missing-function-docstring在我的.pylintrc文件中,丢失的docstring消息也不会消失。最后,以下配置对我有用:

[MESSAGES CONTROL]

disable=missing-docstring,empty-docstring

显然,除非同时禁用两个检查,否则pylint 2.6.0仍将验证文档字符串。

In my case, with pylint 2.6.0, the missing docstring messages wouldn’t disappear, even after explicitly disabling missing-module-docstring, missing-class-docstring and missing-function-docstring in my .pylintrc file. Finally, the following configuration worked for me:

[MESSAGES CONTROL]

disable=missing-docstring,empty-docstring

Apparently, pylint 2.6.0 still validates docstrings unless both checks are disabled.


回答 10

转到“ settings.json”并禁用python pydocstyle

"python.linting.pydocstyleEnabled": false

Go to “settings.json” and disable python pydocstyle

"python.linting.pydocstyleEnabled": false

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。