diff --git a/foamlib/_files/_files.py b/foamlib/_files/_files.py index 5053673..6b87cb1 100644 --- a/foamlib/_files/_files.py +++ b/foamlib/_files/_files.py @@ -19,10 +19,10 @@ from ._serialization import Kind, dumps, normalize from ._types import ( Data, - DataEntry, Dict_, Dimensioned, DimensionSet, + Entry, Field, File, MutableData, @@ -57,13 +57,13 @@ def __init__(self, _file: FoamFile, _keywords: tuple[str, ...]) -> None: self._file = _file self._keywords = _keywords - def __getitem__(self, keyword: str) -> DataEntry | FoamFile.SubDict: + def __getitem__(self, keyword: str) -> Data | FoamFile.SubDict: return self._file[(*self._keywords, keyword)] def __setitem__( self, keyword: str, - data: Data, + data: Entry, ) -> None: self._file[(*self._keywords, keyword)] = data @@ -174,7 +174,7 @@ def object_(self, value: str) -> None: def __getitem__( self, keywords: str | tuple[str, ...] | None - ) -> DataEntry | FoamFile.SubDict: + ) -> Data | FoamFile.SubDict: if not keywords: keywords = () elif not isinstance(keywords, tuple): @@ -190,7 +190,7 @@ def __getitem__( return FoamFile.SubDict(self, keywords) return deepcopy(value) - def __setitem__(self, keywords: str | tuple[str, ...] | None, data: Data) -> None: + def __setitem__(self, keywords: str | tuple[str, ...] | None, data: Entry) -> None: if not keywords: keywords = () elif not isinstance(keywords, tuple): @@ -438,7 +438,7 @@ def value(self) -> None: def __getitem__( self, keywords: str | tuple[str, ...] | None - ) -> DataEntry | FoamFile.SubDict: + ) -> Data | FoamFile.SubDict: if not keywords: keywords = () elif not isinstance(keywords, tuple): diff --git a/foamlib/_files/_parsing.py b/foamlib/_files/_parsing.py index 6e31bd5..1cdcdfa 100644 --- a/foamlib/_files/_parsing.py +++ b/foamlib/_files/_parsing.py @@ -37,7 +37,7 @@ printables, ) -from ._types import DataEntry, Dimensioned, DimensionSet, File +from ._types import Data, Dimensioned, DimensionSet, File def _list_of(entry: ParserElement) -> ParserElement: @@ -332,11 +332,11 @@ def _unpack_binary_field( ) -class Parsed(Mapping[Tuple[str, ...], Union[DataEntry, EllipsisType]]): +class Parsed(Mapping[Tuple[str, ...], Union[Data, EllipsisType]]): def __init__(self, contents: bytes) -> None: self._parsed: MutableMapping[ tuple[str, ...], - tuple[int, DataEntry | EllipsisType, int], + tuple[int, Data | EllipsisType, int], ] = {} for parse_result in _FILE.parse_string( contents.decode("latin-1"), parse_all=True @@ -349,10 +349,10 @@ def __init__(self, contents: bytes) -> None: @staticmethod def _flatten_result( parse_result: ParseResults, *, _keywords: tuple[str, ...] = () - ) -> Mapping[tuple[str, ...], tuple[int, DataEntry | EllipsisType, int]]: + ) -> Mapping[tuple[str, ...], tuple[int, Data | EllipsisType, int]]: ret: MutableMapping[ tuple[str, ...], - tuple[int, DataEntry | EllipsisType, int], + tuple[int, Data | EllipsisType, int], ] = {} start = parse_result.locn_start assert isinstance(start, int) @@ -379,14 +379,14 @@ def _flatten_result( ret[(*_keywords, keyword)] = (start, d, end) return ret - def __getitem__(self, keywords: tuple[str, ...]) -> DataEntry | EllipsisType: + def __getitem__(self, keywords: tuple[str, ...]) -> Data | EllipsisType: _, data, _ = self._parsed[keywords] return data def put( self, keywords: tuple[str, ...], - data: DataEntry | EllipsisType, + data: Data | EllipsisType, content: bytes, ) -> None: start, end = self.entry_location(keywords, missing_ok=True) diff --git a/foamlib/_files/_serialization.py b/foamlib/_files/_serialization.py index bc06ab3..1250bb4 100644 --- a/foamlib/_files/_serialization.py +++ b/foamlib/_files/_serialization.py @@ -12,7 +12,7 @@ from typing import Mapping, Sequence from ._parsing import DATA, KEYWORD -from ._types import Data, DataEntry, Dimensioned, DimensionSet +from ._types import Data, Dimensioned, DimensionSet, Entry from ._util import is_sequence try: @@ -34,14 +34,14 @@ class Kind(Enum): @overload -def normalize(data: DataEntry, *, kind: Kind = Kind.DEFAULT) -> DataEntry: ... +def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data: ... @overload -def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data: ... +def normalize(data: Entry, *, kind: Kind = Kind.DEFAULT) -> Entry: ... -def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data: +def normalize(data: Entry, *, kind: Kind = Kind.DEFAULT) -> Entry: if numpy and isinstance(data, np.ndarray): ret = data.tolist() assert isinstance(ret, list) @@ -76,7 +76,7 @@ def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data: assert isinstance(data, str) return data - return cast(DataEntry, DATA.parse_string(data, parse_all=True)[0]) + return cast(Data, DATA.parse_string(data, parse_all=True)[0]) if isinstance( data, @@ -89,7 +89,7 @@ def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data: def dumps( - data: Data, + data: Entry, *, kind: Kind = Kind.DEFAULT, ) -> bytes: diff --git a/foamlib/_files/_types.py b/foamlib/_files/_types.py index 6dffa30..4c2b0f2 100644 --- a/foamlib/_files/_types.py +++ b/foamlib/_files/_types.py @@ -46,33 +46,36 @@ def __post_init__(self) -> None: Field = Union[ - Tensor, Sequence[Tensor], "np.ndarray[Tuple[int, int], np.dtype[np.generic]]" + Tensor, + Sequence[Tensor], + "np.ndarray[Tuple[int], np.dtype[np.generic]]", + "np.ndarray[Tuple[int, int], np.dtype[np.generic]]", ] -DataEntry = Union[ +Data = Union[ str, int, float, bool, Dimensioned, DimensionSet, - Sequence["Data"], + Sequence["Entry"], Tensor, Field, ] -Data = Union[ - DataEntry, - Mapping[str, "Data"], +Entry = Union[ + Data, + Mapping[str, "Entry"], ] """ A value that can be stored in an OpenFOAM file. """ MutableData = Union[ - DataEntry, + Data, MutableMapping[str, "MutableData"], ] -Dict_ = Dict[str, Union["Data", "Dict_"]] -File = Dict[Optional[str], Union["Data", "Dict_"]] +Dict_ = Dict[str, Union["Entry", "Dict_"]] +File = Dict[Optional[str], Union["Entry", "Dict_"]] diff --git a/foamlib/_files/_util.py b/foamlib/_files/_util.py index 5ce32ff..d7da07d 100644 --- a/foamlib/_files/_util.py +++ b/foamlib/_files/_util.py @@ -14,10 +14,10 @@ from typing_extensions import TypeGuard if TYPE_CHECKING: - from ._types import Data + from ._types import Entry def is_sequence( - value: Data, -) -> TypeGuard[Sequence[Data]]: + value: Entry, +) -> TypeGuard[Sequence[Entry]]: return isinstance(value, Sequence) and not isinstance(value, str)