Skip to content

Commit 18789cd

Browse files
authored
refactor(dfn): drop fkeys from v2/toml (#231)
The foreign key system was to support flopy3's concept of "subpackage" but that should be a flopy concern rather than of the specification, though this leaves the Ref typed dict for now
1 parent 27834ff commit 18789cd

1 file changed

Lines changed: 8 additions & 42 deletions

File tree

modflow_devtools/dfn.py

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,16 @@ def block_sort_key(item) -> int:
126126

127127
def get_blocks(dfn: "Dfn") -> Blocks:
128128
"""
129-
Extract blocks from an input definition. Any entry whose key
130-
is not explicitly defined in `Dfn` is a block.
129+
Extract blocks from an input definition.
131130
"""
131+
132+
def _is_block(item: tuple[str, Any]) -> bool:
133+
k, v = item
134+
return k not in Dfn.__annotations__
135+
132136
return dict(
133137
sorted(
134-
{k: v for k, v in dfn.items() if k not in Dfn.__annotations__}.items(), # type: ignore
138+
{k: v for k, v in dfn.items() if _is_block((k, v))}.items(), # type: ignore
135139
key=block_sort_key,
136140
)
137141
)
@@ -225,7 +229,6 @@ class Dfn(TypedDict):
225229
parent: str | None
226230
ref: Ref | None
227231
sln: Sln | None
228-
fkeys: Dfns | None
229232

230233
@staticmethod # type: ignore[misc]
231234
def _load_v1_flat(f, common: dict | None = None) -> tuple[Mapping, list[str]]:
@@ -307,8 +310,6 @@ def _load_v1(cls, f, name, **kwargs) -> "Dfn":
307310
Temporary load routine for the v1 DFN format.
308311
"""
309312

310-
fkeys = {}
311-
refs = kwargs.pop("refs", {})
312313
flat, meta = Dfn._load_v1_flat(f, **kwargs)
313314

314315
def _convert_period_block(block: Block) -> Block:
@@ -378,11 +379,6 @@ def _load(field) -> Field:
378379
default = try_literal_eval(default) if _type != "string" else default
379380
description = field.pop("description", "")
380381
reader = field.pop("reader", "urword")
381-
ref = refs.get(_name, None)
382-
383-
# if the field is a foreign key, register it
384-
if ref:
385-
fkeys[_name] = ref
386382

387383
def _item() -> Field:
388384
"""Load list item."""
@@ -499,26 +495,6 @@ def _fields() -> Fields:
499495
else:
500496
var_["type"] = _type
501497

502-
# if var is a foreign key, return subpkg var instead
503-
if ref:
504-
return Field(
505-
name=ref["val"],
506-
type=_type,
507-
shape=shape,
508-
block=block,
509-
description=(
510-
f"Contains data for the {ref['abbr']} package. Data can be "
511-
f"passed as a dictionary to the {ref['abbr']} package with "
512-
"variable names as keys and package data as values. Data "
513-
f"for the {ref['val']} variable is also acceptable. See "
514-
f"{ref['abbr']} package documentation for more information."
515-
),
516-
default=None,
517-
ref=ref,
518-
reader=reader,
519-
**field,
520-
)
521-
522498
return var_
523499

524500
return dict(sorted(_load(var).items(), key=field_attr_sort_key))
@@ -618,7 +594,6 @@ def _rest():
618594

619595
return cls(
620596
name=name,
621-
fkeys=fkeys,
622597
advanced=_advanced(),
623598
multi=_multi(),
624599
sln=_sln(),
@@ -666,20 +641,11 @@ def _load_all_v1(dfndir: PathLike) -> Dfns:
666641
with common_path.open() as f:
667642
common, _ = Dfn._load_v1_flat(f)
668643

669-
# load references (subpackages)
670-
refs = {}
671-
for path in paths:
672-
with path.open() as f:
673-
dfn = Dfn.load(f, name=path.stem, common=common)
674-
ref = dfn.get("ref", None)
675-
if ref:
676-
refs[ref["key"]] = ref
677-
678644
# load definitions
679645
dfns: Dfns = {}
680646
for path in paths:
681647
with path.open() as f:
682-
dfn = Dfn.load(f, name=path.stem, common=common, refs=refs)
648+
dfn = Dfn.load(f, name=path.stem, common=common)
683649
dfns[path.stem] = dfn
684650

685651
return dfns

0 commit comments

Comments
 (0)