refactor: raise PipelineError when Pipeline.from_dict receives an invalid type (#8711)

* fix: error on invalid type

* add reno

* Update releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml

Co-authored-by: Stefano Fiorucci <stefanofiorucci@gmail.com>

* Update test/core/pipeline/test_pipeline.py

Co-authored-by: Stefano Fiorucci <stefanofiorucci@gmail.com>

* fix reno

* fix reno

* last reno fix

---------

Co-authored-by: Stefano Fiorucci <stefanofiorucci@gmail.com>
This commit is contained in:
tstadel 2025-01-23 12:40:19 +01:00 committed by GitHub
parent bf79f04932
commit 3119ae1ec9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 3 deletions

View File

@ -167,8 +167,10 @@ class PipelineBase:
f"Successfully imported module {module} but can't find it in the component registry."
"This is unexpected and most likely a bug."
)
except (ImportError, PipelineError) as e:
raise PipelineError(f"Component '{component_data['type']}' not imported.") from e
except (ImportError, PipelineError, ValueError) as e:
raise PipelineError(
f"Component '{component_data['type']}' (name: '{name}') not imported."
) from e
# Create a new one
component_class = component.registry[component_data["type"]]

View File

@ -0,0 +1,5 @@
---
enhancements:
- |
When `Pipeline.from_dict` receives an invalid type (e.g. empty string), an informative `PipelineError` is now
raised.

View File

@ -564,7 +564,7 @@ class TestPipeline:
err.match("Missing 'type' in component 'add_two'")
# UNIT
def test_from_dict_without_registered_component_type(self, request):
def test_from_dict_without_registered_component_type(self):
data = {
"metadata": {"test": "test"},
"components": {"add_two": {"type": "foo.bar.baz", "init_parameters": {"add": 2}}},
@ -575,6 +575,17 @@ class TestPipeline:
err.match(r"Component .+ not imported.")
def test_from_dict_with_invalid_type(self):
data = {
"metadata": {"test": "test"},
"components": {"add_two": {"type": "", "init_parameters": {"add": 2}}},
"connections": [],
}
with pytest.raises(PipelineError) as err:
Pipeline.from_dict(data)
err.match(r"Component '' \(name: 'add_two'\) not imported.")
# UNIT
def test_from_dict_without_connection_sender(self):
data = {"metadata": {"test": "test"}, "components": {}, "connections": [{"receiver": "some.receiver"}]}