The error code D401 for pydocstyle reads: First line should be in imperative mood
.
I often run into cases where I write a docstring, have this error thrown by my linter, and rewrite it -- but the two docstrings are semantically identical. Why is it important to have imperative mood for docstrings?
Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. It's specified in source code that is used, like a comment, to document a specific segment of code.
The docstring for a function or method should summarize its behavior and document its arguments and return values. It should also list all the exceptions that can be raised and other optional arguments.
A quick recap on comments vs docstrings: Use comments to explain how code works. Comments are great for leaving notes for people working on your program. Docstrings provide documentation about functions, classes, and modules. Use docstrings to teach other developers how to use your program.
A docstring is a documentation string at the beginning of the class, function or function body that briefly explains how it works. Every function you create ought to have a docstring. They're in triple-quoted strings and allow for multi-line text.
From the docstring of check_imperative_mood
itself:
"""D401: First line should be in imperative mood: 'Do', not 'Does'. [Docstring] prescribes the function or method's effect as a command: ("Do this", "Return that"), not as a description; e.g. don't write "Returns the pathname ...".
(We'll ignore the irony that this docstring itself would fail the test.)
Why is it important? Because that's the explicit convention for Python docstrings, as detailed in PEP 257. There's nothing particularly special about it - it doesn't seem obvious to me that one of "Multiplies two integers and returns the product" and "Multiply two integers and return the product" is clearly better than the other. But it is explicitly specified in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With