Skip to content

bagof.factories.base

Base class for all factories.

Attributes

FactoryRegistry module-attribute

FactoryRegistry = dict[tx.Hashable, tx.Type['Factory']]

A registry of factories, mapping type hints to factory classes.

register_factory module-attribute

register_factory = Factory.register

Backward-compatible alias for Factory.register

get_factory module-attribute

get_factory = Factory.get

Backward-compatible alias for Factory.get

get_factory_class module-attribute

get_factory_class = Factory.get_class

Backward-compatible alias for Factory.get_class

Classes

Factory

Bases: MagicHint[T]

Base class for magic factories.

Factories build a default value for a type hint. They are registered in a global registry and looked up by type hint. The base factory builds a value by instantiating the hint's fallback concrete type.

A factory class can register itself for one or more type hints directly in its class definition with the register keyword:

Example

class IntFactory(Factory[int], register=int):
    def __call__(self) -> int:
        return 42

Methods:

__call__
__call__() -> T

Build a value for the hint from its fallback type.

register staticmethod
register(factory: Type[Factory], *hints: Unpack[tuple[Any]], registry: FactoryRegistry = ...) -> Type[Factory]
register(*hints: Unpack[tuple[Any]], registry: FactoryRegistry = ...) -> ClassDecorator

Register a factory class for one or more type hints.

Can be used as a decorator or called directly:

Example

@Factory.register(int)
class IntFactory(Factory[int]):
    def __call__(self) -> int:
        return 42

Parameters:

Name Type Description Default
*hints Any

One or more type hints to register the factory class for. Defaults to the class's DEFAULT hint if none are given.

()
registry FactoryRegistry

The registry to register the factory class in. Defaults to the global registry.

FACTORIES
get staticmethod
get(hint: Any, registry: FactoryRegistry = FACTORIES, fallback: Type[Factory] | None = UNSET) -> Factory | None

Get the best-matching factory for a given type hint.

Example

>>> from bagof.factories import get_factory
>>> get_factory(dict[str, int])
DictFactory(dict[str, int])
>>> get_factory(list[int])
SequenceFactory(list[int])

Parameters:

Name Type Description Default
hint Any

The type hint for which to get a factory.

required
registry FactoryRegistry

The registry to look up the factory in. Defaults to the global registry.

FACTORIES
fallback Optional[Type[Factory]]

The fallback factory class to use if no matching factory is found. Defaults to Factory. Pass None explicitly to get None instead of a fallback.

UNSET

Returns:

Type Description
Optional[Factory]

The best-matching factory for the given type hint, or None if no matching factory is found and no fallback is provided.

get_class staticmethod
get_class(hint: Any, registry: FactoryRegistry = FACTORIES, fallback: Type[Factory] | None = UNSET) -> Type[Factory] | None

Get the best-matching factory class for a given type hint.

Warning

By default an unmatched hint returns the base Factory class, never None. Pass fallback=None explicitly to get None back instead.

Parameters:

Name Type Description Default
hint Any

The type hint for which to get a factory class.

required
registry FactoryRegistry

The registry to look up the factory class in. Defaults to the global registry.

FACTORIES
fallback Optional[Type[Factory]]

The fallback factory class to use if no matching factory is found. Defaults to Factory. Pass None explicitly to get None instead of a fallback.

UNSET

Returns:

Type Description
Optional[Type[Factory]]

The best-matching factory class for the given type hint, or None if no matching factory is found and no fallback is provided.