Skip to content

Refactor warning handling of _tell_with_warning#6082

Merged
not522 merged 3 commits intooptuna:masterfrom
sawa3030:fix/warning-implementation-of-tell-with-warning
May 23, 2025
Merged

Refactor warning handling of _tell_with_warning#6082
not522 merged 3 commits intooptuna:masterfrom
sawa3030:fix/warning-implementation-of-tell-with-warning

Conversation

@sawa3030
Copy link
Copy Markdown
Collaborator

Motivation

The warning_message does not need to be stored in frozen_trial.system_attrs. This PR refactors the implementation to return the warning message directly from _tell_with_warning.

Note: After this change, the warning message will no longer be accessible via frozen_trial.system_attrs[STUDY_TELL_WARNING_KEY]. However, warning messages can still be captured via logging.getLogger. In addition, I checked public GitHub repositories, and it looks like STUDY_TELL_WARNING_KEY is not used directly by users, so the impact should be minimal.

Description of the changes

  • Refactored the warning handling logic to return the warning message from _tell_with_warning instead of storing it in system_attrs.
  • Updated affected call sites to handle the returned warning message explicitly.
  • Added tests to ensure the warning message is correctly returned and can be logged or captured as expected.

@HideakiImamura
Copy link
Copy Markdown
Member

@not522 @gen740 Could you review this PR?

if warning_message is not None:
frozen_trial._system_attrs[STUDY_TELL_WARNING_KEY] = warning_message
return frozen_trial
return frozen_trial, warning_message
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT]

As in this function, warning_message is just an alias for values_conversion_failure_message. The name warning_message does not accurately reflect its actual purpose, which could lead to confusion.
For improved readability and clarity, I suggest changing line 129 to:

values_conversion_failure_message = None

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@gen740 Thank you for the review. I've made the update accordingly. PTAL!

Copy link
Copy Markdown
Member

@gen740 gen740 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gen740 gen740 removed their assignment May 23, 2025
Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove tests for warning messages? They are implementation detail and generally are not tested.

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@not522 Thank you for the advice. I've made the update. PTAL!

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@not522 not522 added the code-fix Change that does not change the behavior, such as code refactoring. label May 23, 2025
@not522 not522 added this to the v4.4.0 milestone May 23, 2025
@not522 not522 merged commit 3126551 into optuna:master May 23, 2025
14 checks passed
@sawa3030 sawa3030 deleted the fix/warning-implementation-of-tell-with-warning branch November 14, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-fix Change that does not change the behavior, such as code refactoring.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants