forked from p15670423/monkey
Common: Raise TypeError if DIContainer.register() called with instance
This commit is contained in:
parent
9b52f3f21b
commit
8c3477a000
|
@ -25,6 +25,13 @@ class DIContainer:
|
||||||
:param interface: An interface or abstract base class that other classes depend upon
|
:param interface: An interface or abstract base class that other classes depend upon
|
||||||
:param concrete_type: A type (class) that implements `interface`
|
:param concrete_type: A type (class) that implements `interface`
|
||||||
"""
|
"""
|
||||||
|
if not inspect.isclass(concrete_type):
|
||||||
|
raise TypeError(
|
||||||
|
"Expected a class, but received an instance of type "
|
||||||
|
f'"{concrete_type.__class__.__name__}"; Pass a class, not an instance, to '
|
||||||
|
"register(), or use register_instance() instead."
|
||||||
|
)
|
||||||
|
|
||||||
self._type_registry[interface] = concrete_type
|
self._type_registry[interface] = concrete_type
|
||||||
DIContainer._del_key(self._instance_registry, interface)
|
DIContainer._del_key(self._instance_registry, interface)
|
||||||
|
|
||||||
|
|
|
@ -260,3 +260,9 @@ def test_resolve_dependencies(container):
|
||||||
|
|
||||||
assert isinstance(dependencies[0], ServiceA)
|
assert isinstance(dependencies[0], ServiceA)
|
||||||
assert isinstance(dependencies[1], ServiceB)
|
assert isinstance(dependencies[1], ServiceB)
|
||||||
|
|
||||||
|
|
||||||
|
def test_register_instance_as_type(container):
|
||||||
|
service_a_instance = ServiceA()
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
container.register(IServiceA, service_a_instance)
|
||||||
|
|
Loading…
Reference in New Issue