How do I disable "missing docstring" warnings at a file-level in Pylint?
Question:
Pylint throws errors that some of the 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 have a docstring at the beginning of them. 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 for 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.)
Answers:
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 e.g. Get Rid of Source Code 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 I suggest 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).
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 application.
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
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.
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
As mentioned by followben in the comments, a better solution is to just disable the rules that we want to disable rather than using --errors-only
. This can be done with --disable=<msg ids>, -d <msg ids>
.
The list of message IDs can be found here. For the specific error mentioned in the question, the message ID is C0111
.
For using --disable=
param in your choise of IDE or Text Editor, you will need to figure out how to do it.
For VS Code, this can be done by adding this in settings.json
:
"python.linting.pylintArgs": ["--disable=C0111"]
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
Edit file "C:UsersYour UserAppDataRoamingCodeUsersettings.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"
],
}
Just put the following lines at the beginning of any file you want to disable these warnings for.
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
-
Ctrl + Shift + P
-
Then type and click on > preferences:configure language specific settings
-
and then type "python" after that. Paste the code
{
"python.linting.pylintArgs": [
"--load-plugins=pylint_django", "--errors-only"
],
}
Go to file "settings.json" and disable the Python pydocstyle
:
"python.linting.pydocstyleEnabled": false
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.
If you are a Visual Studio Code user who wants to ignore this, you can add python.linting.pylintArgs
to .vscode/settings.json
:
{
...
"python.linting.pylintArgs": [
"--disable=C0114",
"--disable=C0115",
"--disable=C0116",
],
...
}
I just wanted to add to what @Milovan Tomašević posted above. I decided to use python.linting.pylintArgs
in VSCode’s global settings, as it was far more convenient than using a .pylintrc
file.
Also, instead of using an ID for the switch (such as C0115
), I used the symbolic names instead.
Full reference to Pylint options and switches is here.
{
"python.linting.pylintArgs": [
"--disable=missing-class-docstring",
"--disable=missing-function-docstring"
]
}
Pylint throws errors that some of the 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 have a docstring at the beginning of them. 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 for 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.)
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 e.g. Get Rid of Source Code 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 I suggest 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).
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 application.
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
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.
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
As mentioned by followben in the comments, a better solution is to just disable the rules that we want to disable rather than using --errors-only
. This can be done with --disable=<msg ids>, -d <msg ids>
.
The list of message IDs can be found here. For the specific error mentioned in the question, the message ID is C0111
.
For using --disable=
param in your choise of IDE or Text Editor, you will need to figure out how to do it.
For VS Code, this can be done by adding this in settings.json
:
"python.linting.pylintArgs": ["--disable=C0111"]
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
Edit file "C:UsersYour UserAppDataRoamingCodeUsersettings.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"
],
}
Just put the following lines at the beginning of any file you want to disable these warnings for.
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
-
Ctrl + Shift + P
-
Then type and click on > preferences:configure language specific settings
-
and then type "python" after that. Paste the code
{ "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ], }
Go to file "settings.json" and disable the Python pydocstyle
:
"python.linting.pydocstyleEnabled": false
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.
If you are a Visual Studio Code user who wants to ignore this, you can add python.linting.pylintArgs
to .vscode/settings.json
:
{
...
"python.linting.pylintArgs": [
"--disable=C0114",
"--disable=C0115",
"--disable=C0116",
],
...
}
I just wanted to add to what @Milovan Tomašević posted above. I decided to use python.linting.pylintArgs
in VSCode’s global settings, as it was far more convenient than using a .pylintrc
file.
Also, instead of using an ID for the switch (such as C0115
), I used the symbolic names instead.
Full reference to Pylint options and switches is here.
{
"python.linting.pylintArgs": [
"--disable=missing-class-docstring",
"--disable=missing-function-docstring"
]
}