diff --git a/foamlib/_files/_files.py b/foamlib/_files/_files.py index af2cc90..db5a7f2 100644 --- a/foamlib/_files/_files.py +++ b/foamlib/_files/_files.py @@ -195,7 +195,7 @@ def __setitem__( elif not isinstance(keywords, tuple): keywords = (keywords,) - if not self and keywords[0] != "FoamFile": + if not self and "FoamFile" not in self and keywords[0] != "FoamFile": self.header = { "version": 2.0, "format": "ascii", @@ -278,7 +278,11 @@ def __delitem__(self, keywords: Optional[Union[str, Tuple[str, ...]]]) -> None: def _iter(self, keywords: Tuple[str, ...] = ()) -> Iterator[Optional[str]]: _, parsed = self._read() - yield from (k[-1] if k else None for k in parsed if k[:-1] == keywords) + yield from ( + k[-1] if k else None + for k in parsed + if k != ("FoamFile",) and k[:-1] == keywords + ) def __iter__(self) -> Iterator[Optional[str]]: return self._iter() @@ -310,7 +314,9 @@ def __fspath__(self) -> str: def as_dict(self) -> FoamFileBase._Dict: """Return a nested dict representation of the file.""" _, parsed = self._read() - return parsed.as_dict() + d = parsed.as_dict() + del d["FoamFile"] + return d class FoamFieldFile(FoamFile): diff --git a/tests/test_files/test_files.py b/tests/test_files/test_files.py index 9897ad3..59ec4d5 100644 --- a/tests/test_files/test_files.py +++ b/tests/test_files/test_files.py @@ -26,11 +26,12 @@ def test_write_read(tmp_path: Path) -> None: d["key"] = "value" assert d["key"] == "value" - assert len(d) == 2 + assert len(d) == 1 assert "key" in d - assert list(d) == ["FoamFile", "key"] + assert list(d) == ["key"] + assert "FoamFile" in d del d["key"] - assert len(d) == 1 + assert not d assert "key" not in d with pytest.raises(KeyError): del d["key"]