Skip to content

Commit

Permalink
Merge pull request #276 from gerlero/typing
Browse files Browse the repository at this point in the history
Update type hints
  • Loading branch information
gerlero authored Nov 16, 2024
2 parents 5c53f23 + 8a02f6e commit 9de32a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
12 changes: 6 additions & 6 deletions foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from ._serialization import Kind, dumps, normalize
from ._types import (
Data,
DataEntry,
Dict_,
Dimensioned,
DimensionSet,
Entry,
Field,
File,
MutableData,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions foamlib/_files/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -89,7 +89,7 @@ def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data:


def dumps(
data: Data,
data: Entry,
*,
kind: Kind = Kind.DEFAULT,
) -> bytes:
Expand Down
21 changes: 12 additions & 9 deletions foamlib/_files/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"]]
6 changes: 3 additions & 3 deletions foamlib/_files/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9de32a9

Please sign in to comment.