From d4870db91f19d8a9f7ef87e47dfb281ed76b493b Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Tue, 29 Oct 2024 14:05:42 -0400 Subject: [PATCH] Adding error messages for packed config dicts --- .../base/generic_property.py | 3 ++- .../base/generic_reaction.py | 7 ++++++ .../base/tests/test_generic_property.py | 25 +++++++++++++++++-- .../base/tests/test_generic_reaction.py | 11 ++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/idaes/models/properties/modular_properties/base/generic_property.py b/idaes/models/properties/modular_properties/base/generic_property.py index 26a31b3d57..2dcb428bea 100644 --- a/idaes/models/properties/modular_properties/base/generic_property.py +++ b/idaes/models/properties/modular_properties/base/generic_property.py @@ -304,7 +304,8 @@ def build(self): # Add Phase objects if self.config.phases is None: raise ConfigurationError( - "{} was not provided with a phases argument.".format(self.name) + f"{self.name} was not provided with a phases argument. " + "Did you forget to unpack the configurations dictionary?" ) # Add a flag indicating whether this is an electrolyte system or not diff --git a/idaes/models/properties/modular_properties/base/generic_reaction.py b/idaes/models/properties/modular_properties/base/generic_reaction.py index 531a7bd06d..6ad5644c3a 100644 --- a/idaes/models/properties/modular_properties/base/generic_reaction.py +++ b/idaes/models/properties/modular_properties/base/generic_reaction.py @@ -199,6 +199,13 @@ def build(self): # and cannot be set until the config block is created by super.build super(ReactionParameterBlock, self).build() + # Check to make sure a property block was assigned + if self.config.property_package is None: + raise ConfigurationError( + f"{self.name} was not assigned a property package. " + "Did you forget to unpack the configuration dictionary?" + ) + # Set base units of measurement self.get_metadata().add_default_units(self.config.base_units) diff --git a/idaes/models/properties/modular_properties/base/tests/test_generic_property.py b/idaes/models/properties/modular_properties/base/tests/test_generic_property.py index 716c824443..39a8e84e13 100644 --- a/idaes/models/properties/modular_properties/base/tests/test_generic_property.py +++ b/idaes/models/properties/modular_properties/base/tests/test_generic_property.py @@ -199,7 +199,7 @@ def test_no_components(self): with pytest.raises( ConfigurationError, - match="params was not provided with a components " "argument.", + match="params was not provided with a components argument.", ): m.params = DummyParameterBlock( phases={ @@ -215,12 +215,33 @@ def test_no_phases(self): with pytest.raises( ConfigurationError, - match="params was not provided with a phases " "argument.", + match="params was not provided with a phases argument. " + "Did you forget to unpack the configurations dictionary?", ): m.params = DummyParameterBlock( components={"a": {}, "b": {}, "c": {}}, base_units=base_units ) + @pytest.mark.unit + def test_packed_dict(self): + m = ConcreteModel() + + dummy_dict = { + "phases": { + "p1": {"equation_of_state": "foo"}, + "p2": {"equation_of_state": "bar"}, + }, + } + + with pytest.raises( + ConfigurationError, + match=re.escape( + "params[phases] was not provided with a phases argument. " + "Did you forget to unpack the configurations dictionary?" + ), + ): + m.params = DummyParameterBlock(dummy_dict) + @pytest.mark.unit def test_invalid_component_in_phase_component_list(self): m = ConcreteModel() diff --git a/idaes/models/properties/modular_properties/base/tests/test_generic_reaction.py b/idaes/models/properties/modular_properties/base/tests/test_generic_reaction.py index 39f81c1921..722837906d 100644 --- a/idaes/models/properties/modular_properties/base/tests/test_generic_reaction.py +++ b/idaes/models/properties/modular_properties/base/tests/test_generic_reaction.py @@ -193,6 +193,17 @@ def test_rate_build_no_stoichiometry(self, m): rate_reactions={"r1": {"heat_of_reaction": "foo", "rate_form": "foo"}}, ) + @pytest.mark.unit + def test_packed_config_dict(self, m): + with pytest.raises( + ConfigurationError, + match=re.escape( + "rxn_params[property_package] was not assigned a property package. " + "Did you forget to unpack the configuration dictionary?" + ), + ): + m.rxn_params = GenericReactionParameterBlock({"property_package": m.params}) + @pytest.mark.unit def test_rate_build_invalid_phase_stoichiometry(self, m): with pytest.raises(