cadrumo.core.type_guards module¶
Runtime narrowing predicates for untrusted, unparameterised containers.
A bare isinstance(value, dict) narrows to an unparameterised dict, so
every key and value the caller then touches is Unknown to a type checker.
That is not a checker quirk to be silenced: the check genuinely proves only
“this is a mapping”, and these predicates say the rest – the entries are
untrusted object until something validates them.
The predicates exist here rather than beside each caller because the same three shapes are needed wherever a deserialised payload, a parsed TOML root, or a third-party return value is walked. Before this module the pattern was copied into five places under two different names.
is_str_keyed_mapping is deliberately NOT here: the two copies in
domain/iva assert string keys because their input came from rtoml,
which is a caller-specific justification rather than a general narrowing.
- is_object_dict(value)[source]¶
Narrow to a concrete
dictwith untrusted key/value entries.Distinct from
is_object_mapping()on purpose: a caller that rebuilds adict, or that deliberately admits only the concrete type, must not silently start accepting every other mapping.
- is_object_list_or_tuple(value)[source]¶
Narrow to a list or tuple of untrusted entries.
Deliberately not
Sequence:stris a sequence, and every caller of this shape walks elements only after handling text separately.
- is_object_collection(value)[source]¶
Narrow to any of the four unordered-or-ordered element containers.
Deliberately not
Iterable:strandbytesare iterable, and every caller of this shape has already handled text and raw bytes separately.
- is_str_keyed_dict(value)[source]¶
Narrow to a dict whose keys are all strings, checking the keys.
Unlike the container guards above this one does real work at runtime, and it must: a caller that goes on to index by
stris relying on the key type, so asserting it without looking would be a guess dressed as a proof.