topmark.toml.utils¶
Small helpers for building and normalizing TopMark TOML values and tables.
These helpers keep TOML construction and rendering code concise while
preserving the strict invariant enforced by
TomlValue: absent values must be omitted from
TopMark TOML tables rather than represented explicitly as None.
This module also contains small, side-effect-free builders for TOML-compatible
list values whose static types remain compatible with TomlValue under Pyright
strict mode.
insert_if_present ¶
Insert a TOML key only when a value is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
TOML table being constructed. |
required |
key
|
str
|
TOML key to insert. |
required |
value
|
TomlValue | None
|
TOML value to store. When |
required |
Source code in src/topmark/toml/utils.py
strip_none_for_toml ¶
Remove TOML-incompatible None values from nested TOML-shaped data.
TOML has no null. During rendering, keys with None values are omitted
and None items are dropped from lists.
Notes
- The input is typed as
object(notAny) so Pyright does not treat mapping/list iterators asUnknown. - Mapping keys are defensively normalized to strings, since TOML tables are string-keyed.
Source code in src/topmark/toml/utils.py
as_toml_string_list ¶
Return a TOML-compatible list of strings.
Pyright treats list[str] as incompatible with the recursive TomlValue
alias because list is invariant. Building the list through the wider
element type keeps TOML serializer code strict-typing friendly without
casts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Iterable[str]
|
String values to expose as a TOML-compatible list. |
required |
Returns:
| Type | Description |
|---|---|
list[TomlValue]
|
A list whose static type is compatible with |
Source code in src/topmark/toml/utils.py
as_toml_table_list ¶
Return a TOML-compatible list of TOML tables.
This helper mirrors as_toml_string_list, but for iterables of TOML
tables that need to be stored in a list position accepted by TomlValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Iterable[TomlTable]
|
TOML tables to expose as a TOML-compatible list. |
required |
Returns:
| Type | Description |
|---|---|
list[TomlValue]
|
A list whose static type is compatible with |