Provides a TidiRegistry
, responsible for providing stored dependencies.
RegistrationError
Bases: TypeError
Error finding desired type in registry
Source code in src/tidi/registry.py
| class RegistrationError(TypeError):
"""Error finding desired type in registry"""
|
RegistryLookupError
Bases: LookupError
Error finding desired type in registry
Source code in src/tidi/registry.py
| class RegistryLookupError(LookupError):
"""Error finding desired type in registry"""
|
TidiRegistry
A simple registry of objects indexed by their type.
Source code in src/tidi/registry.py
| class TidiRegistry:
"""A simple registry of objects indexed by their type."""
def __init__(
self,
banned_types: list[t.Type] | None = None,
container_cls: t.Type[_Container] = _PerThreadContainer,
):
if banned_types is None:
banned_types = [
type_ for type_ in builtins.__dict__.values() if isinstance(type_, type)
]
self.banned_types = banned_types
self._container = container_cls()
def register(self, obj: T, type_: t.Type[T] | None = None):
"""Register an instance `obj` of class `T` to be available for injection.
Args:
obj (typing.Any): The instance to register
Raises:
RegistrationError: if trying to register a banned type (a builtin type by default).
"""
if type_ is None:
type_ = type(obj)
if type_ in self.banned_types:
raise RegistrationError(f"Trying to register a banned type: {type_}")
self._container.add(obj, type_)
def get(self, type_: t.Type[T], default: t.Any = _unknown) -> T:
"""Get an instance of type `type_` from the regsitry.
Args:
type_ (t.Type[T]): The type of the dependency being looked for.
default (t.Any, optional): An optional default return value.
Raises:
RegistryLookupError: if the instance hasn't been registered and a
default hasn't been provided.
Returns:
(type_ (T)): the registered object, or default value if it was provided.
"""
obj = self._container.get(type_, default)
if isinstance(obj, _Unknown):
raise RegistryLookupError(f"Type has not been registered: {type_}")
return obj
|
get(type_, default=_unknown)
Get an instance of type type_
from the regsitry.
Parameters:
Name |
Type |
Description |
Default |
type_ |
Type[T]
|
The type of the dependency being looked for.
|
required
|
default |
Any
|
An optional default return value.
|
_unknown
|
Raises:
Type |
Description |
RegistryLookupError
|
if the instance hasn't been registered and a
default hasn't been provided.
|
Returns:
Type |
Description |
type_(T)
|
the registered object, or default value if it was provided.
|
Source code in src/tidi/registry.py
| def get(self, type_: t.Type[T], default: t.Any = _unknown) -> T:
"""Get an instance of type `type_` from the regsitry.
Args:
type_ (t.Type[T]): The type of the dependency being looked for.
default (t.Any, optional): An optional default return value.
Raises:
RegistryLookupError: if the instance hasn't been registered and a
default hasn't been provided.
Returns:
(type_ (T)): the registered object, or default value if it was provided.
"""
obj = self._container.get(type_, default)
if isinstance(obj, _Unknown):
raise RegistryLookupError(f"Type has not been registered: {type_}")
return obj
|
register(obj, type_=None)
Register an instance obj
of class T
to be available for injection.
Parameters:
Name |
Type |
Description |
Default |
obj |
Any
|
|
required
|
Raises:
Type |
Description |
RegistrationError
|
if trying to register a banned type (a builtin type by default).
|
Source code in src/tidi/registry.py
| def register(self, obj: T, type_: t.Type[T] | None = None):
"""Register an instance `obj` of class `T` to be available for injection.
Args:
obj (typing.Any): The instance to register
Raises:
RegistrationError: if trying to register a banned type (a builtin type by default).
"""
if type_ is None:
type_ = type(obj)
if type_ in self.banned_types:
raise RegistrationError(f"Trying to register a banned type: {type_}")
self._container.add(obj, type_)
|