Skip to content

Commit

Permalink
🧹 Improved typing and exception raising
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed May 20, 2022
1 parent e725f4b commit 2e44162
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions glotaran/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def get_dataset_groups(self) -> dict[str, DatasetGroup]:
if group not in groups:
try:
groups[group] = DatasetGroup(model=self.dataset_group_models[group])
except KeyError:
raise ValueError(f"Unknown dataset group '{group}'")
except KeyError as e:
raise ValueError(f"Unknown dataset group '{group}'") from e
groups[group].dataset_models[dataset_model.label] = dataset_model
return groups

Expand Down Expand Up @@ -367,7 +367,7 @@ def is_groupable(self, parameters: ParameterGroup, data: dict[str, xr.DataArray]
}
return len(global_dimensions) == 1 and len(model_dimensions) == 1

def problem_list(self, parameters: ParameterGroup = None) -> list[str]:
def problem_list(self, parameters: ParameterGroup | None = None) -> list[str]:
"""
Returns a list with all problems in the model and missing parameters if specified.
Expand Down
13 changes: 6 additions & 7 deletions glotaran/parameter/parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
from copy import copy
from textwrap import indent
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -132,10 +133,8 @@ def from_list(

for i, item in enumerate(parameter_list):
if isinstance(item, (str, int, float)):
try:
with contextlib.suppress(ValueError):
item = float(item)
except Exception:
pass
if isinstance(item, (float, int, list)):
root.add_parameter(
Parameter.from_list_or_value(item, label=str(i + 1), default_options=defaults)
Expand Down Expand Up @@ -444,14 +443,14 @@ def get(self, label: str) -> Parameter: # type:ignore[override]
for element in path:
try:
group = group[element]
except KeyError:
raise ParameterNotFoundException(path, label)
except KeyError as e:
raise ParameterNotFoundException(path, label) from e
try:
parameter = group._parameters[label]
parameter.full_label = full_label
return parameter
except KeyError:
raise ParameterNotFoundException(path, label)
except KeyError as e:
raise ParameterNotFoundException(path, label) from e

def copy(self) -> ParameterGroup:
"""Create a copy of the :class:`ParameterGroup`.
Expand Down

0 comments on commit 2e44162

Please sign in to comment.