51

Is there any way to automatically show variables var1 and var2 and their init-values in sphinx documentation?

class MyClass:
    """    
    Description for class
    """

    def __init__(self, par1, par2):
       self.var1 = par1 * 2
       self.var2 = par2 * 2

    def method(self):
       pass
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Meloun
  • 13,601
  • 17
  • 64
  • 93

2 Answers2

72

Your variables are instance variables, not class variables.

Without attaching a docstring (or a #: "doc comment") to the variables, they won't be documented. You could do as follows:

class MyClass(object):
    """    
    Description for class 

    """

    def __init__(self, par1, par2):
        self.var1 = par1 #: initial value: par1
        self.var2 = par2 #: initial value: par2

    def method(self):
        pass

But I would prefer to include variable documentation by using info fields:

class MyClass(object):
    """    
    Description for class

    :ivar var1: initial value: par1
    :ivar var2: initial value: par2
    """

    def __init__(self, par1, par2):
        self.var1 = par1 
        self.var2 = par2 

    def method(self):
        pass

See also:

Community
  • 1
  • 1
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • Any setting required for #: doc comment to be visible in html pages by sphinx? I used sphinx-apidoc command to generate .rst files. – varunsinghal Jun 25 '17 at 04:05
  • @varunsinghal: is there a problem? – mzjn Jun 26 '17 at 19:38
  • I was using these doc comments #: for private class variables. – varunsinghal Jun 27 '17 at 05:57
  • @varunsinghal: Perhaps you have been hit by this bug? https://github.com/sphinx-doc/sphinx/issues/1362 – mzjn Jun 28 '17 at 13:26
  • 4
    Nice answer! You do not need to use `:ivar foo:` though, just `:var foo` will do. – RayLuo Dec 20 '18 at 22:30
  • 2
    @RayLuo They're not synonyms though - `ivar` = instance variable, `cvar` = class variable, `var` = work it out yourself. While Sphinx doesn't care it informs a human reader. – c z Jan 14 '21 at 09:48
0

Doc comments: you can also put doc comments above

Besides the self.var1 = par1 # Doc for var1 syntax you can also:

main.py

class MyOtherClass:
    """
    This class does that.
    """
    pass

class MyClass:
    """
    Description for class.
    """

    #: Syntax also works for class variables.
    class_var: int = 1

    def __init__(self, par1: int, par2: MyOtherClass):
        #: Doc for var1
        self.var1: int = par1

        #: Doc for var2.
        #: Another line!
        self.var2: MyOtherClass = par2

    def method(self):
        """
        My favorite method.
        """
        pass

    @classmethod
    def cmethod():
        """
        My favorite class method.
        """
        pass

build.sh

sphinx-build . out

conf.py

import os
import sys
sys.path.insert(0, os.path.abspath('.'))
extensions = [ 'sphinx.ext.autodoc' ]
autodoc_default_options = {
    'members': True,
}
autodoc_typehints = "description"

index.rst

.. automodule:: main

requirements.txt

Sphinx==6.1.3

After ./build.sh the output under out/index.html looks like:

enter image description here

The #: syntax is documented at: https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoproperty

Using :ivar: and :cvar: instead

There are currently tradeoffs between both methods, it is a shame that there isn't one clearly superior method.

Downsides:

  • you have to type the attribute names again
  • types are gone, TODO link to feature request

Upsides:

  • the "Variables:` grouping looks cleaner, TODO link to feature request

Both could be better:

  • clearly show that class_var is a class variable? TODO link to feature reuqest
class MyClass:
    """
    Description for class.

    :ivar var1: Doc for var1
    :ivar var2: Doc for var2.
      Another line!
    :cvar class_var: Syntax also works for class variables.
    """

    class_var: int

    def __init__(self, par1: int, par2: MyOtherClass):
        self.var1: int = par1

        self.var2: MyOtherClass = par2

    def method(self):
        """
        My favorite method.
        """
        pass

    @classmethod
    def cmethod():
        """
        My favorite class method.
        """
        pass

produces:

enter image description here

Related: What is the difference between var, cvar and ivar in python's sphinx?

Tested on Python 3.10, Ubuntu 22.10.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985