xref: /petsc/src/binding/petsc4py/docs/source/documentation_standards.rst (revision 9690009e20e1459e990739dff3c49e091225d41b)
155a74a43SLisandro DalcinDocumentation standards for PETSc4py
255a74a43SLisandro Dalcin====================================
355a74a43SLisandro Dalcin
455a74a43SLisandro DalcinSubject to exceptions given below, new contributions to PETSc4py **must**
555a74a43SLisandro Dalcininclude `type annotations <python:typing>` for function parameters and results,
655a74a43SLisandro Dalcinand docstrings on every class, function and method.
755a74a43SLisandro Dalcin
855a74a43SLisandro DalcinThe documentation should be consistent with the corresponding C API
955a74a43SLisandro Dalcindocumentation, including copying text where this is appropriate. More in-depth
1055a74a43SLisandro Dalcindocumentation from the C API (such as extended discussions of algorithmic or
1155a74a43SLisandro Dalcinperformance factors) should not be copied.
1255a74a43SLisandro Dalcin
1355a74a43SLisandro DalcinDocstring standards
1455a74a43SLisandro Dalcin-------------------
1555a74a43SLisandro DalcinDocstrings are to be written in `numpydoc:format` format.
1655a74a43SLisandro Dalcin
1755a74a43SLisandro DalcinThe first line of a function or method docstring must be a short description of
1855a74a43SLisandro Dalcinthe method in imperative mood ("Return the norm of the matrix.") "Return" is
1955a74a43SLisandro Dalcinto be preferred over "Get" in this sentence. A blank line must follow this
200898713fSStefano Zampinidescription. Use one-liner descriptions for properties.
2155a74a43SLisandro Dalcin
2255a74a43SLisandro DalcinIf the corresponding C API documentation lists a function as being collective,
23e4efd8cbSStefano Zampinithen this information must be repeated on the next line of the docstring.
24e4efd8cbSStefano ZampiniValid strings are: "Not collective.", "Logically collective.", "Collective.",
25e4efd8cbSStefano Zampinior "Neighborwise collective.".
2655a74a43SLisandro Dalcin
2755a74a43SLisandro DalcinThe initial description section can contain more information if this is useful.
2855a74a43SLisandro DalcinIn particular, if there is a PETSc manual chapter about a class, then this
2955a74a43SLisandro Dalcinshould be referred to from here.
3055a74a43SLisandro Dalcin
31*9690009eSJose E. RomanUse double backticks around literals (like strings and numbers), e.g.,
3255a74a43SLisandro Dalcin\`\`2\`\`, \`\`"foo"\`\`.
3355a74a43SLisandro Dalcin
34*9690009eSJose E. RomanReference PETSc functions simply using backticks, e.g., `petsc.KSP` refers to
3555a74a43SLisandro Dalcinthe PETSc C documentation for KSP. Do **not** use URLs in docstrings. Always
3655a74a43SLisandro Dalcinuse Intersphinx references.
3755a74a43SLisandro Dalcin
3855a74a43SLisandro DalcinThe following sections describe the use of numpydoc sections. Other sections
3955a74a43SLisandro Dalcinallowed by numpydoc may be included if they are useful.
4055a74a43SLisandro Dalcin
4155a74a43SLisandro DalcinParameters
4255a74a43SLisandro Dalcin..........
4355a74a43SLisandro Dalcin
4455a74a43SLisandro DalcinThis is required unless there are no parameters, or it will be completely
4555a74a43SLisandro Dalcinobvious to even a novice user what the parameters do.
4655a74a43SLisandro Dalcin
4755a74a43SLisandro DalcinTypes should only be specified in this section if for some reason the types
4855a74a43SLisandro Dalcinprovided by typing prove to be inadequate. If no type is being specified, do
4955a74a43SLisandro Dalcinnot include a colon (``:``) to the right of the parameter name.
5055a74a43SLisandro Dalcin
5155a74a43SLisandro DalcinUse `Sys.getDefaultComm` when specifying the default communicator.
5255a74a43SLisandro Dalcin
5355a74a43SLisandro DalcinReturns
5455a74a43SLisandro Dalcin.......
5555a74a43SLisandro Dalcin
5655a74a43SLisandro DalcinThis should only be specified if the return value is not obvious from the
5755a74a43SLisandro Dalcininitial description and typing.
5855a74a43SLisandro Dalcin
5955a74a43SLisandro DalcinIf a "Returns" section is required, the type of the returned items *must* be
6055a74a43SLisandro Dalcinspecified, even if this duplicates typing information.
6155a74a43SLisandro Dalcin
6255a74a43SLisandro DalcinSee Also
6355a74a43SLisandro Dalcin........
6455a74a43SLisandro Dalcin
6555a74a43SLisandro DalcinIf any of the following apply, then this section is required. The order of
6655a74a43SLisandro Dalcinentries is as follows. Other links are permitted in this section if they add
6755a74a43SLisandro Dalcininformation useful to users.
6855a74a43SLisandro Dalcin
6955a74a43SLisandro DalcinEvery ``setFromOptions`` must include the link \`petsc_options\`.
7055a74a43SLisandro Dalcin
7155a74a43SLisandro DalcinAny closely related part of the PETSc4py API not already linked in the
7255a74a43SLisandro Dalcindocstring should appear (e.g. setters and getters should cross-refer).
7355a74a43SLisandro Dalcin
7455a74a43SLisandro DalcinIf there is a corresponding C API documentation page, this must be linked from
75*9690009eSJose E. Romanthe "See also" section, e.g. \`petsc.MatSetValues\`.
7655a74a43SLisandro Dalcin
7755a74a43SLisandro DalcinEnd docstring with an empty line - "closing three quotation marks must be on a
7855a74a43SLisandro Dalcinline by itself, preferably preceded by a blank line"
7955a74a43SLisandro Dalcin
8055a74a43SLisandro Dalcin.. warning::
8155a74a43SLisandro Dalcin
8255a74a43SLisandro Dalcin    The docstrings must not cause Sphinx warnings.
8355a74a43SLisandro Dalcin
8455a74a43SLisandro Dalcin
8555a74a43SLisandro DalcinType hint standards
8655a74a43SLisandro Dalcin-------------------
8755a74a43SLisandro Dalcin
8855a74a43SLisandro DalcinIf returning ``self``, use ``-> Self`` in function signature.
8955a74a43SLisandro Dalcin
9055a74a43SLisandro DalcinType hints are not required when the static type signature includes a PETSc
9155a74a43SLisandro Dalcintype (e.g. ``Vec x``). These will be automatically generated. This will also
9255a74a43SLisandro Dalcinwork for ``= None``. When using type hints, use spacing around the equals in
9355a74a43SLisandro Dalcinany ``= None``.
9455a74a43SLisandro Dalcin
9555a74a43SLisandro DalcinCommunicators in type signatures must use Python typing instead of c-typing
9655a74a43SLisandro Dalcin(i.e. ``comm: Comm`` not ``Comm comm``). This is because communicators
9755a74a43SLisandro Dalcincan come from ``mpi4py`` and not just the ``petsc4py.PETSc.Comm`` class.
9855a74a43SLisandro Dalcin
9955a74a43SLisandro DalcinFor petsc4py native types that are can be strings, the type is ``argument:
100*9690009eSJose E. RomanKSP.Type | str`` (not e.g.: ``KSPType argument``). If the type is strictly an
10155a74a43SLisandro Dalcinenum the ``| str`` can be omitted. Full signature example::
10255a74a43SLisandro Dalcin
10355a74a43SLisandro Dalcin    def setType(self, ksp_type: KSP.Type | str) -> None:
10455a74a43SLisandro Dalcin
105*9690009eSJose E. RomanIf a NumPy array is returned, use ``ArrayInt``/``ArrayReal``/``ArrayScalar`` as the
10655a74a43SLisandro Dalcinreturn type.
107