bgdev.docstring

Docstring template using Google convention.

created

16/11/2018

author

Benoit Gielly <benoit.gielly@gmail.com>

This module is just a template to show how to write proper docstrings using the Google’s syntax. It will show you all the different applications and keywords you can use so sphinx can generate a nice looking documentation!

You can find a link to the Google Docstring guide here:

http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html

Happy docstring-ing! :D

class ExampleClass(param1, param2, param3)[source]

Bases: object

The summary line for a class docstring should fit on one line.

If the class has public attributes, they may be documented here in an Attributes section and follow the same formatting as a function’s Args section. Alternatively, attributes may be documented inline with the attribute’s declaration (see __init__ method below).

Properties created with the @property decorator should be documented in the property’s getter method.

attr1

Description of attr1.

Type

str

attr2

Description of attr2.

Type

int, optional

Parameters
  • param1 (str) – Description of param1.

  • param2 (int) – Description of param2. Multiple lines are supported.

  • param3 (list(str)) – Description of param3.

Note

Do not include the self parameter in the Args section.

__init__(param1, param2, param3)[source]

Class constructor.

You should avoid documenting classes in the __init__ method of the class. It should go within the class docstring instead.

attr3

Doc comment inline with attribute

attr4

Doc comment before attribute, with type specified

Type

list(str)

attr5

Docstring after attribute, with type specified.

Type

str

example_method(param1, param2)[source]

Class methods are similar to regular functions.

See example_function() for a better description.

Note

Do not include the self parameter in the Args section.

property getter_only_property

Properties should be documented in their getter method.

Type

str

property getter_setter_property

Get a property.

If the setter method contains notable behavior, it should be mentioned here.

Parameters
  • value (str) – Properties with both a getter and setter

  • only be documented in their getter method. (should) –

Returns

The returned value/object.

Return type

list

example_decorated_function(arg)[source]

Test the docstring to ensure the @wraps decorator worked.

Parameters

arg (str) – Description of arg1

Returns

The first argument arg1

Return type

type

example_decorator(func)[source]

Decorate a method.

Decorators need a special treatment to generate the documentation properly. You have to decorate the sub-function with the wraps method of the functools module. If you don’t, the docstring of the decorated function will be skipped.

Example

from functools import wraps

def example_decorator(func):
    "Decorator docstring"

    @wraps(func)
    def function(*args, **kwargs):
        returned_func = func(*args, **kwargs)
        return returned_func
    return function
example_function(param1, param2=None, *args, **kwargs)[source]

Show an example of a module level function.

Function parameters should be documented in the Args section. The name of each parameter is required. The type and description of each parameter is optional, but should be included if not obvious.

If *args or **kwargs are accepted, they should be listed as *args and **kwargs.

The format for a parameter is:

name (type): description
    The description may span multiple lines. Following
    lines should be indented. The "(type)" is optional.

    Multiple paragraphs are supported in parameter
    descriptions.
Parameters
  • param1 (int) – The first parameter.

  • param2 (str) – The second parameter. Defaults to None. Second line of description should be indented.

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns

True if successful, False otherwise.

The return type is optional and may be specified at the beginning of the Returns section followed by a colon.

The Returns section may span multiple lines and paragraphs. Following lines should be indented to match the first line.

The Returns section supports any reStructuredText formatting, including literal blocks:

{
    'param1': param1,
    'param2': param2
}

Return type

bool

Raises
  • AttributeError – The Raises section is a list of all exceptions that are relevant to the interface.

  • ValueError – If param2 is equal to param1.

Example

Examples should be written in doctest format, and should illustrate how to use the function.

>>> print([i for i in range(4)])
[0, 1, 2, 3]

Examples

You can also use literal blocks:

print([i for i in range(4)])
>>> [0, 1, 2, 3]

Todo

  • The Todo section lists in an orange block every task that needs to be done.

  • Make sure to use an * (asterisk) to display bullet points

example_function_with_types(arg1, arg2)[source]

Show an example function with types documented in the docstring.

PEP 484 type annotations are supported. If attribute, parameter, and return types are annotated according to PEP 484, they do not need to be included in the docstring:

Parameters
  • arg1 (int) – The first parameter.

  • arg2 (str) – The second parameter.

Returns

The return value. True for success, False otherwise.

Return type

bool

example_generator(num)[source]

Create generators.

They have a Yields section instead of a Returns section.

Parameters

num (int) – The upper limit of the range to generate, from 0 to num - 1.

Yields

int – The next number in the range of 0 to num - 1.