Skip to content

Commit 19b3daa

Browse files
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
1 parent 1e6991d commit 19b3daa

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

autotest/test_mf6.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,23 @@ def test_multi_model(function_tmpdir):
15791579
sim.write_simulation()
15801580
sim.run_simulation()
15811581

1582+
with pytest.raises(
1583+
flopy.mf6.mfbase.FlopyException,
1584+
match='Extraneous kwargs "param_does_not_exist" '
1585+
"provided to MFPackage.",
1586+
):
1587+
# test kwargs error checking
1588+
wel = ModflowGwfwel(
1589+
gwf2,
1590+
print_input=True,
1591+
print_flows=True,
1592+
stress_period_data=welspd,
1593+
save_flows=False,
1594+
auxiliary="CONCENTRATION",
1595+
pname="WEL-1",
1596+
param_does_not_exist=True,
1597+
)
1598+
15821599

15831600
@requires_exe("mf6")
15841601
def test_namefile_creation(tmpdir):

flopy/mf6/mfpackage.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,16 +1585,18 @@ def __init__(
15851585
loading_package=False,
15861586
**kwargs,
15871587
):
1588+
parent_file = kwargs.pop("parent_file", None)
15881589
if isinstance(parent, MFPackage):
15891590
self.model_or_sim = parent.model_or_sim
15901591
self.parent_file = parent
1591-
elif "parent_file" in kwargs:
1592+
elif parent_file is not None:
15921593
self.model_or_sim = parent
1593-
self.parent_file = kwargs["parent_file"]
1594+
self.parent_file = parent_file
15941595
else:
15951596
self.model_or_sim = parent
15961597
self.parent_file = None
1597-
if "_internal_package" in kwargs and kwargs["_internal_package"]:
1598+
_internal_package = kwargs.pop("_internal_package", False)
1599+
if _internal_package:
15981600
self.internal_package = True
15991601
else:
16001602
self.internal_package = False
@@ -1724,15 +1726,29 @@ def __init__(
17241726
self.bc_color = "black"
17251727
self.__inattr = False
17261728
self._child_package_groups = {}
1729+
child_builder_call = kwargs.pop("child_builder_call", None)
17271730
if (
17281731
self.parent_file is not None
1729-
and "child_builder_call" not in kwargs
1732+
and child_builder_call is None
17301733
and package_type in self.parent_file._child_package_groups
17311734
):
17321735
# initialize as part of the parent's child package group
17331736
chld_pkg_grp = self.parent_file._child_package_groups[package_type]
17341737
chld_pkg_grp.init_package(self, self._filename)
17351738

1739+
# remove any remaining valid kwargs
1740+
key_list = list(kwargs.keys())
1741+
for key in key_list:
1742+
if "filerecord" in key and hasattr(self, f"{key}"):
1743+
kwargs.pop(f"{key}")
1744+
# check for extraneous kwargs
1745+
if len(kwargs) > 0:
1746+
kwargs_str = ", ".join(kwargs.keys())
1747+
excpt_str = (
1748+
f'Extraneous kwargs "{kwargs_str}" provided to MFPackage.'
1749+
)
1750+
raise FlopyException(excpt_str)
1751+
17361752
def __init_subclass__(cls):
17371753
"""Register package type"""
17381754
super().__init_subclass__()

0 commit comments

Comments
 (0)