bagof.converters.collections
Converters for collection types (list, tuple, dict, etc.).
Classes
ToIterable
ToSequence
ToSet
Bases: ToIterable
Converter for abc.Set.
Sets convert element-wise like any other iterable.
Note
The only difference from
ToIterable is the
concrete fallback, so that an abstract Set[int] hint still
produces a real container (frozenset, since abc.Set is
immutable) rather than a bare iterator.
ToMutableSet
ToMapping
ToTuple
ToNamedTuple
Converter for NamedTuple subclasses.
Accepts a mapping keyed by field name, or any sequence in field order, and converts each field through its declared type.
Note
A NamedTuple is a plain tuple subclass at runtime, so it cannot
be a registry key of its own;
ToTuple recognises one
and delegates here.
Example
ToLength
Bases: ToSequence[ITERABLE]
Converter for sequences of a fixed length.
A longer input is truncated to the requested length, and a shorter one is refused. Coercing to a fixed length is a conversion like any other, so the extra items are dropped rather than rejected.
Warning
This is deliberately more permissive than
HasLength in bagof-validators, which
refuses a mismatch in either direction. Validate first if you
need a long sequence to be an error rather than a trim.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
The expected length of the sequence. |
required |
hint
|
Any
|
The type hint to convert to. |
UNSET
|
compose
|
bool
|
Whether to compose this converter with others, when they are
found in |
False
|