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

3

70

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

Cano answered 1/7, 2018 at 17:21 Comment(3)
It seems dataclasses automatically generate a docstring for the class that include the type hints from the class definition, for example 'C(name: str, number: int)', but the docstring for the automatically generated __init__ method is None. So I suppose you could manually assign the __init__ docstring after the class definition. A bit clunky though.Karolyn
This autogenerated docstring doesn't show up if I already have a docstring on the class, which is fine, because a human-supplied docstring is (usually!) much better than an autogenerated one. Manually assigning the docstring is definitely clunky and something I'd like to avoid if possible, hence this question.Cano
True on both points. Also, a manually assigned docstring will work for runtime tools like help, but perhaps not for documentation generators like Sphinx.Karolyn
C
61

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 a dataclasses' attributes should be straight forward mappings to the constructor's arguments, there should usually be little 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.

Crownpiece answered 2/7, 2018 at 8:30 Comment(9)
This is delightfully in-depth, thank you! I especially appreciate the reference to the Sphinx docs that cover precisely this case.Cano
@Crownpiece bro, what configuration for Sphinx do you use? I can't achieve attributes documentation generation, like in your third example.Fennessy
@Fennessy 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.Crownpiece
@Crownpiece thank you! I've got it worked with sphinx.ext.napoleon extension enabled.Fennessy
@Crownpiece 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 hintsQuass
@Quass 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.Crownpiece
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.Crownpiece
@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 dataclassQuass
its probalby an issue with PyCharm, but this solution leads to "unresolved refrence" errors in the IDE, while the solution by @Sterrett does notLimassol
S
10

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
Sterrett answered 12/2, 2021 at 8:50 Comment(2)
"Not sure why rendered results by @Crownpiece look like that" -> Something changed in how sphinx rendered attributes by default: https://mcmap.net/q/281433/-how-napoleon-and-autodoc-interact-documenting-members/962190Crownpiece
this solution works with IDE like PyCharm, while other solutions raise errors with unresolved references.Limassol
R
7

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
'''
Rugen answered 1/7, 2018 at 18:6 Comment(3)
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!Cano
@Rugen But is it possible to eliminate dataclass completely from the documentation?Fordham
It's worth noting that help(InventoryItem) will not show the attributes name, unit_price, or quantity_on_hand or their docstrings.Send

© 2022 - 2024 — McMap. All rights reserved.