Skip to content

Commit

Permalink
fix(MFPackage kwargs check): Now verifying that only valid kwargs are…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
spaulins-usgs authored Dec 22, 2022
1 parent 1e6991d commit 19b3daa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
17 changes: 17 additions & 0 deletions autotest/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 20 additions & 4 deletions flopy/mf6/mfpackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__()
Expand Down

0 comments on commit 19b3daa

Please sign in to comment.