Skip to content

bagof.factories.common

Common factories (none, union, literal, typevar, annotated).

Classes

NoneFactory

Bases: Factory[NONE]

Factory for None (always returns None).

Methods:

__call__
__call__() -> NONE

Return None.

UnionFactory

Bases: Factory[T]

Factory for Union hints.

Returns None if the union is optional, otherwise builds a value for the first member type that can be instantiated.

Example

>>> from bagof.factories import get_factory
>>> factory = get_factory(int | str)
>>> factory
UnionFactory(int | str)
>>> factory()
0
>>> get_factory(str | None)()  # optional -> None

Methods:

__call__
__call__() -> T

Build a value for the first instantiable member of the union.

LiteralFactory

Bases: Factory[T]

Factory for Literal hints.

Returns the first literal value (or None if the literal allows it).

Methods:

__call__
__call__() -> T

Return the first value of the literal.

TypeVarFactory

Bases: Factory[T]

Factory for TypeVar hints.

Builds a value for the type the typevar resolves to (its default, bound or constraints).

Methods:

__call__
__call__() -> T

Build a value for the type the typevar resolves to.

AnnotatedFactory

Bases: Factory[T]

Factory for Annotated hints.

Builds a value using the annotated origin type, unless a more specific factory is provided in the annotation metadata.

Attributes

factories property
factories: tuple[Factory, ...]

The factories that apply to this annotated hint, ordered from the origin type (least specific) to the last matching metadata entry (most specific, used first by __call__).

Methods:

register classmethod
register(*hints: Unpack[tuple[Any]]) -> ClassDecorator

Register a factory class for one or more pieces of annotation metadata.

__call__
__call__() -> T

Build a value using the most specific applicable factory.