Skip to main content

Generate commented YAML input files and markdown documentation from dataclasses.

Project description


title: README author: Jan-Michael Rye

Hatch Latest Release License Pipeline Status PyPI PyPI Downloads Pylint Test Coverage

Synopsis

A Python package and command-line utility to automate documentation of Python dataclasses. It can generate the following:

  • Commented YAML input files which can be used as user configuration files.
  • Markdown output for README files which contain the dataclass docstring and commented YAML.

Links

GitLab

Other Repositories

Usage

Command-Line Utility

The package installs the dataclass_documenter which can be used to generate the output from the command-line.

usage: dataclass_documenter [-h] [-m] [-l LEVEL] dataclass

Generate Markdown or YAML for a dataclass. Note that this will load the module
containing the target dataclass and thus execute any top-level code within
that module.

positional arguments:
  dataclass          The target dataclass, specified as
                     "<module|file>:<class>", e.g. "mypkg.mod1:MyDataclass" or
                     "path/to/src.py:MyDataclass".

options:
  -h, --help         show this help message and exit
  -m, --markdown     Output Markdown documentation instead of just the YAML
                     input file.
  -l, --level LEVEL  Nesting level of output. For YAML this defines the
                     indentation level. For Markdown this defines the header
                     level.

Python API

The package can also be used directly via its API.

# Import the documenter class.
from dataclass_documenter import Dataclass_Documenter

# Initialize it with a custom dataclass. Here we assume that MyDataclass is a
defined dataclass.
dado = Dataclass_Documenter(MyDataclass)

# Retrieve YAML and/or Markdown text.
yaml_output = dado.get_yaml()
markdown_output = dado.get_markdown()

Example

The following is an example using the dataclasses defined for the unit tests.

Dataclass Definitions

Example dataclasses to show the correpondence between the definition and the generated documentation.

@dataclasses.dataclass
class NestedExampleDataclass:
    """
    Nested dataclass to test recursive documentation.

    Parameters:
        nested_string:
            A string parameter of the nested dataclass.

        nested_number:
            A numerical parameter of the nested dataclass
    """

    nested_string: str = "Another string value."
    nested_number: int | float = 5


@dataclasses.dataclass
class ExampleDataclass:  # pylint: disable=too-many-instance-attributes
    """
    Brief description for example dataclass.

    This is a longer description. This dataclass is used for testing and
    generating an example in the README.

    Parameters:
        string:
            A string parameter.

        nested_dataclass:
            A nested dataclass that encapsulates its own parameters.

        integer:
            An integer parameter.

        floats:
            A list of floats.

        opt_string:
            An optional string that may be None.

        nested_dataclass_list:
            List of nested dataclass objects for testing composite types.
    """

    string: str
    nested_dataclass: NestedExampleDataclass
    integer: int = 7
    floats: list[float] = dataclasses.field(default_factory=list)
    opt_string: Optional[str] = None
    undocumented_string: str = "Intentionally undocumented string."
    nested_dataclass_list: list[NestedExampleDataclass] = dataclasses.field(
        default_factory=list
    )
    nested_dataclass_dict: dict[str, NestedExampleDataclass] = dataclasses.field(
        default_factory=dict
    )

Markdown Output

The following is the Markdowna and YAML output automatically generated from the example dataclasses.

ExampleDataclass

Brief description for example dataclass.

This is a longer description. This dataclass is used for testing and generating an example in the README.

Input
# ExampleDataclass

# A string parameter.
# Type: str [REQUIRED]
string: ...

# A nested dataclass that encapsulates its own parameters.
nested_dataclass:
  # A string parameter of the nested dataclass.
  # Type: str [OPTIONAL]
  nested_string: Another string value.

  # A numerical parameter of the nested dataclass
  # Type: UnionType[int, float] [OPTIONAL]
  nested_number: 5

# An integer parameter.
# Type: int [OPTIONAL]
integer: 7

# A list of floats.
# Type: list[float] [OPTIONAL]
floats: []

# An optional string that may be None.
# Type: str [OPTIONAL]
opt_string: null

# Undocumented.
# Type: str [OPTIONAL]
undocumented_string: Intentionally undocumented string.

# List of nested dataclass objects for testing composite types.
# Type: list[NestedExampleDataclass] [OPTIONAL]
nested_dataclass_list: []
  # NestedExampleDataclass

  # A string parameter of the nested dataclass.
  # Type: str [OPTIONAL]
  # - nested_string: Another string value.

  # A numerical parameter of the nested dataclass
  # Type: UnionType[int, float] [OPTIONAL]
  #   nested_number: 5

# Undocumented.
# Type: dict[str, NestedExampleDataclass] [OPTIONAL]
nested_dataclass_dict: {}
  # String key
  # key:
    # NestedExampleDataclass

    # A string parameter of the nested dataclass.
    # Type: str [OPTIONAL]
    # nested_string: Another string value.

    # A numerical parameter of the nested dataclass
    # Type: UnionType[int, float] [OPTIONAL]
    # nested_number: 5

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataclass_documenter-2025.10.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataclass_documenter-2025.10-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_documenter-2025.10.tar.gz.

File metadata

  • Download URL: dataclass_documenter-2025.10.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for dataclass_documenter-2025.10.tar.gz
Algorithm Hash digest
SHA256 7e96d10053438f6c8dd8233bd1e6c6d3bc993d23ab58fd20288f872e927589cd
MD5 85aa8d93675cb1f1f42c4afbf430a8b4
BLAKE2b-256 2e1168bfa4d1c1df0e98764be0189d86f67acc7b2b53ef26e834fc2dcf7a3eba

See more details on using hashes here.

File details

Details for the file dataclass_documenter-2025.10-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclass_documenter-2025.10-py3-none-any.whl
Algorithm Hash digest
SHA256 e3bab06fb0102564c14da2d063f0f19058fa659def056bb9a19a1f2b3fefaa81
MD5 74b1fb5ce1201b797a708e346f978640
BLAKE2b-256 6b1592bb833215543046658ded1390fe80bf7d07ae534c2fc9616789f53108b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page