Skip to content

bagof.validators.collections

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

Classes

IsIterable

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

Bases: Validator[ITERABLE]

Validator for abc.Iterable.

Note

When parameterized (e.g. Iterable[int]), each element is validated against the argument type. A one-shot iterator or generator cannot be checked this way -- walking it would consume the value being validated -- so it raises a TypeValidationError instead.

IsSequence

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

Bases: IsIterable[SEQUENCE]

Validator for abc.Sequence.

IsIterator

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

Bases: IsIterable[ITERABLE]

Validator for Iterator.

Only the container type is checked. An iterator yields itself from __iter__, so walking it to check elements would consume the very value being validated -- a parameterized hint like Iterator[int] therefore passes any iterator, and its elements are left unchecked.

IsSet

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

Bases: IsIterable[ITERABLE]

Validator for Set.

A set is finite and re-iterable, so a parameterized hint (e.g. Set[int]) checks every member.

IsFrozenSet

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

Bases: IsSet[ITERABLE]

Validator for frozenset.

IsMutableSet

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

Bases: IsSet[ITERABLE]

Validator for MutableSet.

IsMapping

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

Bases: Validator[MAPPING]

Validator for abc.Mapping.

IsTupleIsh

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

Bases: Validator[TUPLE]

Per-item validator for sequence (not necessarily tuple) containers.

Note

This validator is not registered by default, so it is only used when instantiated explicitly.

IsTuple

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

Bases: IsTupleIsh[TUPLE]

Validator for tuple.

IsDict

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

Bases: IsMapping[MAPPING]

Validator for dict.

IsTypedDict

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

Bases: IsMapping[MAPPING]

Validator for TypedDict.

HasLength

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

Bases: IsSequence[ITERABLE]

Validator for abc.Sequence, that checks its length.

Note

This validator is not registered by default, so it is only used when instantiated explicitly, e.g. HasLength(3).

Parameters:

Name Type Description Default
length int

The expected length of the sequence.

required
hint Any

The type hint to validate against.

UNSET
compose bool

Whether to compose this validator with others, when they are found in Annotated metadata.

False

Methods: