diff --git a/monkey/common/di_container.py b/monkey/common/di_container.py index 6086440e7..8559e8036 100644 --- a/monkey/common/di_container.py +++ b/monkey/common/di_container.py @@ -29,7 +29,12 @@ class DIContainer: 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." + "register(), or use register_instance() instead" + ) + + if not issubclass(concrete_type, interface): + raise TypeError( + f'Class "{concrete_type.__name__}" is not a subclass of {interface.__name__}' ) self._type_registry[interface] = concrete_type @@ -42,6 +47,12 @@ class DIContainer: :param interface: An interface or abstract base class that other classes depend upon :param instance: An instance (object) of a type that implements `interface` """ + if not isinstance(instance, interface): + raise TypeError( + f'The provided instance of type "{instance.__class__.__name__}" ' + f"is not an instance of {interface.__name__}" + ) + self._instance_registry[interface] = instance DIContainer._del_key(self._type_registry, interface) diff --git a/monkey/tests/unit_tests/common/test_di_container.py b/monkey/tests/unit_tests/common/test_di_container.py index b0109b11d..f7ea5534c 100644 --- a/monkey/tests/unit_tests/common/test_di_container.py +++ b/monkey/tests/unit_tests/common/test_di_container.py @@ -270,3 +270,14 @@ def test_register_instance_as_type(container): service_a_instance = ServiceA() with pytest.raises(TypeError): container.register(IServiceA, service_a_instance) + + +def test_register_conflicting_type(container): + with pytest.raises(TypeError): + container.register(IServiceA, ServiceB) + + +def test_register_instance_with_conflicting_type(container): + service_b_instance = ServiceB() + with pytest.raises(TypeError): + container.register_instance(IServiceA, service_b_instance)