Skip to content

Commit

Permalink
🩹 Fix ParameterGroup repr when created with 'from_list'
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Sep 18, 2021
1 parent 2f1afe7 commit 421cc84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 9 additions & 7 deletions glotaran/parameter/parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,16 @@ def _repr_markdown_(self) -> str:

def __repr__(self):
"""Representation used by repl and tracebacks."""
if self.label is None:

parameter_short_notations = [
[str(parameter.label), parameter.value] for parameter in self._parameters.values()
]
if self.label is None and len(self._parameters) == 0:
return f"{type(self).__name__}.from_dict({super().__repr__()})"
elif len(self._parameters):
parameter_short_notations = [
f"[{parameter.label!r}, {parameter.value}]"
for _, parameter in self._parameters.items()
]
return f"[{', '.join(parameter_short_notations)}]"
if self.label is None:
return f"{type(self).__name__}.from_list({parameter_short_notations})"
if len(self._parameters):
return parameter_short_notations.__repr__()
else:
return super().__repr__()

Expand Down
15 changes: 14 additions & 1 deletion glotaran/parameter/test/test_parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,27 @@ def test_param_group_markdown_is_order_independent():


def test_param_group_repr():
"""Repr creates code to recreate the object."""
"""Repr creates code to recreate the object with from_dict."""
result = ParameterGroup.from_dict({"foo": {"bar": [["1", 1.0], ["2", 2.0], ["3", 3.0]]}})
result_short = ParameterGroup.from_dict({"foo": {"bar": [1, 2, 3]}})
expected = "ParameterGroup.from_dict({'foo': {'bar': [['1', 1.0], ['2', 2.0], ['3', 3.0]]}})"

assert result == result_short
assert result_short.__repr__() == expected
assert result.__repr__() == expected
assert result == eval(result.__repr__())


def test_param_group_repr_from_list():
"""Repr creates code to recreate the object with from_list."""
result = ParameterGroup.from_list([["1", 2.3], ["2", 3.0]])
result_short = ParameterGroup.from_list([2.3, 3.0])
expected = "ParameterGroup.from_list([['1', 2.3], ['2', 3.0]])"

assert result == result_short
assert result.__repr__() == expected
assert result_short.__repr__() == expected
assert result == eval(result.__repr__())


def test_param_group_ipython_rendering():
Expand Down

0 comments on commit 421cc84

Please sign in to comment.