New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve docstrings in classes & functions for API #225
Comments
Answers to your questions
Your proposed solutions seems good to me 馃憣 |
@tomschr are there any practical, go-to docstring guidelines (maybe even with an accompanying tutorial specifically for documenting python functions, as I haven't used sphinx before) we could reuse? I'm somewhat intimidated by all the features, markups and outputs. 馃 |
@N-Coder Well, normally if you need any information regarding docstrings, the first go-to place would be the PEPs PEP 257 (Docstring Conventions) and PEP 287 (reStructuredText Docstring Format). However, both are a bit too technical and academic for a go-to docstring guideline. A more helpful approach is RealPython's article Documenting Python Code. In this article, they give recommendations for classes, module, and package docstrings and show some formats to use inside it. I think it's very helpful. They even referenced Divio's blog, a link that you recently shared. 馃槃 Regarding Sphinx, yes, it can be intimidating. However, you really need very few markup only. If you use the RST format (also used in Python and this library), I normally use:
That's it! Of course, there are more, but in most cases it's enough for a function. You don't mention datatypes, as they are automatically added when using type annotations. Very convenient! This is an example from the python-semver library, slightly redacted: def parse(version: str) -> dict:
"""
Parse version to major, minor, patch, pre-release, build parts.
:param version: a version string
:return: dictionary with the keys 'build', 'major', 'minor', 'patch', and 'prerelease'.
The prerelease or build keys can be None if not provided
:raises: ValueError, if the version string contains an invalid semver version
Example:
>>> ver = semver.parse('3.4.5-pre.2+build.4')
>>> ver['major']
3
>>> ver['minor']
4
"""
# ... I think, the most important tip that I can give here is: be consistent. It doesn't matter much to use fancy markup, but with the macros above, you can document a lot. If needed, you can always add more. |
Situation
Currently, the situation with docstrings in classes and functions seems to me ambiguous and inconsistent. 馃槈 Some are pretty well documented, others are not.
Additionally, some signatures contain conflicting information between docstrings and type annotations, for example:
Compare the arguments
imports
,events
, andtodos
in the docstring and with the type annotation. Both are advertised differently. This is one of the worst situations when documenting stuff: conflicting information. IMHO this should be improved to avoid confusing our developers.Questions
sphinx-autodoc-typehints
plugin?Organizer
in fileorganizer.py
,Timeline
intimeline.py
and others.Proposed Solution
Create a consistent docstring "spec" to improve consistency for every function. For example, a docstring should contain (at least):
We should check if we could rephrase docstring which contains the pipe symbol (
|
); it should be avoided due to consistency reasons (interactive Python shell).Document the missing pieces to improve API documentation.
Avoid ambiguous types between docstring and type annotation. Ideally, type annotations are included into the API documentation without explicitly mention it in the docstring.
The text was updated successfully, but these errors were encountered: