Skip to content

Commit

Permalink
Merge pull request #16 from gerlero/typing
Browse files Browse the repository at this point in the history
Update type hints
  • Loading branch information
gerlero authored Mar 20, 2024
2 parents 93bf2c5 + d0f88ea commit 7e5d7d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

from ._subprocesses import run_process, CalledProcessError

np: Optional[Any]
try:
import numpy as np
from numpy.typing import NDArray
except ModuleNotFoundError:
np = None
numpy = False
else:
numpy = True


FoamDimensionSet = namedtuple(
"FoamDimensionSet",
Expand Down Expand Up @@ -177,7 +180,7 @@ def _serialize_sequence(sequence: Any) -> str:
if (
isinstance(sequence, Sequence)
and not isinstance(sequence, str)
or np
or numpy
and isinstance(sequence, np.ndarray)
):
return f"({' '.join(_serialize(v) for v in sequence)})"
Expand Down Expand Up @@ -216,7 +219,7 @@ def _serialize_dimensions(value: Any) -> str:
if (
isinstance(value, Sequence)
and not isinstance(value, str)
or np
or numpy
and isinstance(value, np.ndarray)
) and len(value) == 7:
return f"[{' '.join(str(v) for v in value)}]"
Expand Down Expand Up @@ -372,7 +375,12 @@ def type(self, value: str) -> None:
@property
def value(
self,
) -> Union[int, float, Sequence[Union[int, float, Sequence[Union[int, float]]]]]:
) -> Union[
int,
float,
Sequence[Union[int, float, Sequence[Union[int, float]]]],
NDArray[np.generic],
]:
"""
Alias of `self["value"]`.
"""
Expand Down Expand Up @@ -457,7 +465,12 @@ def dimensions(
@property
def internal_field(
self,
) -> Union[int, float, Sequence[Union[int, float, Sequence[Union[int, float]]]]]:
) -> Union[
int,
float,
Sequence[Union[int, float, Sequence[Union[int, float]]]],
NDArray[np.generic],
]:
"""
Alias of `self["internalField"]`.
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_internal_field(pitz: FoamCase) -> None:
p_arr = np.arange(size) * 1e-6
U_arr = np.full((size, 3), [-1e-6, 1e-6, 0]) * np.arange(size)[:, np.newaxis]

pitz[0]["p"].internal_field = p_arr # type: ignore
pitz[0]["p"].internal_field = p_arr
pitz[0]["U"].internal_field = U_arr

assert pitz[0]["p"].internal_field == pytest.approx(p_arr)
Expand Down

0 comments on commit 7e5d7d5

Please sign in to comment.