Skip to content

Toml edit

toml_edit

Shared tomlkit container helpers for metadata merging.

table_at(container, key)

Return the table at key, creating an empty one when absent.

Parameters:

Name Type Description Default
container TomlContainer

The document or table to read the branch from.

required
key str

The key holding the nested table.

required

Returns:

Type Description
Table

The existing table, or the freshly created and attached one.

Raises:

Type Description
ValueError

When key already holds a non-table value.

Source code in packages/axm-init/src/axm_init/core/toml_edit.py
Python
def table_at(container: TomlContainer, key: str) -> Table:
    """Return the table at *key*, creating an empty one when absent.

    Args:
        container: The document or table to read the branch from.
        key: The key holding the nested table.

    Returns:
        The existing table, or the freshly created and attached one.

    Raises:
        ValueError: When *key* already holds a non-table value.
    """
    value = container.get(key)
    if value is None:
        created = table()
        container[key] = created
        return created
    if not isinstance(value, Table):
        msg = f"{key!r} must be a TOML table"
        raise ValueError(msg)
    return value