Skip to content

bagof.converters.collections

Converters for collection types (list, tuple, dict, etc.).

Classes

ToIterable

ToIterable(hint: Any = UNSET, compose: bool = False)

Bases: Converter[ITERABLE, Any]

Converter for abc.Iterable.

Methods:

like
like(__reentrant: tuple = ()) -> Any

Return the input hint for this converter.

__call__
__call__(value: Any) -> ITERABLE

Convert the value to an iterable, converting each element.

ToSequence

ToSequence(hint: Any = UNSET, compose: bool = False)

Bases: Converter[SEQUENCE, Any]

Converter for abc.Sequence.

Methods:

like
like(__reentrant: tuple = ()) -> Any

Return the input hint for this converter.

__call__
__call__(value: Any) -> SEQUENCE

Convert the value to a sequence, converting each element.

ToSet

ToSet(hint: Any = UNSET, compose: bool = False)

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

ToMutableSet(hint: Any = UNSET, compose: bool = False)

Bases: ToSet

Converter for abc.MutableSet.

ToMapping

ToMapping(hint: Any = UNSET, compose: bool = False)

Bases: Converter[MAPPING, Any]

Converter for abc.Mapping.

Methods:

like
like(__reentrant: tuple = ()) -> Any

Return the input hint for this converter.

__call__
__call__(value: Any) -> MAPPING

Convert the value to a mapping, converting each key and value.

ToTuple

ToTuple(hint: Any = UNSET, compose: bool = False)

Bases: Converter[TUPLE, Any]

Converter for tuple.

Methods:

like
like(__reentrant: tuple = ()) -> Any

Return the input hint for this converter.

__call__
__call__(value: Any) -> TUPLE

Convert the value to a tuple, converting each element.

ToNamedTuple

ToNamedTuple(hint: Any = UNSET, compose: bool = False)

Bases: Converter[TUPLE, Any]

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

>>> import typing_extensions as tx
>>> from bagof.converters import get_converter
>>> class Point(tx.NamedTuple):
...     x: int
...     y: int
>>> get_converter(Point)({"x": "1", "y": "2"})
Point(x=1, y=2)
>>> get_converter(Point)(["1", "2"])
Point(x=1, y=2)

Methods:

like
like(__reentrant: tuple = ()) -> Any

Accept an instance, a mapping by field name, or a sequence.

__call__
__call__(value: Any) -> TUPLE

Convert the value field by field.

ToLength

ToLength(length: int, hint: Any = UNSET, compose: bool = False)

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

>>> from bagof.converters.collections import ToLength
>>> to_pair = ToLength(2, tx.List[int])
>>> to_pair(["1", "2", "3"])
[1, 2]
>>> to_pair(["1"])
ValueConversionError: Expected sequence of length 2, got 1.

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 Annotated metadata.

False

Methods:

__call__
__call__(value: Any) -> ITERABLE

Convert the value to a sequence, trimmed to the length.