xref: /petsc/src/binding/petsc4py/docs/source/documentation_standards.rst (revision 89a64d68d2f8dc8c4ae19e87b21a1e0b1231e51b)
16f336411SStefano ZampiniDocumentation standards for petsc4py
255a74a43SLisandro Dalcin====================================
355a74a43SLisandro Dalcin
46f336411SStefano ZampiniSubject 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
176f336411SStefano ZampiniThe first line of a class, function or method docstring must be a short
186f336411SStefano Zampinidescription of the method in imperative mood ("Return the norm of the matrix.")
196f336411SStefano Zampini"Return" is to be preferred over "Get" in this sentence. A blank line must
206f336411SStefano Zampinifollow this description. Use one-liner descriptions for properties.
2155a74a43SLisandro Dalcin
226f336411SStefano ZampiniIf the corresponding C API documentation of a method lists a function as being
236f336411SStefano Zampinicollective, then this information must be repeated on the next line of the
246f336411SStefano Zampinidocstring. Valid strings are: "Not collective.", "Logically collective.",
256f336411SStefano Zampini"Collective.", "Neighborwise collective.", or "Collective the first time it is
266f336411SStefano Zampinicalled".
2755a74a43SLisandro Dalcin
2855a74a43SLisandro DalcinThe initial description section can contain more information if this is useful.
2955a74a43SLisandro DalcinIn particular, if there is a PETSc manual chapter about a class, then this
3055a74a43SLisandro Dalcinshould be referred to from here.
3155a74a43SLisandro Dalcin
329690009eSJose E. RomanUse double backticks around literals (like strings and numbers), e.g.,
3355a74a43SLisandro Dalcin\`\`2\`\`, \`\`"foo"\`\`.
3455a74a43SLisandro Dalcin
359690009eSJose E. RomanReference PETSc functions simply using backticks, e.g., `petsc.KSP` refers to
3655a74a43SLisandro Dalcinthe PETSc C documentation for KSP. Do **not** use URLs in docstrings. Always
3755a74a43SLisandro Dalcinuse Intersphinx references.
3855a74a43SLisandro Dalcin
3955a74a43SLisandro DalcinThe following sections describe the use of numpydoc sections. Other sections
4055a74a43SLisandro Dalcinallowed by numpydoc may be included if they are useful.
4155a74a43SLisandro Dalcin
4255a74a43SLisandro DalcinParameters
4355a74a43SLisandro Dalcin..........
4455a74a43SLisandro Dalcin
456f336411SStefano ZampiniThis is required for methods unless there are no parameters, or it will be
466f336411SStefano Zampinicompletely obvious to even a novice user what the parameters do.
4755a74a43SLisandro Dalcin
486f336411SStefano ZampiniIf a class has a non-trivial constructor, the arguments of the constructor and
496f336411SStefano Zampinitheir types must be explicitly documented within this section.
506f336411SStefano Zampini
516f336411SStefano ZampiniFor methods, types should only be specified in this section if for some reason
526f336411SStefano Zampinithe types provided by typing prove to be inadequate. If no type is being
536f336411SStefano Zampinispecified, do not include a colon (``:``) to the right of the parameter name.
5455a74a43SLisandro Dalcin
5555a74a43SLisandro DalcinUse `Sys.getDefaultComm` when specifying the default communicator.
5655a74a43SLisandro Dalcin
5755a74a43SLisandro DalcinReturns
5855a74a43SLisandro Dalcin.......
5955a74a43SLisandro Dalcin
6055a74a43SLisandro DalcinThis should only be specified if the return value is not obvious from the
6155a74a43SLisandro Dalcininitial description and typing.
6255a74a43SLisandro Dalcin
6355a74a43SLisandro DalcinIf a "Returns" section is required, the type of the returned items *must* be
6455a74a43SLisandro Dalcinspecified, even if this duplicates typing information.
6555a74a43SLisandro Dalcin
6655a74a43SLisandro DalcinSee Also
6755a74a43SLisandro Dalcin........
6855a74a43SLisandro Dalcin
6955a74a43SLisandro DalcinIf any of the following apply, then this section is required. The order of
7055a74a43SLisandro Dalcinentries is as follows. Other links are permitted in this section if they add
7155a74a43SLisandro Dalcininformation useful to users.
7255a74a43SLisandro Dalcin
73*c80e1b15SStefano ZampiniEvery ``setFromOptions`` must include the link ``petsc_options``.
7455a74a43SLisandro Dalcin
756f336411SStefano ZampiniAny closely related part of the petsc4py API not already linked in the
7655a74a43SLisandro Dalcindocstring should appear (e.g. setters and getters should cross-refer).
7755a74a43SLisandro Dalcin
7855a74a43SLisandro DalcinIf there is a corresponding C API documentation page, this must be linked from
79*c80e1b15SStefano Zampinithe "See also" section, e.g. ``petsc.MatSetValues``.
8055a74a43SLisandro Dalcin
8155a74a43SLisandro DalcinEnd docstring with an empty line - "closing three quotation marks must be on a
8255a74a43SLisandro Dalcinline by itself, preferably preceded by a blank line"
8355a74a43SLisandro Dalcin
8455a74a43SLisandro DalcinType hint standards
8555a74a43SLisandro Dalcin-------------------
8655a74a43SLisandro Dalcin
8755a74a43SLisandro DalcinIf returning ``self``, use ``-> Self`` in function signature.
8855a74a43SLisandro Dalcin
8955a74a43SLisandro DalcinType hints are not required when the static type signature includes a PETSc
9055a74a43SLisandro Dalcintype (e.g. ``Vec x``). These will be automatically generated. This will also
9155a74a43SLisandro Dalcinwork for ``= None``. When using type hints, use spacing around the equals in
9255a74a43SLisandro Dalcinany ``= None``.
9355a74a43SLisandro Dalcin
9455a74a43SLisandro DalcinCommunicators in type signatures must use Python typing instead of c-typing
9555a74a43SLisandro Dalcin(i.e. ``comm: Comm`` not ``Comm comm``). This is because communicators
9655a74a43SLisandro Dalcincan come from ``mpi4py`` and not just the ``petsc4py.PETSc.Comm`` class.
9755a74a43SLisandro Dalcin
986f336411SStefano ZampiniFor petsc4py native types that are strings, the type is ``argument:
999690009eSJose E. RomanKSP.Type | str`` (not e.g.: ``KSPType argument``). If the type is strictly an
10055a74a43SLisandro Dalcinenum the ``| str`` can be omitted. Full signature example::
10155a74a43SLisandro Dalcin
10255a74a43SLisandro Dalcin    def setType(self, ksp_type: KSP.Type | str) -> None:
10355a74a43SLisandro Dalcin
1046f336411SStefano ZampiniIf a NumPy array is returned, use
1056f336411SStefano Zampini``ArrayBool``/``ArrayInt``/``ArrayReal``/``ArrayScalar`` as the return type.
106