Skip to content

Abstract factory

objectory.abstract_factory

Implement the AbstractFactory metaclass used to create abstract factories.

This module provides an abstract factory implementation that allows automatic registration and instantiation of classes and functions.

objectory.abstract_factory.AbstractFactory

Bases: ABCMeta

Implement the abstract factory metaclass to create factories automatically.

Please read the documentation about this abstract factory to learn how it works and how to use it.

To avoid potential conflicts with the other classes, all the non-public attributes or functions starts with _abstractfactory_**** where **** is the name of the attribute or the function.

Parameters:

Name Type Description Default
name str

The class name. This becomes the __name__ attribute of the class.

required
bases tuple[type, ...]

A tuple of the base classes from which the class inherits. This becomes the __bases__ attribute of the class.

required
dct dict[str, Any]

A namespace dictionary containing definitions for the class body. This becomes the __dict__ attribute of the class.

required

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> obj = BaseClass.factory("MyClass")
>>> obj
<....MyClass object at 0x...>

objectory.abstract_factory.AbstractFactory.inheritors property

inheritors: dict[str, Any]

Get the inheritors.

Returns:

Type Description
dict[str, Any]

The inheritors.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> BaseClass.inheritors
{'....BaseClass': <class '....BaseClass'>, '....MyClass': <class '....MyClass'>}

objectory.abstract_factory.AbstractFactory.factory

factory(
    _target_: str,
    *args: Any,
    _init_: str = "__init__",
    **kwargs: Any
) -> Any

Instantiate dynamically an object given its configuration.

This method creates an instance of a registered class or calls a registered function. The target can be specified using either the short name (e.g., "MyClass") or the fully qualified name (e.g., "mymodule.MyClass"). If the target is not yet registered, it will attempt to import and register it automatically.

Parameters:

Name Type Description Default
_target_ str

The name of the object (class or function) to instantiate. It can be the class name or the full class name. Supports name resolution for registered objects.

required
*args Any

Positional arguments to pass to the class constructor or function.

()
_init_ str

The function or method to use to create the object. If "__init__" (default), the object is created by calling the constructor. Can also be "__new__" or the name of a class method.

'__init__'
**kwargs Any

Keyword arguments to pass to the class constructor or function.

{}

Returns:

Type Description
Any

The instantiated object with the given parameters.

Raises:

Type Description
AbstractClassAbstractFactoryError

when an abstract class is instantiated.

UnregisteredClassAbstractFactoryError

when the target is not found.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> obj = BaseClass.factory("MyClass")
>>> obj
<....MyClass object at 0x...>

objectory.abstract_factory.AbstractFactory.register_object

register_object(obj: type | Callable) -> None

Register a class or function to the factory.

This method manually registers a class or function with the factory, making it available for instantiation. This is particularly useful when working with third-party libraries where you cannot modify the source code to inherit from the factory. The object is registered using its fully qualified name. If an object with the same name already exists, it will be replaced with a warning.

Parameters:

Name Type Description Default
obj type | Callable

The class or function to register to the factory. Must be a valid class or function object (not a lambda function).

required

Raises:

Type Description
IncorrectObjectAbstractFactoryError

if the object is not a class or function, or if it is a lambda function.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass:
...     pass
...
>>> BaseClass.register_object(MyClass)
>>> BaseClass.inheritors
{...}

objectory.abstract_factory.AbstractFactory.unregister

unregister(name: str) -> None

Remove a registered object from the factory.

This method removes a class or function from the factory's registry. The object will no longer be available for instantiation through the factory. This is an experimental function and may change in the future.

Parameters:

Name Type Description Default
name str

The name of the object to remove. Can be either the short name (e.g., "MyClass") or the fully qualified name (e.g., "mymodule.MyClass"). This function uses the name resolution mechanism to find the full name if only the short name is given.

required

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass:
...     pass
...
>>> BaseClass.register_object(MyClass)
>>> BaseClass.unregister("MyClass")
>>> BaseClass.inheritors
{'....BaseClass': <class '....BaseClass'>}

objectory.abstract_factory.is_abstract_factory

is_abstract_factory(cls: Any) -> bool

Indicate if a class implements the AbstractFactory metaclass.

Parameters:

Name Type Description Default
cls Any

The class to check.

required

Returns:

Type Description
bool

True if the class implements the AbstractFactory metaclass, otherwise False.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, is_abstract_factory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> is_abstract_factory(BaseClass)
True
>>> is_abstract_factory(int)
False

objectory.abstract_factory.register

register(cls: AbstractFactory) -> Callable

Define a decorator to register a function to a factory.

This decorator is designed to register functions that return an object of a class registered in the factory.

Parameters:

Name Type Description Default
cls AbstractFactory

The class where to register the function. Must be a class that uses the AbstractFactory metaclass.

required

Returns:

Type Description
Callable

The decorated function.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, register
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> @register(BaseClass)
... def function_to_register(value: int) -> int:
...     return value + 2
...
>>> BaseClass.factory("function_to_register", 40)
42

objectory.abstract_factory.register_child_classes

register_child_classes(
    factory_cls: AbstractFactory | type,
    cls: type,
    ignore_abstract_class: bool = True,
) -> None

Register the given class and its child classes.

This function registers all the child classes including the child classes of the child classes, etc.

Parameters:

Name Type Description Default
factory_cls AbstractFactory | type

The factory class. The child classes will be registered to this class.

required
cls type

The class to register along with its child classes.

required
ignore_abstract_class bool

Indicate if the abstract classes should be ignored or not. By default, the abstract classes are not registered because they cannot be instantiated.

True

Raises:

Type Description
AbstractFactoryTypeError

if the factory class does not implement the AbstractFactory metaclass.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, register_child_classes
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> register_child_classes(BaseClass, dict)