How do I document a constructor for a class using Python dataclasses?

17,815

Solution 1

The napoleon-style docstrings as they are described in the sphinx docs (see the ExampleError class for their take on it) explicitly touch on your case:

The __init__ method may be documented in either the class level docstring, or as a docstring on the __init__ method itself.

And if you do not want this behavior, you have to explicitly tell sphinx that the constructor docstring and the class docstring are not the same thing.

Meaning, you can just paste your constructor info into the body of the class docstring.


In case you build documents from your docstrings, these are the granularities that can be achieved:

1) The bare minimum:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.
    """
    var_int: int
    var_str: str

enter image description here

2) Additional constructor parameter description:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.

    Args:
        var_int (int): An integer.
        var_str (str): A string.

    """
    var_int: int
    var_str: str

enter image description here

3) Additional attribute description:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.

    Attributes:
        var_int (int): An integer.
        var_str (str): A string.

    """
    var_int: int
    var_str: str

enter image description here


Parameter and attribute descriptions can of course be combined as well, but since dataclasses should be straight forward mappings I don't see a reason to do so.

In my opinion, 1) would do for small or simple dataclasses -- it already includes the constructor signature with their respective types, which is plenty for a dataclass. If you want to say more about each attribute, 3) would serve best.

Solution 2

A major advantage of dataclasses is that they are self-documenting. Assuming the reader of your code knows how dataclasses work (and your attributes are appropriately named), the type-annotated class attributes should be excellent documentation of the constructor. See this example from the official dataclass docs:

@dataclass
class InventoryItem:
    '''Class for keeping track of an item in inventory.'''
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

If you don't expect that readers of your code would know how dataclasses work then you might want to reconsider using them or adding an explanation or link to the docs in an inline comment after the @dataclass decorator. If you really need a docstring for a dataclass, I'd recommend putting the constructor docstring within the class docstring. For the example above:

'''Class for keeping track of an item in inventory.

Constructor arguments:
:param name: name of the item
:param unit_price: price in USD per unit of the item
:param quantity_on_hand: number of units currently available
'''

Solution 3

I think the easiest way is:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.

    """
    var_int: int  #: An integer.

    #: A string.
    #: (Able to have multiple lines.)
    var_str: str

    var_float: float
    """A float. (Able to have multiple lines.)"""

Not sure why rendered results by @Arne look like that. In my case, attributes in a dataclass will always show regardless of the docstring. That is:

1) The bare minimum:

2) Additional constructor parameter description:

3) Additional attribute description:

Probably because I have set something wrong in my conf.py (Sphinx v3.4.3, Python 3.7):

extensions = [
    "sphinx.ext.napoleon",
    "sphinx.ext.autodoc",
    "sphinx_autodoc_typehints",
    "sphinx.ext.viewcode",
    "sphinx.ext.autosectionlabel",
]

# Napoleon settings
napoleon_google_docstring = True
napoleon_include_init_with_doc = True
Share:
17,815
anahata
Author by

anahata

Updated on June 03, 2022

Comments

  • anahata
    anahata about 2 years

    I have some existing Python 3.6 code that I'd like to move to Python 3.7 dataclasses. I have __init__ methods with nice docstring documentation, specifying the attributes the constructors take and their types.

    However, if I change these classes to use the new Python dataclasses in 3.7, the constructor is implicit. How do I provide constructor documentation in this case? I like the idea of dataclasses, but not if I have to forego clear documentation to use them.

    edited to clarify I'm using docstrings presently

  • anahata
    anahata almost 6 years
    Hmmm. This places a greater emphasis on the importance of naming your attributes if they're truly going to be self-documenting. Thank you for the suggestions!
  • anahata
    anahata almost 6 years
    This is delightfully in-depth, thank you! I especially appreciate the reference to the Sphinx docs that cover precisely this case.
  • Arne
    Arne almost 6 years
    Glad it helps you =)
  • Dmytro Chasovskyi
    Dmytro Chasovskyi over 4 years
    @orn688 But is it possible to eliminate dataclass completely from the documentation?
  • Mike Holler
    Mike Holler over 4 years
    It's worth noting that help(InventoryItem) will not show the attributes name, unit_price, or quantity_on_hand or their docstrings.
  • woozly
    woozly about 4 years
    @Arne bro, what configuration for Sphinx do you use? I can't achieve attributes documentation generation, like in your third example.
  • Arne
    Arne about 4 years
    @woozly I just re-ran what I think was my sample cpde, and I can't get them to look the same. Maybe sphinx changed how they treat dataclasses? Anyway, my conf.pys all look more or less like this(just switch the theme to "alabaster" for the look in the post here), and the docs are built as described here.
  • woozly
    woozly about 4 years
    @Arne thank you! I've got it worked with sphinx.ext.napoleon extension enabled.
  • magomar
    magomar over 3 years
    @Arne I have a simmilar configuraiton and my problem is that I'm getting the fields twice in the docs, once with my docstring, the other one auto-generated with type hints
  • Arne
    Arne over 3 years
    @magomar You're right, I just re-ran it the code and am also getting duplicate variable descriptions that didn't appear before. This answer is now out of date as sphinx started giving dataclasses special treatment.
  • Arne
    Arne over 3 years
    correction - all classes now get their classvars listen in the docstring rendered by sphinx, not only dataclasses. I'm following this up in this new questions and will update this answer accordingly.
  • magomar
    magomar over 3 years
    @bad_coder Gracias, lo acabo de ver. Lo tengo que estudiar más despacio, pero dado que genero los rst de forma automática no creo que me resultw práctico tener que añadir excepciones para cada dataclass
  • Arne
    Arne about 3 years
    "Not sure why rendered results by @Arne look like that" -> Something changed in how sphinx rendered attributes by default: stackoverflow.com/q/64588821/962190
  • Dan Ciborowski - MSFT
    Dan Ciborowski - MSFT over 2 years
    its probalby an issue with PyCharm, but this solution leads to "unresolved refrence" errors in the IDE, while the solution by @Edward does not
  • Dan Ciborowski - MSFT
    Dan Ciborowski - MSFT over 2 years
    this solution works with IDE like PyCharm, while other solutions raise errors with unresolved references.