Skip to content

Commit

Permalink
Disallow configuring non-component instances. Fixes #130. (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamHillier authored Mar 18, 2020
1 parent e41661a commit 899d23b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions zookeeper/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions zookeeper/core/component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,40 @@ class GrandParent:
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:
Expand Down

0 comments on commit 899d23b

Please sign in to comment.