Skip to content

Commit

Permalink
Fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Dec 27, 2024
1 parent 5a89f90 commit bd0e1b7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _parse_ascii_field(
s = re.sub(ignore.re, " ", s)
s = s.replace("(", " ").replace(")", " ")

return np.fromstring(s, sep=" ").reshape(-1, *tensor_kind.shape)
return np.fromstring(s, sep=" ").reshape(-1, *tensor_kind.shape) # type: ignore [return-value]


def _unpack_binary_field(
Expand All @@ -99,7 +99,7 @@ def _unpack_binary_field(
assert float_size in (4, 8)

dtype = np.float32 if float_size == 4 else float
return np.frombuffer(b, dtype=dtype).reshape(-1, *tensor_kind.shape)
return np.frombuffer(b, dtype=dtype).reshape(-1, *tensor_kind.shape) # type: ignore [return-value]


def _tensor_list(
Expand Down
2 changes: 1 addition & 1 deletion foamlib/_files/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def normalize(data: Entry, *, kind: Kind = Kind.DEFAULT) -> Entry:
arr = arr.astype(float)

if arr.ndim == 1 or (arr.ndim == 2 and arr.shape[1] in (3, 6, 9)):
return arr
return arr # type: ignore [return-value]

return data

Expand Down
2 changes: 1 addition & 1 deletion foamlib/_files/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Dimensioned:

def __post_init__(self) -> None:
if is_sequence(self.value):
self.value = np.asarray(self.value, dtype=float)
self.value = np.asarray(self.value, dtype=float) # type: ignore [assignment]
else:
assert isinstance(self.value, (int, float, np.ndarray))
self.value = float(self.value)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_files/test_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_serialize_data() -> None:
== b"nonuniform List<vector> 2(\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@)"
)
assert (
dumps(np.array([1, 2], dtype=np.float32), kind=Kind.BINARY_FIELD)
dumps(np.array([1, 2], dtype=np.float32), kind=Kind.BINARY_FIELD) # type: ignore [arg-type]
== b"nonuniform List<scalar> 2(\x00\x00\x80?\x00\x00\x00@)"
)
assert (
Expand Down
14 changes: 7 additions & 7 deletions tests/test_files/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ def test_internal_field(cavity: FoamCase) -> None:
p_arr = np.zeros(size)
U_arr = np.zeros((size, 3))

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

assert cavity[0]["p"].internal_field == pytest.approx(p_arr)
U = cavity[0]["U"].internal_field
assert isinstance(U, np.ndarray)
assert U_arr == pytest.approx(U)

p_arr = np.arange(size) * 1e-6
p_arr = np.arange(size) * 1e-6 # type: ignore [assignment]
U_arr = np.full((size, 3), [-1e-6, 1e-6, 0]) * np.arange(size)[:, np.newaxis]

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

assert cavity[0]["p"].internal_field == pytest.approx(p_arr)
U = cavity[0]["U"].internal_field
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_binary_field(cavity: FoamCase) -> None:
p_arr = np.arange(len(p_bin)) * 1e-6
U_arr = np.full_like(U_bin, [-1e-6, 1e-6, 0]) * np.arange(len(U_bin))[:, np.newaxis]

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

assert cavity[0]["p"].internal_field == pytest.approx(p_arr)
Expand All @@ -237,7 +237,7 @@ def test_compressed_field(cavity: FoamCase) -> None:
p_arr = np.arange(len(p_bin)) * 1e-6
U_arr = np.full_like(U_bin, [-1e-6, 1e-6, 0]) * np.arange(len(U_bin))[:, np.newaxis]

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

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

0 comments on commit bd0e1b7

Please sign in to comment.