docments
provides programmatic access to comments in function parameters and return types. It can be used to create more developer-friendly documentation, CLI, etc tools.
Without docments, if you want to document your parameters, you have to repeat param names in docstrings, since they're already in the function signature. The parameters have to be kept synchronized in the two places as you change your code. Readers of your code have to look back and forth between two places to understand what's happening. So it's more work for you, and for your users.
Furthermore, to have parameter documentation formatted nicely without docments, you have to use special magic docstring formatting, often with odd quirks, which is a pain to create and maintain, and awkward to read in code. For instance, using numpy-style documentation:
def add_np(a:int, b:int=0)->int:
"""The sum of two numbers.
Used to demonstrate numpy-style docstrings.
Parameters
----------
a : int
the 1st number to add
b : int
the 2nd number to add (default: 0)
Returns
-------
int
the result of adding `a` to `b`"""
return a+b
By comparison, here's the same thing using docments:
def add(
a:int, # the 1st number to add
b=0, # the 2nd number to add
)->int: # the result of adding `a` to `b`
"The sum of two numbers."
return a+b
docments
also supports numpy-style docstrings, or a mix or numpy-style and docments parameter documentation. The functions in this section help get and parse this information.
test_eq(docstring(add), "The sum of two numbers.")
parse_docstring(add_np)
The returned dict
has parameter names as keys, docments as values. The return value comment appears in the return
, unless returns=False
. Using the add
definition above, we get:
docments(add)
If you pass full=False
, the values are dict
of defaults, types, and docments as values. Note that the type annotation is inferred from the default value, if the annotation is empty and a default is supplied.
docments(add, full=True)
docments(add, full=True)
To evaluate stringified annotations (from python 3.10), use eval_str
:
docments(add, full=True, eval_str=True)['a']
If you need more space to document a parameter, place one or more lines of comments above the parameter, or above the return type. You can mix-and-match these docment styles:
def add(
# The first operand
a:int,
# This is the second of the operands to the *addition* operator.
# Note that passing a negative value here is the equivalent of the *subtraction* operator.
b:int,
)->int: # The result is calculated using Python's builtin `+` operator.
"Add `a` to `b`"
return a+b
docments(add)
You can also use docments with classes and methods:
class Adder:
"An addition calculator"
def __init__(self,
a:int, # First operand
b:int, # 2nd operand
): self.a,self.b = a,b
def calculate(self
)->int: # Integral result of addition operator
"Add `a` to `b`"
return a+b
docments(Adder)
docments(Adder.calculate)
docments can also be extracted from numpy-style docstrings:
print(add_np.__doc__)
docments(add_np)
You can even mix and match docments and numpy parameters:
def add_mixed(a:int, # the first number to add
b
)->int: # the result
"""The sum of two numbers.
Parameters
----------
b : int
the 2nd number to add (default: 0)"""
return a+b
docments(add_mixed, full=True)
from nbdev.export import notebook2script
notebook2script()