From 19b3daa3a63abda992bfc75938f9cf79b92f333d Mon Sep 17 00:00:00 2001 From: spaulins-usgs Date: Thu, 22 Dec 2022 13:02:49 -0800 Subject: [PATCH] fix(MFPackage kwargs check): Now verifying that only valid kwargs are passed to MFPackage (#1667) * fix(MFPackage kwargs check): Now verifying only valid kwargs are passed to MFPackage. * fix(MFPackage kwargs): Accept possible kwargs from child classes as valid. * fix(MFPackage kwargs) * fix(MFPackage kwargs): Added test to verify the kwargs check is working --- autotest/test_mf6.py | 17 +++++++++++++++++ flopy/mf6/mfpackage.py | 24 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/autotest/test_mf6.py b/autotest/test_mf6.py index e1ad3ce3fe..3c3f0dbc18 100644 --- a/autotest/test_mf6.py +++ b/autotest/test_mf6.py @@ -1579,6 +1579,23 @@ def test_multi_model(function_tmpdir): sim.write_simulation() sim.run_simulation() + with pytest.raises( + flopy.mf6.mfbase.FlopyException, + match='Extraneous kwargs "param_does_not_exist" ' + "provided to MFPackage.", + ): + # test kwargs error checking + wel = ModflowGwfwel( + gwf2, + print_input=True, + print_flows=True, + stress_period_data=welspd, + save_flows=False, + auxiliary="CONCENTRATION", + pname="WEL-1", + param_does_not_exist=True, + ) + @requires_exe("mf6") def test_namefile_creation(tmpdir): diff --git a/flopy/mf6/mfpackage.py b/flopy/mf6/mfpackage.py index f7bf5ad1f0..6bfb770faa 100644 --- a/flopy/mf6/mfpackage.py +++ b/flopy/mf6/mfpackage.py @@ -1585,16 +1585,18 @@ def __init__( loading_package=False, **kwargs, ): + parent_file = kwargs.pop("parent_file", None) if isinstance(parent, MFPackage): self.model_or_sim = parent.model_or_sim self.parent_file = parent - elif "parent_file" in kwargs: + elif parent_file is not None: self.model_or_sim = parent - self.parent_file = kwargs["parent_file"] + self.parent_file = parent_file else: self.model_or_sim = parent self.parent_file = None - if "_internal_package" in kwargs and kwargs["_internal_package"]: + _internal_package = kwargs.pop("_internal_package", False) + if _internal_package: self.internal_package = True else: self.internal_package = False @@ -1724,15 +1726,29 @@ def __init__( self.bc_color = "black" self.__inattr = False self._child_package_groups = {} + child_builder_call = kwargs.pop("child_builder_call", None) if ( self.parent_file is not None - and "child_builder_call" not in kwargs + and child_builder_call is None and package_type in self.parent_file._child_package_groups ): # initialize as part of the parent's child package group chld_pkg_grp = self.parent_file._child_package_groups[package_type] chld_pkg_grp.init_package(self, self._filename) + # remove any remaining valid kwargs + key_list = list(kwargs.keys()) + for key in key_list: + if "filerecord" in key and hasattr(self, f"{key}"): + kwargs.pop(f"{key}") + # check for extraneous kwargs + if len(kwargs) > 0: + kwargs_str = ", ".join(kwargs.keys()) + excpt_str = ( + f'Extraneous kwargs "{kwargs_str}" provided to MFPackage.' + ) + raise FlopyException(excpt_str) + def __init_subclass__(cls): """Register package type""" super().__init_subclass__()