Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error message when loading class that is not a Step #105

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion nodestream/pipeline/class_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from importlib import import_module
from typing import Optional, Type

DECLARATIVE_INIT_METHOD_NAME = "from_file_data"

Expand Down Expand Up @@ -43,6 +44,9 @@ def find_class(class_path):
class ClassLoader:
"""Loads a class from a string path and instantiates it with the given arguments."""

def __init__(self, class_constratint: Optional[Type] = None) -> None:
self.class_constratint = class_constratint or object

def find_class_initializer(self, implementation, factory=None):
class_definition = find_class(implementation)
factory_method = factory or DECLARATIVE_INIT_METHOD_NAME
Expand All @@ -55,6 +59,12 @@ def load_class(self, implementation, arguments=None, factory=None):
arguments = arguments or {}
initializer = self.find_class_initializer(implementation, factory)
try:
return initializer(**arguments)
result = initializer(**arguments)
except TypeError as e:
raise PipelineComponentInitializationError(initializer, arguments) from e

if not isinstance(result, self.class_constratint):
raise TypeError(
f"Expected class of type {self.class_constratint}, but got {type(result)}."
)
return result
3 changes: 2 additions & 1 deletion nodestream/pipeline/pipeline_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .normalizers import Normalizer
from .pipeline import Pipeline
from .scope_config import ScopeConfig
from .step import Step
from .value_providers import ValueProvider


Expand Down Expand Up @@ -70,7 +71,7 @@ def for_testing(cls):

def initialize_from_file_data(self, file_data: List[dict]):
return Pipeline(
steps=self.load_steps(ClassLoader(), file_data),
steps=self.load_steps(ClassLoader(Step), file_data),
step_outbox_size=self.step_outbox_size,
)

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/pipeline/test_class_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ def test_class_loader_invalid_path_invalid_arugment(subject):
implementation="tests.unit.pipeline.test_class_loader:SimpleClass",
arguments={"not_a_valid_argument": True},
)


def test_class_loader_invalid_type_constraint(subject):
with pytest.raises(TypeError):
subject.class_constratint = SimpleClassWithFactories
subject.load_class(
implementation="tests.unit.pipeline.test_class_loader:SimpleClass",
arguments={"argument": "test"},
)


def test_class_loader_valid_subclass(subject):
subject.class_constratint = SimpleClass
subject.load_class(
implementation="tests.unit.pipeline.test_class_loader:SimpleClassWithFactories",
arguments={"argument": "test"},
factory="another_factory",
)
Loading