Skip to content

Commit 703e74f

Browse files
committed
Remaining type hints improvements
1 parent afc99be commit 703e74f

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

climada/entity/impact_funcs/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def from_step_impf(
189189
haz_type: str,
190190
mdd: tuple[float, float] = (0, 1),
191191
paa: tuple[float, float] = (1, 1),
192-
impf_id: int = 1,
192+
impf_id: int | str = 1,
193193
**kwargs,
194194
):
195195
"""Step function type impact function.
@@ -207,7 +207,7 @@ def from_step_impf(
207207
(min, max) mdd values. The default is (0, 1)
208208
paa: tuple(float, float)
209209
(min, max) paa values. The default is (1, 1)
210-
impf_id : int, optional, default=1
210+
impf_id : int|str, optional, default=1
211211
impact function id
212212
kwargs :
213213
keyword arguments passed to ImpactFunc()
@@ -250,7 +250,7 @@ def from_sigmoid_impf(
250250
k: float,
251251
x0: float,
252252
haz_type: str,
253-
impf_id: int = 1,
253+
impf_id: int | str = 1,
254254
**kwargs,
255255
):
256256
r"""Sigmoid type impact function hinging on three parameter.
@@ -320,7 +320,7 @@ def from_poly_s_shape(
320320
scale: float,
321321
exponent: float,
322322
haz_type: str,
323-
impf_id: int = 1,
323+
impf_id: int | str = 1,
324324
**kwargs,
325325
):
326326
r"""S-shape polynomial impact function hinging on four parameter.

climada/entity/impact_funcs/impact_func_set.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ def append(self, func: ImpactFunc):
141141
self._data[func.haz_type] = dict()
142142
self._data[func.haz_type][func.id] = func
143143

144-
def remove_func(self, haz_type: str = None, fun_id: str | int = None):
144+
def remove_func(
145+
self, haz_type: Optional[str] = None, fun_id: Optional[str | int] = None
146+
):
145147
"""Remove impact function(s) with provided hazard type and/or id.
146148
If no input provided, all impact functions are removed.
147149
@@ -174,21 +176,21 @@ def remove_func(self, haz_type: str = None, fun_id: str | int = None):
174176
self._data = dict()
175177

176178
@overload
177-
def get_func(self, haz_type: str, fun_id: int) -> ImpactFunc: ...
179+
def get_func(self, haz_type: str, fun_id: int | str) -> ImpactFunc: ...
178180

179181
@overload
180182
def get_func(self, haz_type: str, fun_id: None = None) -> list[ImpactFunc]: ...
181183

182184
@overload
183-
def get_func(self, haz_type: None, fun_id: int) -> list[ImpactFunc]: ...
185+
def get_func(self, haz_type: None, fun_id: int | str) -> list[ImpactFunc]: ...
184186

185187
@overload
186188
def get_func(
187189
self, haz_type: None = None, fun_id: None = None
188190
) -> Dict[str, Dict[int, ImpactFunc]]: ...
189191

190192
def get_func(
191-
self, haz_type: Optional[str] = None, fun_id: Optional[int] = None
193+
self, haz_type: Optional[str] = None, fun_id: Optional[int | str] = None
192194
) -> Union[ImpactFunc, list[ImpactFunc], Dict[str, Dict[int, ImpactFunc]]]:
193195
"""Get ImpactFunc(s) of input hazard type and/or id.
194196
If no input provided, all impact functions are returned.
@@ -225,7 +227,7 @@ def get_func(
225227
else:
226228
return self._data
227229

228-
def get_hazard_types(self, fun_id: str | int) -> list[str]:
230+
def get_hazard_types(self, fun_id: Optional[str | int]) -> list[str]:
229231
"""Get impact functions hazard types contained for the id provided.
230232
Return all hazard types if no input id.
231233
@@ -247,7 +249,15 @@ def get_hazard_types(self, fun_id: str | int) -> list[str]:
247249
haz_types.append(vul_haz)
248250
return haz_types
249251

250-
def get_ids(self, haz_type: str = None) -> list[int | str]:
252+
@overload
253+
def get_ids(self, haz_type: None = None) -> dict[str, list[str | int]]: ...
254+
255+
@overload
256+
def get_ids(self, haz_type: str) -> list[int | str]: ...
257+
258+
def get_ids(
259+
self, haz_type: Optional[str] = None
260+
) -> dict[str, list[str | int]] | list[int | str]:
251261
"""Get impact functions ids contained for the hazard type provided.
252262
Return all ids for each hazard type if no input hazard type.
253263
@@ -272,7 +282,9 @@ def get_ids(self, haz_type: str = None) -> list[int | str]:
272282
except KeyError:
273283
return list()
274284

275-
def size(self, haz_type: str = None, fun_id: str | int = None) -> int:
285+
def size(
286+
self, haz_type: Optional[str] = None, fun_id: Optional[str | int] = None
287+
) -> int:
276288
"""Get number of impact functions contained with input hazard type and
277289
/or id. If no input provided, get total number of impact functions.
278290
@@ -295,6 +307,7 @@ def size(self, haz_type: str = None, fun_id: str | int = None) -> int:
295307
return 1
296308
if (haz_type is not None) or (fun_id is not None):
297309
return len(self.get_func(haz_type, fun_id))
310+
298311
return sum(len(vul_list) for vul_list in self.get_ids().values())
299312

300313
def check(self):
@@ -316,7 +329,7 @@ def check(self):
316329
)
317330
vul.check()
318331

319-
def extend(self, impact_funcs: ImpactFuncSet):
332+
def extend(self, impact_funcs: "ImpactFuncSet"):
320333
"""Append impact functions of input ImpactFuncSet to current
321334
ImpactFuncSet. Overwrite ImpactFunc if same id and haz_type.
322335
@@ -339,7 +352,13 @@ def extend(self, impact_funcs: ImpactFuncSet):
339352
for _, vul in vul_dict.items():
340353
self.append(vul)
341354

342-
def plot(self, haz_type: str = None, fun_id: str | int = None, axis=None, **kwargs):
355+
def plot(
356+
self,
357+
haz_type: Optional[str] = None,
358+
fun_id: Optional[str | int] = None,
359+
axis=None,
360+
**kwargs,
361+
):
343362
"""Plot impact functions of selected hazard (all if not provided) and
344363
selected function id (all if not provided).
345364

0 commit comments

Comments
 (0)