Provider¶
uzi.providers.Provider
¶
Bases: t.Generic[_T_Concrete, _T_Node]
The base class for all providers.
Subclasses can implement the resolve()
method to return the appropriate
Dependency
object for any given dependency.
Attributes:
Name | Type | Description |
---|---|---|
concrete |
Any
|
The object used to resolve |
container |
Container
|
The Container where this provider is defined. |
access_modifier |
t.Optional[AccessModifier]
|
The minimum access modifier required to access provider |
is_default |
bool
|
Whether this provider is the default. A default provider only gets used if none other was provided to override it. |
is_final |
bool
|
Whether this provider is final. A final provider will error if overridden by containers further down the provider resolution order. |
is_async |
bool
|
Whether this provider is asyncnous |
filters |
tuple[Callable]
|
Called to determine whether this provider can be resolved. |
default(is_default: bool = True) -> Self
¶
Mark/Unmark this provider as the default. Updates the provider's
is_default
attribute.
A default provider will be skipped if the dependency they provide has another provider. This means that a default provider will only get used if no other providers for the given dependency were defined in the current scope.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_default |
bool
|
|
True
|
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
final(is_final: bool = True) -> Self
¶
Mark/Unmark this provider as final. Updates is_final
attribute.
A final provider will error if overridden by containers further down the provider resolution order. This does not apply to providers marked as default. Default providers will get skipped silently.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_final |
bool
|
|
True
|
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
private() -> Self
¶
Set the access_modifier
for this provider to AccessModifier.private
.
A private provider is only avaliable to dependants declared in the same container.
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
guarded() -> Self
¶
Set the access_modifier
for this provider to AccessModifier.guarded
.
A guarded provider is only visible to dependants declared in the provider's container and it's bases.
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
protected() -> Self
¶
Set the access_modifier
for this provider to AccessModifier.protected
.
A protected provider is only visible to dependants declared in containers within the inheritance heirachy of the provider's container. This includes both base and derived containers.
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
public() -> Self
¶
Set the access_modifier
for this provider to AccessModifier.public
.
A public provider is visible to all dependants within it's scope.
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
_can_resolve(dep: 'DepKey', scope: 'Graph') -> bool
¶
Check whether this provider is avaliable to the given dep.
Unlike _resolve
, this method will be called on all matching dependencies.
If available, the provider's filters
will be applied and the result is
returned.
Returns True
if no filters are available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dep |
DepKey
|
description |
required |
scope |
Scope
|
description |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
_resolve(abstract: T_Injectable, scope: 'Graph') -> _T_Node
¶
Resolves the given dependency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
abstract |
T_Injectable
|
dependency to be resolved |
required |
scope |
Scope
|
Scope within which the dependency is getting resolved. |
required |
Returns:
Name | Type | Description |
---|---|---|
node |
Node
|
_setup(container: 'Container', abstract: T_Injectable = None) -> Self
¶
Called when the provider is added to a container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container |
Container
|
the container |
required |
abstract |
T_Injectable
|
the bound dependency |
None
|
Raises:
Type | Description |
---|---|
AttributeError
|
When another container was already set |
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
use(using: _T_Concrete) -> Self
¶
Set the provider's concrete
attribute.
The given value will depend on the type of provider
Can be used as decorator
@provider.use()
def func():
... # pragma: no cover
Parameters:
Name | Type | Description | Default |
---|---|---|---|
using |
_T_Concrete
|
the object to provide. this depends on the type of provider |
required |
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |
when(*filters: abc.Callable, replace: bool = False) -> Self
¶
Add/replace the provider's filters
.
Filters are callables that determine whether this provider can provide a given dependency.
Filters are called with 4 arguments:- provider
- this provide,
abstract
- the dependency to be provided, scope
- scope within which
the dependency is getting resolved and dependant
the Container
from
which the dependency was requested.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*filters |
Callable
|
The filter callables. |
()
|
replace |
bool
|
Whether to replace the existing filters instead of adding. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
self |
Provider
|
this provider |