fairseq2.dependency¶
This module contains the lightweight dependency injection API of the library. See Design Philosophy to learn more.
ABCs
DependencyContainer
, DependencyResolver
Classes
Protocols
Exceptions
DependencyError
, DependencyNotFoundError
Functions
ABCs¶
- class fairseq2.dependency.DependencyContainer[source]¶
Bases:
DependencyResolver
Holds a dependency graph.
- abstract register(kls, sub_kls=None, key=None)[source]¶
Registers a lazily-initialized object of type
T
.The
__init__()
method ofkls
, or if notNone
,sub_kls
must have type annotations for all its parameters in order for dependency injection to work.If multiple objects of type
T
, optionally with the same key, are registered,resolve()
will return only the last registered one.- Parameters:
- Raises:
ValueError – when
sub_kls
is not a subclass ofkls
.RuntimeError – when called after the container is already used to resolve an object.
- abstract register_factory(kls, factory, key=None)[source]¶
Registers a lazily-initialized object of type
T
.If multiple objects of type
T
, optionally with the same key, are registered,resolve()
will return only the last registered one.- Parameters:
kls (type[T]) – The value of
T
.factory (DependencyFactory[T]) – A callable to create an object of type
T
. If the callable returnsNone
, the object is considered to not exist.key (str | None) – If not
None
, registers the object with the specified key.resolve()
will return the object only if the same key is provided.
- Raises:
RuntimeError – when called after the container is already used to resolve an object.
- abstract register_instance(kls, obj, key=None)[source]¶
Registers an object of type
T
.Other than registering an existing object instead of a factory, the method behaves the same as
register()
.- Parameters:
- Raises:
RuntimeError – when called after the container is already used to resolve an object.
- class fairseq2.dependency.DependencyResolver[source]¶
Bases:
ABC
Resolves objects contained in a dependency graph.
- abstract resolve(kls, key=None)[source]¶
Returns the object of type
T
.- Parameters:
- Raises:
DependencyError – when the dependencies of the object cannot be inferred due to invalid or missing type annotations.
DependencyNotFoundError – when the object or one of its dependencies cannot be found.
- Returns:
The resolved object.
- Return type:
T
- abstract resolve_optional(kls, key=None)[source]¶
Returns the object of type
T
similar toresolve()
, but returnsNone
instead of raising aDependencyNotFoundError
if the object is not found.
- abstract resolve_all(kls)[source]¶
Returns all non-keyed objects of type
T
.If multiple objects of type
T
are registered,resolve()
returns only the last registered one. In contrast,resolve_all
returns them all in the order they were registered.
- abstract resolve_all_keyed(kls)[source]¶
Returns all keyed objects of type
T
.This method behaves similar to
resolve_all()
, but returns an iterable of key-object pairs instead.
Classes¶
- final class fairseq2.dependency.StandardDependencyContainer[source]¶
Bases:
DependencyContainer
This is the standard implementation of
DependencyContainer
and transitively ofDependencyResolver
.- register(kls, sub_kls=None, key=None)[source]¶
Registers a lazily-initialized object of type
T
.The
__init__()
method ofkls
, or if notNone
,sub_kls
must have type annotations for all its parameters in order for dependency injection to work.If multiple objects of type
T
, optionally with the same key, are registered,resolve()
will return only the last registered one.- Parameters:
- Raises:
ValueError – when
sub_kls
is not a subclass ofkls
.RuntimeError – when called after the container is already used to resolve an object.
- register_factory(kls, factory, key=None)[source]¶
Registers a lazily-initialized object of type
T
.If multiple objects of type
T
, optionally with the same key, are registered,resolve()
will return only the last registered one.- Parameters:
kls (type[T]) – The value of
T
.factory (DependencyFactory[T]) – A callable to create an object of type
T
. If the callable returnsNone
, the object is considered to not exist.key (str | None) – If not
None
, registers the object with the specified key.resolve()
will return the object only if the same key is provided.
- Raises:
RuntimeError – when called after the container is already used to resolve an object.
- register_instance(kls, obj, key=None)[source]¶
Registers an object of type
T
.Other than registering an existing object instead of a factory, the method behaves the same as
register()
.- Parameters:
- Raises:
RuntimeError – when called after the container is already used to resolve an object.
- resolve(kls, key=None)[source]¶
Returns the object of type
T
.- Parameters:
- Raises:
DependencyError – when the dependencies of the object cannot be inferred due to invalid or missing type annotations.
DependencyNotFoundError – when the object or one of its dependencies cannot be found.
- Returns:
The resolved object.
- Return type:
T
- resolve_optional(kls, key=None)[source]¶
Returns the object of type
T
similar toresolve()
, but returnsNone
instead of raising aDependencyNotFoundError
if the object is not found.
- resolve_all(kls)[source]¶
Returns all non-keyed objects of type
T
.If multiple objects of type
T
are registered,resolve()
returns only the last registered one. In contrast,resolve_all
returns them all in the order they were registered.
- resolve_all_keyed(kls)[source]¶
Returns all keyed objects of type
T
.This method behaves similar to
resolve_all()
, but returns an iterable of key-object pairs instead.
Protocols¶
- class fairseq2.dependency.DependencyFactory[source]¶
Bases:
Protocol
[T_co
]Used by
DependencyContainer.register()
for itsfactory
parameter.- __call__(resolver)[source]¶
Creates an object of type
T_co
.- Parameters:
resolver (DependencyResolver) – The dependency resolver that the callable can use to resolve the dependencies of the newly-created object.
- Returns:
An object of type
T_co
that will be registered in the callingDependencyContainer
, orNone
if the object cannot be created.- Return type:
T_co | None
Exceptions¶
- class fairseq2.dependency.DependencyError[source]¶
Bases:
RuntimeError
Raised when an error occurs while resolving a dependency.
- class fairseq2.dependency.DependencyNotFoundError[source]¶
Bases:
DependencyError
Raised when a dependency cannot be found.
Functions¶
- fairseq2.dependency.get_container()[source]¶
Returns the global
DependencyContainer
instance.- Raises:
RuntimeError – when
setup_fairseq2()
has not been called first which is responsible for initializing the global container.- Return type: