Skip to content

Factory

uzi.providers.Factory

Bases: Provider[abc.Callable[..., T_Injected], nodes._T_FactoryNode], t.Generic[T_Injected, nodes._T_FactoryNode]

Resolves to the return value of the given factory. A factory can be a type, function or a Callable object.

The factory is called every time a dependency for this provider is requested.

Attributes:

Name Type Description
concrete Union[type[T_Injected], abc.Callable[..., T_Injected]]

the factory to used to create the provided value.

arguments tuple[tuple.frozendict]

A tuple of positional and keyword arguments passed to the factory.

Parameters:

Name Type Description Default
concrete Union[type[T_Injected], abc.Callable[..., T_Injected]]

the factory. Can be a type, function or a Callable object.

None
*args Union[Dep, Any]

Positional arguments to pass to the factory.

()
**kwargs Union[Dep, Any]

Keyword arguments to pass to the factory.

{}

With Arguments

Positional and/or keyword arguments to pass to the factory may be provided.

Values Only
1
2
Factory(func, 'a', 32, obj, key='xyz')
# will call: func('a', 32, obj, key='xyz')
Values and Dependencies

Arguments of type DependencyMarker will automatically be resolved and passed to the factory while calling it.

e.g. using Dep and Lookup:

1
2
3
4
5
6
7
8
9
Factory(
    func,
    'a',
    uzi.makers.Dep(Foo),
    obj,
    key='xyz',
    bar=uzi.makers.Lookup(FooBar).bar
)
# will call: func('a', <inject: Foo>, obj, key='xyz', bar=<inject: FooBar>.bar)

asynchronous(is_async: bool = True) -> Self

Mark/Unmark this provider as asynchronous. Updates is_async attribute.

Normally, coroutines and factories with async dependencies automatically detected as asynchronous. This method provides the ability to change this default behaviour.

Parameters:

Name Type Description Default
is_async Union[bool, None]

True to mark, False to unmark or None to revert to the default behaviour. Defaults to True.

True

Returns:

Name Type Description
self Provider

this provider

args(*args) -> Self

Set the positional arguments to pass to the factory.

Updates the arguments attribute.

Parameters:

Name Type Description Default
*args Union[Dep, Any]

Positional arguments to pass to the factory.

()

Returns:

Name Type Description
self Provider

this provider

kwargs(**kwargs) -> Self

Set the keyword arguments to pass to the factory.

Updates the arguments attribute.

Parameters:

Name Type Description Default
**kwargs Union[Dep, Any]

Keyword arguments to pass to the factory.

{}

Returns:

Name Type Description
self Provider

this provider

use(concrete, *args, **kwargs)

Sets the provider's factory and arguments.

Parameters:

Name Type Description Default
concrete Union[type[T_Injected], abc.Callable[..., T_Injected]]

the factory. Can be a type, function or a Callable object.

required
*args Union[Dep, Any]

Positional arguments to pass to the factory.

()
**kwargs Union[Dep, Any]

Keyword arguments to pass to the factory.

{}

Returns:

Name Type Description
self Factory

this provider

signature(signature: Signature) -> Self

Set a custom Signature for the factory.

Parameters:

Name Type Description Default
signature Signature

the signature

required

Returns:

Name Type Description
self Factory

this provider

Back to top