Note

Changes to this document must be approved by the System Architect (RFC-24). To request changes to these standards, please file an RFC.

Documenting Python APIs with Docstrings

We use Python docstrings to create reference documentation for our Python APIs. Docstrings are read by developers, interactive Python users, and readers of our online documentation. This page describes how to write these docstrings in Numpydoc, DM’s standard format.

Format reference:

How to format different APIs:

Learn by example:

Treat the guidelines on this page as an extension of the DM Python Style Guide.

Basic Format of Docstrings

Python docstrings form the __doc__ attributes attached to modules, classes, methods and functions. See PEP 257 for background.

Docstrings MUST be delimited by triple double quotes

Docstrings must be delimited by triple double quotes: """. This allows docstrings to span multiple lines. You may use u""" for unicode, but it’s usually preferable to stick to ASCII.

For consistency, do not use triple single quotes: '''.

Docstrings SHOULD begin with """ and terminate with """ on its own line

The docstring’s summary sentence occurs on the same line as the opening """.

The terminating """ should be on its own line, even for ‘one-line’ docstrings (this is a minor departure from PEP 257). For example, a one-line docstring:

"""Sum numbers in an array.
"""

(Note: one-line docstrings are rarely used for public APIs, see Numpydoc Sections in Docstrings.)

An example of a multi-paragraph docstring:

"""Sum numbers in an array.

Parameters
----------
values : iterable
   Python iterable whose values are summed.

Returns
-------
sum : `float`
   Sum of ``values``.
"""

Docstrings of methods and functions SHOULD NOT be preceded or followed by a blank line

Inside a function or method, there should be no blank lines surrounding the docstring:

def sum(values):
    """Sum numbers in an array.

    Parameters
    ----------
    values : iterable
       Python iterable whose values are summed.

    Returns
    -------
    sum : `float`
       Sum of ``values``.
    """
    pass

Docstrings of classes SHOULD be followed, but not preceded, by a blank line

Like method and function docstrings, the docstring should immediately follow the class definition, without a blank space. However, there should be a single blank line before following code such as class variables or the __init__ method:

class Point(object):
    """Point in a 2D cartesian space.

    Parameters
    ----------
    x, y : `float`
       Coordinate of the point.
    """

    def __init__(x, y):
        self.x = x
        self.y = y

Docstring content MUST be indented with the code’s scope

For example:

def sum(values):
    """Sum numbers in an array.

    Parameters
    ----------
    values : iterable
       Python iterable whose values are summed.
    """
    pass

Not:

def sum(values):
    """Sum numbers in an array.

Parameters
----------
values : iterable
   Python iterable whose values are summed.
"""
    pass

ReStructuredText in Docstrings

We use reStructuredText to mark up and give semantic meaning to text in docstrings. ReStructuredText is lightweight enough to read in raw form, such as command line terminal printouts, but is also parsed and rendered with our Sphinx-based documentation build system. All of the style guidance for using reStructuredText from our ReStructuredText Style Guide applies in docstrings with a few exceptions defined here.

No space between headers and paragraphs

For docstrings, the Numpydoc standard is to omit any space between a header and the following paragraph.

For example

"""A summary

Notes
-----
The content of the notes section directly follows the header, with no blank line.
"""

This deviation from the normal style guide is in keeping with Python community idioms and to save vertical space in terminal help printouts.

Sections are restricted to the Numpydoc section set

Sections must be from the set of standard Numpydoc sections (see Numpydoc Sections in Docstrings). You cannot introduce new section headers, or use the full reStructuredText subsection hierarchy, since these subsections won’t be parsed by the documentation toolchain.

Always use the dash (-) to underline sections. For example:

def myFunction(a):
    """Do something.

    Parameters
    ----------
    [...]

    Returns
    -------
    [...]

    Notes
    -----
    [...]
    """

Simulate subsections with bold text

Conventional reStructuredText subsections are not allowed in docstrings, given the previous guideline. However, you may structure long sections with bold text that simulates subsection headers. This technique is useful for the Notes and Examples Numpydoc sections. For example:

def myFunction(a):
    """Do something.

    [...]

    Examples
    --------
    **Example 1**

    [...]

    **Example 2**

    [...]
    """

Line Lengths

Hard-wrap text in docstrings to match the docstring line length allowed by the coding standard.

Marking Up Parameter Names

The default reStructuredText role in docstrings is :py:obj:. Sphinx automatically generates links when the API names are marked up in single backticks. For example: `str` or `lsst.pipe.base.Struct`.

You cannot use this role to mark up parameters, however. Instead, use the code literal role (double backticks) to mark parameters and return variables in monospace type. For example, the description for format references the should_plot parameter:

Parameters
----------
should_plot : `bool`
    Plot the fit if `True`.
plot_format : `str`, optional
    Format of the plot when ``should_plot`` is `True`.

Numpydoc Sections in Docstrings

We organize Python docstrings into sections that appear in a common order. This format follows the Numpydoc standard (used by NumPy, SciPy, and Astropy, among other scientific Python packages) rather than the format described in PEP 287. These are the sections and their relative order:

  1. Short Summary
  2. Deprecation Warning (if applicable)
  3. Extended Summary (optional)
  4. Parameters (if applicable; for classes, methods, and functions)
  5. Returns or Yields (if applicable; for functions, methods, and generators)
  6. Other Parameters (if applicable; for classes, methods, and functions)
  7. Raises (if applicable)
  8. See Also (optional)
  9. Notes (optional)
  10. References (optional)
  11. Examples (optional)

For summaries of how these docstring sections are composed in specific contexts, see:

Short Summary

A one-sentence summary that does not use variable names or the function’s name:

def add(a, b):
    """Sum two numbers.
    """
    return a + b

For functions and methods, write in the imperative voice. That is, the summary is treated a command that the API consumer can give. Some examples:

  • Get metadata for all tasks.
  • Make an `lsst.pex.config.ConfigurableField` for this task.
  • Create a `Measurement` instance from a parsed YAML or JSON document.

Deprecation Warning

A section (where applicable) to warn users that the object is deprecated. Section contents should include:

  1. In what stack version the object was deprecated, and when it will be removed.
  2. Reason for deprecation if this is useful information (for example, the object is superseded, or duplicates functionality found elsewhere).
  3. New recommended way of obtaining the same functionality.

This section should use the note Sphinx directive instead of an underlined section header.

.. note:: Deprecated in 11_0
          `ndobj_old` will be removed in 12_0, it is replaced by
          `ndobj_new` because the latter works also with array subclasses.

Extended Summary

A few sentences giving an extended description. This section should be used to clarify functionality, not to discuss implementation detail or background theory, which should rather be explored in the ‘Notes’ section below. You may refer to the parameters and the function name, but parameter descriptions still belong in the ‘Parameters’ section.

Parameters

For functions, methods and classes.

‘Parameters’ is a description of a function or method’s arguments and their respective types. Parameters should be listed in the same order as they appear in the function or method signature.

For example:

def calcDistance(x, y, x0=0., y0=0.):
    """Calculate the distance between two points.

    Parameters
    ----------
    x : `float`
        X-axis coordinate.
    y : `float`
        Y-axis coordinate.
    x0 : `float`, optional
        X-axis coordinate for the second point (the origin, by default).
    y0 : `float`, optional
        Y-axis coordinate for the second point (the origin, by default).
    """

Each parameter is declared with a line formatted as {name} : {type} that is justified to the docstring. A single space is required before and after the colon (:). The name corresponds to the variable name in the function or method’s arguments. The type is described below (Describing Parameter Types). The description is indented by four spaces relative to the docstring and appears without a preceding blank line.

Normally parameters are documented consecutively, without blank lines between (see the earlier example). However, if the descriptions of an individual parameter span multiple paragraphs, or include lists, then you must separate each parameter with a blank line. For example:

Parameters
----------
output_path : `str`
    Filepath where the plot will be saved.

plot_settings : `dict`, optional
    Settings for the plot that may include these fields:

    - ``'dpi'``: resolution of the plot in dots per inch (`int`).
    - ``'rasterize'``: if `True`, then rasterize the plot. `False` by default.

Describing Parameter Types

Be as precise as possible when describing parameter types. The type description is free-form text, making it possible to list several supported types or indicate nuances. Complex and lengthy type descriptions can be partially moved to the parameter’s description field. The following sections will help you deal with the different kinds of types commonly seen.

Concrete types

Wrap concrete types in backticks (in docstrings, single backticks are equivalent to :py:obj:) to make a link to either an internal API or an external API that is supported by intersphinx. This works for both built-in types and most importable objects:

Parameters
----------
filename : `str`
    [...]
n : `int`
    [...]
verbose : `bool`
    [...]
items : `list` or `tuple`
    [...]
magnitudes : `numpy.ndarray`
    [...]
struct : `lsst.pipe.base.Struct`
    [...]

In general, provide the full namespace to the object, such as `lsst.pipe.base.Struct`. It may be possible to reference objects in the same namespace as the current module without any namespace prefix. Always check the compiled documentation site to ensure the link worked.

Choices

When a parameter can only assume one of a fixed set of values, those choices can be listed in braces:

order : {'C', 'F', 'A'}
    [...]
Sequence types

When a type is a sequence container (like a list or tuple), you can describe the type of the contents. For example:

mags : `list` of `float`
    Sequence of magnitudes.
Dictionary types

For dictionaries it is usually best to document the keys and their values in the parameter’s description:

settings : `dict`
    Settings dictionary with fields:

    - ``color``: Hex colour code (`str`).
    - ``size``: Point area in pixels (`float`).
Array types

For Numpy arrays, try to include the dimensionality:

coords : `numpy.ndarray`, (N, 2)
    [...]
flags : `numpy.ndarray`, (N,)
    [...]
image : `numpy.ndarray`, (Ny, Nx)
    [...]

Choose conventional variables or labels to describe dimensions, like N for the number of sources or Nx, Ny for rectangular dimensions.

Callable types

For callback functions, describe the type as callable:

likelihood : callable
    Likelihood function that takes two positional arguments:

    - ``x``: current parameter (`float`).
    - ``extra_args``: additional arguments (`dict`).

Optional Parameters

For keyword arguments with useful defaults, add optional to the type specification:

x : `int`, optional

Optional keyword parameters have default values, which are automatically documented as part of the function or method’s signature. You can also explain defaults in the description:

x : `int`, optional
    Description of parameter ``x`` (the default is -1, which implies summation
    over all axes).

Shorthand

When two or more consecutive input parameters have exactly the same type, shape and description, they can be combined:

x1, x2 : array-like
    Input arrays, description of `x1`, `x2`.

Returns

For functions and methods.

‘Returns’ is an explanation of the returned values and their types, in the same format as ‘Parameters’.

Basic example

If a sequence of values is returned, each value may be separately listed, in order:

def getCoord(self):
    """Get the point's pixel coordinate.

    Returns
    -------
    x : `int`
        X-axis pixel coordinate.
    y : `int`
        Y-axis pixel coordinate.
    """
    return self._x, self._y

Dictionary return types

If a return type is dict, ensure that the key-value pairs are documented in the description:

def getCoord(self):
    """Get the point's pixel coordinate.

    Returns
    -------
    pixelCoord : `dict`
       Pixel coordinates with fields:

       - ``x``: x-axis coordinate (`int`).
       - ``y``: y-axis coordinate (`int`).
     """
     return {'x': self._x, 'y': self._y}

Struct return types

lsst.pipe.base.Structs, returned by Tasks for example, are documented the same way as dictionaries:

def getCoord(self):
    """Get the point's pixel coordinate.

    Returns
    -------
    result : `lsst.pipe.base.Struct`
       Result struct with components:

       - ``x``: x-axis coordinate (`int`).
       - ``y``: y-axis coordinate (`int`).
     """
     return lsst.pipe.base.Struct(x=self._x, y=self._y)

Naming return variables

Note that the names of the returned variables do not necessarily correspond to the names of variables. In the previous examples, the variables x, y, and pixelCoord never existed in the method scope. Simply choose a variable-like name that is clear. Order is important.

If a returned variable is named in the method or function scope, you will usually want to use that name for clarity. For example:

def getDistance(self, x, y):
    """Compute the distance of the point to an (x, y) coordinate.

    [...]

    Returns
    -------
    distance : `float`
        Distance, in units of pixels.
    """
    distance = np.hypot(self._x - x, self._y - y)
    return distance

Yields

For generators.

‘Yields’ is used identically to ‘Returns’, but for generators. For example:

def items(self):
    """Iterate over items in the container.

    Yields
    ------
    key : `str`
        Item key.
    value : obj
        Item value.
    """
    for key, value in self._data.items():
        yield key, value

Other Parameters

For classes, methods and functions.

‘Other Parameters’ is an optional section used to describe infrequently used parameters. It should only be used if a function has a large number of keyword parameters, to prevent cluttering the Parameters section. In practice, this section is seldom used.

Raises

For classes, methods and functions.

‘Raises’ is an optional section for describing the exceptions that can be raised. You usually cannot document all possible exceptions that might get raised by the entire call stack. Instead, focus on:

  • Exceptions that are commonly raised.
  • Exceptions that are unique (custom exceptions, in particular).
  • Exceptions that are important to using an API.

The ‘Raises’ section looks like this:

Raises
------
IOError
    Raised if the input file cannot be read.
TypeError
    Raised if parameter ``example`` is an invalid type.

Don’t wrap each exception’s name with backticks, as we do when describing types in Parameters and Returns). No namespace prefix is needed when referring to exceptions in the same module as the API. Providing the full namespace is often a good idea, though.

The description text is indented by four spaces from the docstring’s left justification. Like the description fields for Parameters and Returns, the description can consist of multiple paragraphs and lists.

Stylistically, write the first sentence of each description in the form:

Raised if [insert circumstance].

See Also

Use the ‘See also’ section to link to related APIs that the user may not be aware of, or may not easily discover from other parts of the docstring. Here are some good uses of the ‘See also’ section:

  • If a function wraps another function, you may want to reference the lower-level function.
  • If a function is typically used with another API, you can reference that API.
  • If there is a family of closely related APIs, you might link to others in the family so a user can compare and choose between them easily.

As an example, for a function such as numpy.cos, we would have:

See also
--------
sin
tan

Numpydoc assumes that the contents of the ‘See also’ section are API names, so don’t wrap each name with backticks, as we do when describing types in Parameters and Returns). No namespace prefix is needed when referring to functions in the same module. Providing the full namespace is always safe, though, and provides clarity to fellow developers:

See also
--------
numpy.sin
numpy.tan

Notes

Notes is an optional section that provides additional information about the code, possibly including a discussion of the algorithm. Most reStructuredText formatting is allowed in the Notes section, including:

When using images, remember that many developers and users will be reading the docstring in its raw source form. Images should add information, but the docstring should still be useful and complete without them.

See also ReStructuredText in Docstrings for restrictions.

References

References cited in the ‘Notes’ section are listed here. For example, if you cited an article using the syntax [1]_, include its reference as follows:

References
----------
.. [1] O. McNoleg, "The integration of GIS, remote sensing,
   expert systems and adaptive co-kriging for environmental habitat
   modelling of the Highland Haggis using object-oriented, fuzzy-logic
   and neural-network techniques," Computers & Geosciences, vol. 22,
   pp. 585-588, 1996.

Web pages should be referenced with regular inline links.

References are meant to augment the docstring, but should not be required to understand it. References are numbered, starting from one, in the order in which they are cited.

Note

In the future we may support bibtex-based references instead instead of explicitly writing bibliographies in docstrings.

Examples

‘Examples’ is an optional section for usage examples written in the doctest format. These examples do not replace unit tests, but are intended to be tested to ensure documentation and code are consistent. While optional, this section is useful for users and developers alike.

When multiple examples are provided, they should be separated by blank lines. Comments explaining the examples should have blank lines both above and below them:

Examples
--------
A simple example:

>>> np.add(1, 2)
3

Comment explaining the second example:

>>> np.add([1, 2], [3, 4])
array([4, 6])

For tests with a result that is random or platform-dependent, mark the output as such:

Examples
--------
An example marked as creating a random result:

>>> np.random.rand(2)
array([ 0.35773152,  0.38568979])  #random

It is not necessary to use the doctest markup <BLANKLINE> to indicate empty lines in the output.

For more information on doctest, see:

Documenting Modules

Sections in Module Docstrings

Module docstrings contain the following sections:

  1. Short Summary
  2. Deprecation Warning (if applicable)
  3. Extended Summary (optional)
  4. See Also (optional)

Note

Module docstrings aren’t featured heavily in the documentation we generate and publish with Sphinx. Avoid putting important end-user documentation in module docstrings. Instead, write introductory and overview documentation in the module’s user guide (the doc/ directories of Stack packages).

Module docstrings can still be useful for developer-oriented notes, though.

Placement of Module Docstrings

Module-level docstrings must be placed as close to the top of the Python file as possible: below any #!/usr/bin/env python and license statements, but above imports. See also: Standard code order SHOULD be followed.

Module docstrings should not be indented. For example:

#
# This file is part of dm_dev_guide.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""Summary of MyModule.

Extended discussion of my module.
"""

import lsst.afw.table as afwTable
# [...]

Documenting Classes

Class docstrings are placed directly after the class definition, and serve to document both the class as a whole and the arguments passed to the __init__ constructor.

Sections in Class Docstrings

Class docstrings contain the following sections:

  1. Short Summary
  2. Deprecation Warning (if applicable)
  3. Extended Summary (optional)
  4. Parameters (if applicable)
  5. Other Parameters (if applicable)
  6. Raises (if applicable)
  7. See Also (optional)
  8. Notes (optional)
  9. References (optional)
  10. Examples (optional)

Placement of Class Docstrings

Class docstrings must be placed directly below the declaration, and indented according to the code scope:

class MyClass(object):
    """Summary of MyClass.

    Parameters
    ----------
    a : `str`
       Documentation for the ``a`` parameter.
    """

    def __init__(self, a):
        pass

The __init__ method never has a docstring since the class docstring documents the constructor.

Examples of Class Docstrings

Here’s an example of a more comprehensive class docstring with Short Summary, Parameters, Raises, See Also, and Examples sections:

class SkyCoordinate(object):
    """Equatorial coordinate on the sky as Right Ascension and Declination.

    Parameters
    ----------
    ra : `float`
       Right ascension (degrees).
    dec : `float`
       Declination (degrees).
    frame : {'icrs', 'fk5'}, optional
       Coordinate frame.

    Raises
    ------
    ValueError
        Raised when input angles are outside range.

    See also
    --------
    lsst.example.GalacticCoordinate

    Examples
    --------
    To define the coordinate of the M31 galaxy:

    >>> m31_coord = SkyCoordinate(10.683333333, 41.269166667)
    SkyCoordinate(10.683333333, 41.269166667, frame='icrs')
    """

    def __init__(self, ra, dec, frame='icrs'):
        pass

Documenting Methods and Functions

Sections in Method and Function Docstring Sections

Method and function docstrings contain the following sections:

  1. Short Summary
  2. Deprecation Warning (if applicable)
  3. Extended Summary (optional)
  4. Parameters (if applicable)
  5. Returns or Yields (if applicable)
  6. Other Parameters (if applicable)
  7. Raises (if applicable)
  8. See Also (optional)
  9. Notes (optional)
  10. References (optional)
  11. Examples (optional)

Placement of Module and Function Docstrings

Class, method, and function docstrings must be placed directly below the declaration, and indented according to the code scope:

class MyClass(object):
    """Summary of MyClass.

    Extended discussion of MyClass.
    """

    def __init__(self):
        pass

    def myMethod(self):
        """Summary of method.

        Extended Discussion of myMethod.
        """
        pass


def my_function():
    """Summary of my_function.

    Extended discussion of my_function.
    """
    pass

Again, the class docstring takes the place of a docstring for the __init__ method. __init__ methods don’t have docstrings.

Dunder Methods

Special “dunder” methods on classes only need to have docstrings if they are doing anything non-standard. For example, if a __getslice__ method cannot take negative indices, that should be noted. But if __ge__ returns true if self is greater than or equal to the argument, that need not be documented.

Examples of Method and Function Docstrings

Here’s an example function:

def check_unit(self, quantity):
    """Check that a `~astropy.units.Quantity` has equivalent units to
    this metric.

    Parameters
    ----------
    quantity : `astropy.units.Quantity`
        Quantity to be tested.

    Returns
    -------
    is_equivalent : `bool`
        `True` if the units are equivalent, meaning that the quantity
        can be presented in the units of this metric. `False` if not.

    See also
    --------
    astropy.units.is_equivalent

    Examples
    --------
    Check that a quantity in arcseconds is compatible with a metric defined in arcminutes:

    >>> import astropy.units as u
    >>> from lsst.verify import Metric
    >>> metric = Metric('example.test', 'Example', u.arcminute)
    >>> metric.check_units(1.*u.arcsecond)
    True

    But mags are not a compatible unit:

    >>> metric.check_units(21.*u.mag)
    False
    """
    if not quantity.unit.is_equivalent(self.unit):
        return False
    else:
        return True

Documenting Constants and Class Attributes

Sections in Constant and Class Attribute Docstrings

Constants in modules and attributes in classes are all documented similarly. At a minimum, they should have a summary line that includes the type. They can also have a more complete structure with these sections:

  1. Short Summary
  2. Deprecation Warning (if applicable)
  3. Extended Summary (optional)
  4. Notes (optional)
  5. References (optional)
  6. Examples (optional)

Placement of Constant and Class Attribute Docstrings

Docstrings for module-level variables and class attributes appear directly below their first declaration. For example:

MAX_ITER = 10
"""Maximum number of iterations (`int`).
"""


class MyClass(object):
    """Example class for documenting attributes.
    """

    x = None
    """Description of x attribute.
    """

Examples of Constant and Class Attribute Docstrings

Minimal constant or attribute example

Include the attribute or constant’s type in parentheses at the end of the summary line:

NAME = 'LSST'
"""Name of the project (`str`)."""

Multi-section docstrings

Multi-section docstrings keep the type information in the summary line. For example:

PA1_DESIGN = 5. * u.mmag
"""PA1 design specification (`astropy.units.Quantity`).

Notes
-----
The PA1 metric [1]_ is defined so that the rms of the unresolved source
magnitude distribution around the mean value (repeatability) will not
exceed PA1 millimag (median distribution for a large number of sources).

References
----------
.. [1] Z. Ivezic and the LSST Science Collaboration. 2011, LSST Science
   Requirements Document, LPM-17, URL https://ls.st/LPM-17
"""

Attributes set in __init__ methods

In many classes, public attributes are set in the __init__ method. The best way to document these public attributes is by declaring the attribute at the class level and including a docstring with that declaration:

class Metric(object):
    """Verification metric.

    Parameters
    ----------
    name : `str`
        Name of the metric.
    unit : `astropy.units.Unit`
        Units of the metric.
    package : `str`, optional
        Name of the package where this metric is defined.
    """

    name = None
    """Name of the metric (`str`).
    """

    unit = None
    """Units of the metric (`astropy.units.Unit`).
    """

    def __init__(self, name, unit, package=None):
        self.name = name
        self.unit = unit
        self._package = package

Notice that the parameters to the __init__ method are documented separately from the class attributes (highlighted).

Documenting Class Properties

Properties are documented like class attributes rather than methods. After all, properties are designed to appear to the user like simple attributes.

For example:

class Measurement(object):

    @property
    def quantity(self):
        """The measurement quantity (`astropy.units.Quantity`).
        """
        # ...

    @quantity.setter
    def quantity(self, q):
        # ...

    @property
    def unit(self):
        """Units of the measurement (`astropy.units.Unit`, read-only).
        """
        # ...

Note:

  • Do not use the Returns section in the property’s docstring. Instead, include type information in the summary, as is done for class attributes.
  • Only document the property’s “getter” method, not the “setter” (if present).
  • If a property does not have a “setter” method, include the words read-only after the type information.

Complete Example Module

#
# This file is part of dm_dev_guide.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""Example Python module with Numpydoc-formatted docstrings.

This module demonstrates documentation written according to LSST DM's
guidelines for `Documenting Python APIs with Docstrings`_. Docstrings have
well-specified sections. This paragraph is considered an `Extended
Summary`_. Permitted sections are listed in `Numpydoc Sections in Docstrings`_.
You can't add arbitrary sections since they won't be parsed.

Notes
-----
Usually we don't write extensive module docstrings. Focus the module docstring
on information that a Stack developer needs to know when working inside that
module. Module *users* typically won't see module docstrings (instead they will
read module documentation topics written in the package's ``doc/`` directory).

.. _`Documenting Python APIs with Docstrings`:
   https://developer.lsst.io/docs/py_docs.html
.. _`Extended Summary`:
   https://developer.lsst.io/docs/py_docs.html#py-docstring-extended-summary
.. _`Numpydoc Sections in Docstrings`:
   https://developer.lsst.io/docs/py_docs.html#py-docstring-sections
"""

__all__ = ('MODULE_LEVEL_VARIABLE', 'moduleLevelFunction', 'exampleGenerator',
           'ExampleClass', 'ExampleError')

MODULE_LEVEL_VARIABLE = 12345
"""Module level variable documented inline (`int`).

The module variable's type is specified in the short summary, as shown above.
Module variables (constants) can have extended descriptions, like this
paragraph. For a complete list of sections permitted in constant docstrings see
`Documenting Constants and Class Attributes`_.

.. _`Documenting Constants and Class Attributes`:
   https://developer.lsst.io/docs/py_docs.html#py-docstring-attribute-constants-structure
"""


def moduleLevelFunction(param1, param2=None, *args, **kwargs):
    """Test that two parameters are not equal.

    This is an example of a function docstring. Function parameters are
    documented in the ``Parameters`` section. See *Notes* for the format
    specification. See `Documenting Methods and Functions`_ for more
    information about function docstrings.

    Parameters
    ----------
    param1 : `int`
        The first parameter. Note how the type is marked up with backticks.
        This marks ``int`` as an API object so that Sphinx will attempt to
        link to its reference documentation. You can do this for custom types
        as well. You'll see an example in the `Returns`_ documentation.
    param2 : `str`, optional
        Optional arguments (those with defaults) always include the word
        ``optional`` after the type info. See the `Parameters`_ section
        documentation for details.
    *args
        Variable length argument list.
    **kwargs
        Arbitrary keyword arguments. If you do accept ``**kwargs``, make sure
        you link to documentation that describes what keywords are accepted,
        or list the keyword arguments here:

        - ``key1``: description (`int`).
        - ``key2``: description (`str`).

    Returns
    -------
    success : `bool`
        `True` if successful, `False` otherwise.

        The return type is not optional. The ``Returns`` section may span
        multiple lines and paragraphs. Following lines should be indented to
        match the first line of the description.

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

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

        See the `Returns`_ section documentation for details.

    Raises
    ------
    AttributeError
        Raised if <insert situation>.
    ValueError
        Raised if <insert situation>. See the `Raises`_ section documentation
        for details.

    Notes
    -----
    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 to match the first line of the description.

            Multiple paragraphs are supported in parameter descriptions.

    See also
    --------
    exampleGenerator
    ExampleClass

    Examples
    --------
    If possible, include an API usage example using the doctest format:

    >>> moduleLevelFunction('Hello', param2='World')
    True

    See the `Examples`_ section reference for details.

    .. _`Documenting Methods and Functions`:
       py-docstring-method-function-structure
    .. _`Parameters`:
       https://developer.lsst.io/docs/py_docs.html#py-docstring-parameters
    .. _`Returns`:
       https://developer.lsst.io/docs/py_docs.html#py-docstring-returns
    .. _`Raises`:
       https://developer.lsst.io/docs/py_docs.html#py-docstring-raises
    .. _`Examples`:
       https://developer.lsst.io/docs/py_docs.html#py-docstring-examples
    """
    if param1 == param2:
        raise ValueError('param1 may not be equal to param2')
    return True


def exampleGenerator(n):
    """Generate an increasing sequence of numbers from 0 to a given limit.

    Generators have a ``Yields`` section instead of a ``Returns`` section.

    Parameters
    ----------
    n : `int`
        The upper limit of the range to generate, from 0 to ``n`` - 1.

    Yields
    ------
    number : `int`
        The next number in the range of 0 to ``n`` - 1.

    See also
    --------
    moduleLevelFunction

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

    >>> print([i for i in example_generator(4)])
    [0, 1, 2, 3]
    """
    for i in range(n):
        yield i


class ExampleClass(object):
    """Example class.

    Parameters
    ----------
    param1 : `str`
        Description of ``param1``.
    param2 : `list` of `str`
        Description of ``param2``.
    param3 : `int`, optional
        Description of ``param3``.
    """

    attr1 = None
    """Description of ``attr1`` (`str`).
    """

    attr2 = None
    """Description of ``attr2`` (`list` of `str`).
    """

    attr3 = None
    """Description of ``attr3`` (`int`).
    """

    attr4 = None
    """Description of ``attr4`` (`list` of `str`).
    """

    def __init__(self, param1, param2, param3=None):
        self.attr1 = param1
        self.attr2 = param2
        self.attr3 = param3

        self.attr4 = ['attr4']

    @property
    def readonlyProperty(self):
        """Properties are documented in their getter method (`str`, read-only).
        """
        return 'readonlyProperty'

    @property
    def readwriteProperty(self):
        """Properties with both a getter and setter are documented in their
        getter method (`list` of `str`).

        If the setter method contains notable behavior, it should be mentioned
        here as well.
        """
        return self.attr4

    @readwriteProperty.setter
    def readwriteProperty(self, value):
        self.attr4 = value

    def exampleMethod(self, param1, param2):
        """Test that a situation is true.

        Class methods are similar to regular functions. Always use the
        imperative mood when writing the one-sentence summary of a method or
        function.

        Parameters
        ----------
        param1 : obj
            The first parameter.
        param2 : obj
            The second parameter.

        Returns
        -------
        success : `bool`
            `True` if successful, `False` otherwise.

        Notes
        -----
        Do not include the ``self`` parameter in the ``Parameters`` section.
        """
        return True

    def __special__(self):
        """At the moment, special members with docstrings are not published in
        the HTML documentation.

        You must still document special members.

        Notes
        -----
        Special members are any methods or attributes that start with and end
        with a double underscore.
        """
        pass

    def _private(self):
        """By default private members are not included in the HTML docs either.

        Notes
        -----
        Private members are any methods or attributes that start with an
        underscore and are *not* special. By default they are not included in
        the output.

        However, you should still provide docstrings for private members to
        document code for internal developers.
        """
        pass


class ExampleError(Exception):
    """Example exception.

    Exceptions are documented in the same manner as other classes.

    Parameters
    ----------
    msg : `str`
        Human readable string describing the exception.
    code : `int`, optional
        Numeric error code.

    Notes
    -----
    Do not include the ``self`` parameter in the ``Parameters`` section.
    """

    msg = None
    """Human readable string describing the exception (`str`).
    """

    code = None
    """Numeric error code (`int`).
    """

    def __init__(self, msg, code=None):
        self.msg = msg
        self.code = code

Acknowledgements

These docstring guidelines are derived/adapted from the NumPy and Astropy documentation. The example module is adapted from the Napoleon documentation.

NumPy is Copyright © 2005-2013, NumPy Developers.

Astropy is Copyright © 2011-2015, Astropy Developers.