Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 Fix ParameterGroup repr when created with 'from_list' #827

Merged
merged 1 commit into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions glotaran/parameter/parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,17 @@ def _repr_markdown_(self) -> str:

def __repr__(self):
"""Representation used by repl and tracebacks."""

parameter_short_notations = [
[str(parameter.label), parameter.value] for parameter in self._parameters.values()
]
if self.label is None:
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 len(self._parameters) == 0:
return f"{type(self).__name__}.from_dict({super().__repr__()})"
else:
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