topmark.toml.getters¶
Value getters for TOML config tables.
This module contains small helpers for extracting values from parsed TOML tables.
Two families of getters exist:
- Unchecked getters: return defaults and only emit debug logs.
- Checked getters: validate the expected shape and record warnings in a
MutableDiagnosticLog
(and also log a warning).
The checked getters are used when parsing config files so that user mistakes are surfaced without crashing or changing defaulting behavior.
These helpers support checked extraction and local value-shape diagnostics during TOML-backed
parsing. They are not the authority for whole-document TOML schema validation; unknown
sections/keys and malformed section structure belong to topmark.toml.schema.
get_string_value ¶
Extract a string value from a TOML table.
If the value is a str, it is returned as is. If the value is of type
int, float, or bool, it is coerced to a string using str(...).
When the key is missing or the value is not coercible, default is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Table to query. |
required |
key
|
str
|
Key to extract. |
required |
default
|
str
|
Default value if the key is not found or not coercible. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
The extracted or coerced string value, or |
Source code in src/topmark/toml/getters.py
get_string_value_or_none ¶
Extract an optional string value from a TOML table.
If the value is a str, it is returned as is. If the value is of type
int, float, or bool, it is coerced to a string using str(...).
When the key is missing, None is returned. If the key is present but
the value is not coercible, None is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Table to query. |
required |
key
|
str
|
Key to extract. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The extracted or coerced string value, or |
Source code in src/topmark/toml/getters.py
get_bool_value ¶
Extract a boolean value from a TOML table.
If the value is a bool, it is returned as is. If the value is missing or
not of type bool, default is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Table to query. |
required |
key
|
str
|
Key to extract. |
required |
default
|
bool
|
Default value if the key is not found or not coercible. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
The extracted boolean value, or |
Source code in src/topmark/toml/getters.py
get_bool_value_or_none ¶
Extract an optional boolean value from a TOML table.
If the value is a bool, it is returned as is. If the value is missing or
not of type bool, None is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Table to query. |
required |
key
|
str
|
Key to extract. |
required |
Returns:
| Type | Description |
|---|---|
bool | None
|
The extracted boolean value, or |
Source code in src/topmark/toml/getters.py
get_list_value ¶
Extract a list value from a TOML table.
If the key is present and the value is a list, a shallow copy is returned
without item-level validation.
Otherwise, default is returned (or [] when default is None).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Table to query. |
required |
key
|
str
|
Key to extract. |
required |
default
|
list[object] | None
|
Default list when the key is missing or not a list. |
None
|
Returns:
| Type | Description |
|---|---|
list[object]
|
A shallow copy of the list value, |
Source code in src/topmark/toml/getters.py
get_string_value_checked ¶
Return a string value, recording a warning when the type is not str.
Unlike get_string_value(), this helper does not coerce ints/bools/floats
to strings. If the key is missing, default is returned.
Source code in src/topmark/toml/getters.py
get_string_value_or_none_checked ¶
Return an optional string value, warning when present but not str.
Source code in src/topmark/toml/getters.py
get_bool_value_checked ¶
Return a boolean value, recording a warning when the type is not bool.
Unlike get_bool_value(), this helper does not coerce integers.
If the key is missing, default is returned.
Source code in src/topmark/toml/getters.py
get_bool_value_or_none_checked ¶
Return an optional boolean value, warning when present but not bool.
Source code in src/topmark/toml/getters.py
get_int_value_or_none_checked ¶
Return an optional int value, warning when present but not int.
Notes
- Missing key / None -> None
boolis rejected (sinceboolis a subclass ofint).
Source code in src/topmark/toml/getters.py
get_string_list_value_checked ¶
Extract a list of strings from a TOML table, recording a warning when the type is incorrect.
This helper enforces a "list of strings" shape for TOML list fields.
Non-string values in the list are dropped with a diagnostic with a uniform, stable warning location like: "Ignoring non-string entry in [header].fields: ..."
Behavior
- If the key is missing or not a list, returns [].
- If the list contains non-string items, they are ignored.
- Each ignored entry emits a warning and a diagnostic with TOML location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
TOML table to query. |
required |
key
|
str
|
Key to extract. |
required |
where
|
str
|
TOML location prefix (e.g. "[files]"). |
required |
diagnostics
|
MutableDiagnosticLog
|
|
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Filtered list containing only string entries. |
Source code in src/topmark/toml/getters.py
get_enum_value_checked ¶
Parse an enum value from TOML.
Expected input is a str matching one of the Enum values.
- Missing key -> None
- Wrong type -> warning + None
- Unknown enum value -> error + None
This is intended for checked extraction of known enum-valued TOML keys during
parsing. It does not decide whether a key is allowed in a section; unknown-key
and section-shape validation belong to topmark.toml.schema.
Source code in src/topmark/toml/getters.py
get_table_value ¶
Extract a sub-table from a TOML table.
Returns a new empty dict if the sub-table is missing or not a mapping.
This helper is intentionally lossy and does not record diagnostics; callers that need to distinguish "missing" from "present but malformed" should rely on TOML schema validation before using this accessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TomlTable
|
Parent table mapping. |
required |
key
|
str
|
Sub-table key. |
required |
Returns:
| Type | Description |
|---|---|
TomlTable
|
The sub-table if present and a mapping, otherwise an empty dict. |