topmark.core.typing_guards¶
topmark / core / typing_guards
Generic type guards and normalization helpers for TopMark.
This module provides TypeGuard-based predicates that help Pyright narrow runtime values coming
from weakly typed objects, e.g. values stored in the Click context.
It also includes small, side-effect-free normalization helpers such as as_object_dict, plus
permissive generic mapping-extraction helpers used by runtime/config payload shaping. These helpers
are meant for already-normalized Python mappings, not TOML-schema validation.
See Also:
- topmark.toml.typing_guards: type guards and normalization helpers
for TOML parsing.
is_mapping ¶
Type guard for a Mapping value.
Checks only that the value is a Mapping; does not validate item types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
Value to test. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[Mapping[object, object]]
|
True if obj is a Mapping. |
Source code in src/topmark/core/typing_guards.py
is_any_list ¶
Type guard for a generic list value.
Checks only that the value is a list; does not validate item types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
Value to test. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[list[object]]
|
True if obj is a list. |
Source code in src/topmark/core/typing_guards.py
is_str_list ¶
Type guard for a string list value.
Checks that the value is a list[str].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
Value to test. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[list[str]]
|
True if obj is a list[str]. |
Source code in src/topmark/core/typing_guards.py
as_object_dict ¶
Return value as a shallow dict[str, object] when possible.
This helper is intentionally permissive and is meant for post-normalization
payload shaping, not TOML-schema validation. Non-dictionary inputs yield an
empty dictionary. Dictionary keys are stringified to provide a stable
dict[str, object] result for downstream machine-payload builders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Arbitrary runtime value. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
A shallow dictionary with string keys when |
dict[str, object]
|
otherwise |
Source code in src/topmark/core/typing_guards.py
get_object_dict_value ¶
Return a shallow dict[str, object] value for key when present.
This helper is intentionally permissive and is meant for generic mapping extraction from already-normalized runtime/config payloads. It is not a TOML schema-validation helper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, object]
|
Mapping to inspect. |
required |
key
|
str
|
Key to extract. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
A shallow dictionary with stringified keys when the value at |
dict[str, object]
|
plain |
Source code in src/topmark/core/typing_guards.py
get_string_dict_value ¶
Return a dict[str, str] value for key, filtering invalid items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, object]
|
Mapping to inspect. |
required |
key
|
str
|
Key to extract. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A dictionary containing only |
dict[str, str]
|
at |
Source code in src/topmark/core/typing_guards.py
get_string_list_dict_value ¶
Return a dict[str, list[str]] value for key, filtering invalid items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, object]
|
Mapping to inspect. |
required |
key
|
str
|
Key to extract. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
A dictionary containing only |
dict[str, list[str]]
|
stored at |
dict[str, list[str]]
|
|