Repository: larq/zookeeper

Issue:
Non-components should raise an error when being configured
### Describe the bug
A subclass of a component that itself does not have an `@component` decorator can be initialized with `configure` without any errors. It will then not act as expected, since it is not a component.

### To Reproduce

```python
@component
class BaseComponent:
    value: int = Field(1)

class SubclassedComponent(BaseComponent):
    value = Field(2)
    
component = SubclassedComponent()
configure(component, {})
print(component.value)  # prints 1
assert component.value == 2  # fails
```

### Expected behavior

During the call to configure an error should be thrown that the component being configured is not a component.

### Environment

Zookeeper version: 1.0b7


Discussion:

Patch:
diff --git a/zookeeper/core/component.py b/zookeeper/core/component.py
index 7944fa6..d5f0f7a 100644
--- a/zookeeper/core/component.py
+++ b/zookeeper/core/component.py
@@ -474,6 +474,13 @@ def configure(
     overwrite any values already set on the instance - either class defaults
     or those set in `__init__`.
     """
+    # Only component instances can be configured.
+    if not utils.is_component_instance(instance):
+        raise TypeError(
+            "Only @component, @factory, and @task instances can be configured. "
+            f"Received: {instance}."
+        )
+
     # Configuration can only happen once.
     if instance.__component_configured__:
         raise ValueError(


Test patch:
diff --git a/zookeeper/core/component_test.py b/zookeeper/core/component_test.py
index 0d188ba..6c4764a 100644
--- a/zookeeper/core/component_test.py
+++ b/zookeeper/core/component_test.py
@@ -523,6 +523,40 @@ def test_component_configure_error_non_existant_key():
         configure(GrandParent(), {"parent.non_existent_field": "bar"})
 
 
+def test_component_configure_error_non_component_instance():
+    class A:
+        a: int = Field()
+
+    with pytest.raises(
+        TypeError,
+        match="Only @component, @factory, and @task instances can be configured.",
+    ):
+        configure(A(), conf={"a": 5})
+
+    @component
+    class B:
+        b: int = Field()
+
+    with pytest.raises(
+        TypeError,
+        match="Only @component, @factory, and @task instances can be configured.",
+    ):
+        # The following we expect to fail because it is a component class, not
+        # an instance.
+        configure(B, conf={"b": 3})
+
+    class C(B):
+        c: int = Field()
+
+    with pytest.raises(
+        TypeError,
+        match="Only @component, @factory, and @task instances can be configured.",
+    ):
+        # Even the an instance of a class that subclasses a component class
+        # should fail.
+        configure(C(), conf={"b": 3, "c": 42})
+
+
 def test_component_configure_field_allow_missing():
     @component
     class A: