Skip to content

bagof.factories.collections

Factories for collection types (sequences, mappings, sets, etc.).

Classes

SequenceFactory

Bases: Factory[SEQUENCE]

Factory for [Sequence][bagof.factories.collections.abc.Sequence] hints (a list).

MappingFactory

Bases: Factory[MAPPING]

Factory for [Mapping][bagof.factories.collections.abc.Mapping] hints (a dict).

DictFactory

Bases: MappingFactory

Factory for dict hints (an empty dict).

SetFactory

Bases: Factory[ITERABLE]

Factory for [Set][bagof.factories.collections.abc.Set] hints (a frozenset).

[Set][bagof.factories.collections.abc.Set] is the immutable interface, so it builds a frozenset; [MutableSet][bagof.factories.collections.abc.MutableSet] builds a set.

MutableSetFactory

Bases: SetFactory

Factory for [MutableSet][bagof.factories.collections.abc.MutableSet] (a set).

IterableFactory

Bases: Factory[ITERABLE]

Factory for [Iterable][bagof.factories.collections.abc.Iterable] hints (a list).

Also covers [Collection][bagof.factories.collections.abc.Collection], [Reversible][bagof.factories.collections.abc.Reversible] and [Container][bagof.factories.collections.abc.Container].

IteratorFactory

Bases: Factory[ITERABLE]

Factory for [Iterator][bagof.factories.collections.abc.Iterator] (an empty iterator).

Methods:

__call__
__call__() -> Iterator

Return an empty iterator.

TupleFactory

Bases: Factory[TUPLE]

Factory for tuple hints.

A fixed-length tuple builds a value for each element (Tuple[int, str] -> (0, "")).

Note

A variadic tuple (Tuple[int, ...]), an unparametrised tuple, or the empty tuple (Tuple[()]) builds an empty tuple, since no length is implied.

Methods:

__call__
__call__() -> TUPLE

Build a value for each element of a fixed-length tuple.

NamedTupleFactory

Bases: Factory[TUPLE]

Factory for NamedTuple subclasses.

Builds a real instance, taking each field's declared default where there is one and otherwise building a value from the field's annotation -- the same shape TypedDictFactory has for dicts.

Note

A NamedTuple is a plain tuple subclass at runtime, so it cannot be a registry key of its own; TupleFactory recognises one and delegates here.

Example

>>> import typing_extensions as tx
>>> from bagof.factories import get_factory
>>> class Point(tx.NamedTuple):
...     x: int
...     y: str = "origin"
>>> get_factory(Point)()
Point(x=0, y='origin')

Methods:

__call__
__call__() -> TUPLE

Build an instance, field by field.