Skip to content

Commit

Permalink
👌 Improved validation output
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed May 20, 2022
1 parent 4a96db2 commit 2acf3ec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions glotaran/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ def problem_list(self, parameters: ParameterGroup | None = None) -> list[str]:

return problems

def validate(self, parameters: ParameterGroup = None, raise_exception: bool = False) -> str:
def validate(
self, parameters: ParameterGroup = None, raise_exception: bool = False
) -> MarkdownStr:
"""
Returns a string listing all problems in the model and missing parameters if specified.
Expand All @@ -410,14 +412,14 @@ def validate(self, parameters: ParameterGroup = None, raise_exception: bool = Fa
result = ""

if problems := self.problem_list(parameters):
result = f"Your model has {len(problems)} problems:\n"
result = f"Your model has {len(problems)} problem{'s' if len(problems) > 1 else ''}:\n"
for p in problems:
result += f"\n * {p}"
if raise_exception:
raise ModelError(result)
else:
result = "Your model is valid."
return result
return MarkdownStr(result)

def valid(self, parameters: ParameterGroup = None) -> bool:
"""Returns `True` if the number problems in the model is 0, else `False`
Expand Down
4 changes: 2 additions & 2 deletions glotaran/model/test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ def test_model_validate_missing_parameters():
)
expected = dedent(
"""\
Your model has 1 problems:
Your model has 1 problem:
* Parameter definition is missing values for the labels:
- b.missing_value_1
- b.missing_value_2
- kinetic.j.missing_value_3"""
)
assert model.validate(parameters) == expected
assert str(model.validate(parameters)) == expected


def test_items(test_model: Model):
Expand Down
4 changes: 2 additions & 2 deletions glotaran/project/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ def problem_list(self) -> list[str]:
"""
return self.model.problem_list(self.parameters)

def validate(self) -> str:
def validate(self) -> MarkdownStr:
"""Return a string listing all problems in the model and missing parameters.
Returns
-------
str
MarkdownStr
A user-friendly string containing all the problems of a model if any.
Defaults to 'Your model is valid.' if no problems are found.
"""
Expand Down

0 comments on commit 2acf3ec

Please sign in to comment.