Having a function like:
def foo(x: int) -> float:
return float(x)
I would like to use a NumPy-like docstring like the following:
def foo(x: int) -> float:
"""
Parameters
----------
x
Input parameter
Returns
-------
The output value.
"""
return float(x)
Note that:
Is there a Sphinx extension that supports that? Would you recommend another syntax?
What Are Type Annotations? Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and methods. In many languages, datatypes are explicitly stated. In these languages, if you don't declare your datatype — the code will not run.
A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation. Also, it is a common practice to generate online (html) documentation automatically from docstrings.
Docstrings can be further broken up into three major categories: Class Docstrings: Class and class methods. Package and Module Docstrings: Package, modules, and functions. Script Docstrings: Script and functions.
Comments in Python improve the readability of the program, it explains the code in a language that is easy to understand whereas docstrings describe what the code does, it does not explain how the code works.
Standard extension is autodoc. Napoleon extension supports Google- and NumPy-style docstrings.
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