Change TrialState.__repr__ and TrialState.__str__#6281
Conversation
TrialState.__repr__:
TrialState.__repr__:TrialState.__repr__
|
@c-bata |
|
Thank you for your PR and sorry for the late response. class TrialState(enum.IntEnum):
...
__str__ = enum.Enum.__str__
...It does not return a valid Python expression for |
|
I also prefer to use the default |
|
Thank you! Could you update this PR as my comment? |
|
|
strange, I pushed changes to the test but failing CI job seems to have executed the old test with |
|
Ah... sorry I misunderstood. I didn't notice there was another test using eval. |
TrialState.__repr__TrialState.__repr__ and TrialState.__str__
|
I just noticed the PR title was obsolete. Is the new title OK? |
gen740
left a comment
There was a problem hiding this comment.
I think tests/trial_tests/test_state.py is unnecessary since it only tests Python’s built-in functionality, not Optuna.
|
I think it's advisable to be there to notify people when stdlib changed behavior. |
|
and also when the baseclass is changed again. |
If so, the test should compare with the full output of the repr like, @pytest.mark.parametrize("state", TrialState)
def test_trial_state_repr(state: TrialState) -> None:
assert f"<TrialState.{state.name}: {state.value}>" in repr(state)but I think even this is not necessary, because Optuna does not depend on any Would there be any issue if the base class of |
|
While I'm neutral about this test, Optuna's policy is to not test visual elements like visualization or error messages, so this test might be unnecessary. |
|
I'd rather insist to keep the test because it describes the intention to use |
|
I'm neutral with having tests, but aren't your points about documentation or comments in the code rather than about the tests themselves? |
|
In the first place, why does If |
|
To explain the background: |
|
Of course documentation or comments will do. I won't insist to have the test if they are written but I prefer test personally because documentation or comments tend to get ignored. Do you want me to write comments? |
|
@ktns Thank you for your continuous contribution! Regarding the ongoing discussion, I also think leaving a code comment instead of unit tests would be more appropriate in this case, since it would be difficult to find the background from the current test code. I don’t think we should spend too much time for a few lines of test code, but my impression is closer to @gen740’s and @not522’s opinions. |
|
Sorry, I totallly forgot that I added a comment on |
c-bata
left a comment
There was a problem hiding this comment.
Sorry, I totallly forgot that I added a comment on
__str__definition. Does this look good?
It looks good. Thank you for the update 🙏 LGTM.
Motivation
According to this comment,
TrialState.__repr__was intended to return a string that can be passed toevaland evaluated as itself. At that timeTrialStatewas a subclass ofenum.Enumbut nowenum.IntEnum.__str__returns just a numeric string.Description of the changes
Use
enum.Enum.__str__inTrialState.__repr__and ensure it's evaluated as itself via a unit test.