From 6f3b839518a209e58b57925570904a028606a50d Mon Sep 17 00:00:00 2001 From: Martin Lange <martin.lange@ufz.de> Date: Sun, 30 Oct 2022 00:13:06 +0200 Subject: [PATCH] add a check for rule types --- src/finam/sdk/component.py | 2 +- src/finam/tools/connect_helper.py | 21 ++++++++++++++------- tests/tools/test_connect.py | 8 ++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/finam/sdk/component.py b/src/finam/sdk/component.py index 87dd09bb..8106f9ef 100644 --- a/src/finam/sdk/component.py +++ b/src/finam/sdk/component.py @@ -281,7 +281,7 @@ class Component(IComponent, Loggable, ABC): } ) - The :class:`.Info` object for output ``Out`` will be crated and pushed automatically in :meth:`.try_connect` + The :class:`.Info` object for output ``Out`` will be created and pushed automatically in :meth:`.try_connect` as soon as the metadata for ``In`` becomes available. Here, the metadata of an output is composed from the metadata of two inputs and a user-defined value: diff --git a/src/finam/tools/connect_helper.py b/src/finam/tools/connect_helper.py index fe29b8bb..05693523 100644 --- a/src/finam/tools/connect_helper.py +++ b/src/finam/tools/connect_helper.py @@ -174,13 +174,20 @@ class ConnectHelper(Loggable): self._check_rule(rule) def _check_rule(self, rule): - if isinstance(rule, FromInput) and rule.name not in self._inputs: - raise KeyError( - f"No input named '{rule.name}' to use in info transfer rule." - ) - if isinstance(rule, FromOutput) and rule.name not in self._outputs: - raise KeyError( - f"No output named '{rule.name}' to use in info transfer rule." + if isinstance(rule, FromInput): + if rule.name not in self._inputs: + raise KeyError( + f"No input named '{rule.name}' to use in info transfer rule." + ) + elif isinstance(rule, FromOutput): + if rule.name not in self._outputs: + raise KeyError( + f"No output named '{rule.name}' to use in info transfer rule." + ) + elif not isinstance(rule, FromValue): + raise TypeError( + f"Rules must be one of the types FromInput, FromOutput or FromValue. " + f"Got '{rule.__class__.__name__}'." ) @property diff --git a/tests/tools/test_connect.py b/tests/tools/test_connect.py index 2b0f869d..91d6b752 100644 --- a/tests/tools/test_connect.py +++ b/tests/tools/test_connect.py @@ -358,3 +358,11 @@ class TestConnectHelper(unittest.TestCase): outputs, in_info_rules={"In1": [FromOutput("OutX", "grid")]}, ) + + with self.assertRaises(TypeError): + _connector: ConnectHelper = ConnectHelper( + "TestLogger", + inputs, + outputs, + in_info_rules={"In1": [0]}, + ) -- GitLab