Skip to content

Commit 564df83

Browse files
authored
Merge pull request #672 from gerlero/typing
Improve type hints
2 parents 056a396 + a963945 commit 564df83

File tree

3 files changed

+165
-70
lines changed

3 files changed

+165
-70
lines changed

src/foamlib/_files/_parsing/parsing.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import dataclasses
22
import sys
33
from collections.abc import Collection, Iterator, Sequence
4-
from typing import cast
4+
from typing import cast, overload
5+
6+
if sys.version_info >= (3, 11):
7+
from typing import Never, Unpack
8+
else:
9+
from typing_extensions import Never, Unpack
510

611
if sys.version_info >= (3, 12):
712
from typing import override
@@ -116,20 +121,63 @@ def _flatten_results(
116121
)
117122
return ret
118123

124+
@overload
125+
@with_default
126+
def getall(self, keywords: tuple[()], /) -> Collection[StandaloneData]: ...
127+
128+
@overload
129+
@with_default
130+
def getall(
131+
self, keywords: tuple[str, Unpack[tuple[str, ...]]], /
132+
) -> Collection[Data | EllipsisType | None]: ...
133+
119134
@override
120135
@with_default
121136
def getall(
122137
self, keywords: tuple[str, ...], /
123-
) -> Collection[Data | StandaloneData | EllipsisType]:
138+
) -> Collection[Data | StandaloneData | EllipsisType | None]:
124139
return [entry.data for entry in self._parsed.getall(keywords)]
125140

141+
@overload
142+
def __getitem__(self, keywords: tuple[()]) -> StandaloneData: ...
143+
144+
@overload
145+
def __getitem__(
146+
self, keywords: tuple[str, Unpack[tuple[str, ...]]]
147+
) -> Data | EllipsisType | None: ...
148+
149+
@override
150+
def __getitem__(
151+
self, keywords: tuple[str, ...]
152+
) -> Data | StandaloneData | EllipsisType | None: # ty: ignore[invalid-method-override]
153+
entry = self._parsed[keywords]
154+
return entry.data
155+
126156
@override
127-
def __setitem__(
128-
self, key: tuple[str, ...], value: Data | StandaloneData | EllipsisType | None
157+
def __setitem__( # ty: ignore[invalid-method-override]
158+
self, key: Never, value: Never
129159
) -> None: # pragma: no cover
130160
msg = "Use 'put' method instead"
131161
raise NotImplementedError(msg)
132162

163+
@overload
164+
def put(
165+
self,
166+
keywords: tuple[()],
167+
/,
168+
data: StandaloneData,
169+
content: bytes,
170+
) -> None: ...
171+
172+
@overload
173+
def put(
174+
self,
175+
keywords: tuple[str, Unpack[tuple[str, ...]]],
176+
/,
177+
data: Data | EllipsisType | None,
178+
content: bytes,
179+
) -> None: ...
180+
133181
def put(
134182
self,
135183
keywords: tuple[str, ...],
@@ -143,6 +191,24 @@ def put(
143191
self._parsed[keywords] = ParsedFile._Entry(data, start, start + len(content))
144192
self._remove_child_entries(keywords)
145193

194+
@overload
195+
def add(
196+
self,
197+
keywords: tuple[()],
198+
data: StandaloneData,
199+
content: bytes,
200+
/,
201+
) -> None: ...
202+
203+
@overload
204+
def add(
205+
self,
206+
keywords: tuple[str, Unpack[tuple[str, ...]]],
207+
data: Data | EllipsisType | None,
208+
content: bytes,
209+
/,
210+
) -> None: ...
211+
146212
@override
147213
def add( # ty: ignore[invalid-method-override]
148214
self,

src/foamlib/_files/_typing.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import sys
12
from collections.abc import Mapping, Sequence
23
from typing import Literal, TypeAlias
34

5+
if sys.version_info >= (3, 11):
6+
from typing import Unpack
7+
else:
8+
from typing_extensions import Unpack
9+
410
import numpy as np
511
from multicollections import MultiDict
612

@@ -38,8 +44,11 @@
3844
DataEntry | Sequence["DataEntryLike | KeywordEntryLike | DictLike"] | FieldLike
3945
)
4046

41-
Data: TypeAlias = DataEntry | tuple[DataEntry, ...]
42-
DataLike: TypeAlias = DataEntryLike | tuple[DataEntryLike, ...]
47+
Data: TypeAlias = DataEntry | tuple[DataEntry, DataEntry, Unpack[tuple[DataEntry, ...]]]
48+
DataLike: TypeAlias = (
49+
DataEntryLike
50+
| tuple[DataEntryLike, DataEntryLike, Unpack[tuple[DataEntryLike, ...]]]
51+
)
4352

4453
StandaloneDataEntry: TypeAlias = (
4554
DataEntry
@@ -57,9 +66,21 @@
5766
| Sequence[Sequence[int]]
5867
)
5968

60-
StandaloneData: TypeAlias = StandaloneDataEntry | tuple[StandaloneDataEntry, ...]
69+
StandaloneData: TypeAlias = (
70+
StandaloneDataEntry
71+
| tuple[
72+
StandaloneDataEntry,
73+
StandaloneDataEntry,
74+
Unpack[tuple[StandaloneDataEntry, ...]],
75+
]
76+
)
6177
StandaloneDataLike: TypeAlias = (
62-
StandaloneDataEntryLike | tuple[StandaloneDataEntryLike, ...]
78+
StandaloneDataEntryLike
79+
| tuple[
80+
StandaloneDataEntryLike,
81+
StandaloneDataEntryLike,
82+
Unpack[tuple[StandaloneDataEntryLike, ...]],
83+
]
6384
)
6485

6586
SubDict: TypeAlias = (

0 commit comments

Comments
 (0)