Add _store_name field to StoreAwareMixin to ease serialisation (#5531)

This commit is contained in:
Silvano Cerza 2023-08-10 15:42:19 +02:00 committed by GitHub
parent 4bb22c9665
commit 168b7c806c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -13,6 +13,10 @@ class StoreAwareMixin:
"""
_store: Optional[Store] = None
# This is necessary to ease serialisation when converting a Component that uses
# a Store into a dictionary.
# This is only set when calling `Pipeline.add_component()`.
_store_name: str = ""
supported_stores: List[Type[Store]] # type: ignore # (see https://github.com/python/mypy/issues/4717)
@property

View File

@ -99,6 +99,7 @@ class Pipeline(CanalsPipeline):
raise ValueError("Reusing components with stores is not supported (yet). Create a separate instance.")
instance.store = self._stores[store]
instance._store_name = store
elif store:
raise ValueError(f"Component '{name}' doesn't support stores.")

View File

@ -102,6 +102,7 @@ def test_add_component_store_aware_component_receives_one_docstore():
pipe.add_store(name="second_store", store=store_2)
pipe.add_component("component", mock, store="first_store")
assert mock.store == store_1
assert mock._store_name == "first_store"
assert pipe.run(data={"component": {"value": 1}}) == {"component": {"value": 1}}
@ -188,6 +189,7 @@ def test_add_component_store_aware_component_receives_correct_docstore_type():
pipe.add_component("component", mock, store="second_store")
assert mock.store == store_2
assert mock._store_name == "second_store"
@pytest.mark.unit
@ -217,6 +219,7 @@ def test_add_component_store_aware_component_is_reused():
pipe.add_component("component2", mock, store="first_store")
assert mock.store == store_2
assert mock._store_name == "second_store"
@pytest.mark.unit
@ -243,8 +246,9 @@ def test_add_component_store_aware_component_receives_subclass_of_correct_docsto
pipe.add_component("component", mock, store="first_store")
assert mock.store == store_1
assert mock._store_name == "first_store"
pipe.add_component("component2", mock2, store="second_store")
assert mock2.store == store_2
assert mock2._store_name == "second_store"
@pytest.mark.unit