Skip to content

Make positional args to kwargs in suggest_int#5044

Merged
HideakiImamura merged 11 commits intooptuna:masterfrom
nabenabe0928:code-fix/change-args-to-kwargs-in-suggest-int
Nov 2, 2023
Merged

Make positional args to kwargs in suggest_int#5044
HideakiImamura merged 11 commits intooptuna:masterfrom
nabenabe0928:code-fix/change-args-to-kwargs-in-suggest-int

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 commented Oct 16, 2023

Motivation

As suggest_float has * before steps and log so that users need to pass them as kwargs, I applied the same to suggest_int.

Description of the changes

I made the following changes:

  1. Change suggest_int(name, low, high, step, log) into suggest_int(name, low, high, *, step, log) to strengthen the future safety, and
  2. Wrap suggest_int with convert_positional_args for now to avoid breaking changes.

@github-actions github-actions bot added the optuna.trial Related to the `optuna.trial` submodule. This is automatically labeled by github-actions. label Oct 16, 2023
@nabenabe0928 nabenabe0928 force-pushed the code-fix/change-args-to-kwargs-in-suggest-int branch from 2dc92a4 to 25d7a0a Compare October 16, 2023 06:58
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

I have a question.
This PR does not improve the safety of the software by itself, but is it still fine?

@nabenabe0928 nabenabe0928 force-pushed the code-fix/change-args-to-kwargs-in-suggest-int branch from 25d7a0a to 29b63da Compare October 16, 2023 07:04
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 16, 2023

Codecov Report

Merging #5044 (6a44734) into master (3fcbf88) will increase coverage by 0.01%.
Report is 78 commits behind head on master.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #5044      +/-   ##
==========================================
+ Coverage   89.40%   89.42%   +0.01%     
==========================================
  Files         203      205       +2     
  Lines       15055    15135      +80     
==========================================
+ Hits        13460    13534      +74     
- Misses       1595     1601       +6     
Files Coverage Δ
optuna/_convert_positional_args.py 92.50% <100.00%> (+2.84%) ⬆️
optuna/multi_objective/trial.py 87.42% <100.00%> (+0.23%) ⬆️
optuna/trial/_base.py 76.34% <100.00%> (+0.25%) ⬆️
optuna/trial/_fixed.py 90.00% <100.00%> (+0.30%) ⬆️
optuna/trial/_frozen.py 94.90% <100.00%> (+0.07%) ⬆️
optuna/trial/_trial.py 95.74% <100.00%> (+0.06%) ⬆️

... and 17 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@contramundum53
Copy link
Copy Markdown
Member

@not522 @HideakiImamura Could you review this PR?

Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Basically, LGTM. I have two minor comments. PTAL.

By the way, as you mentioned, this PR does not force anything on the users as it is a change aimed at continuing support for the older API. However, this change is a milestone for the future. It is expected that the older API will no longer be supported in a future major version, and by making these changes in the code, it becomes easier for us to keep motivation.

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.

The multi_objective module is deprecated, so you should not have to add tests on this module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just to make sure, but did you mean "do not have to" or "discouraging" (In other words, should I remove it)?

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.

I mean I discourage to fix the deprecated module.

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.

The multi_objective module is deprecated, so you should not have to fix this module.

from optuna.distributions import CategoricalChoiceType


_SUGGEST_INT_POSITIONAL_ARGS = ["self", "name", "low", "high", "step", "log"]
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.

Could you make only step and log positional? Currently convert_positional_args does not support partial conversion, but you can use inspect.signature to achieve this.

Copy link
Copy Markdown
Contributor Author

@nabenabe0928 nabenabe0928 Oct 24, 2023

Choose a reason for hiding this comment

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

Hi sorry for the late response:(
The variable name was actually misleading because these are "previous" positional arguments, but not the current positional arguments.
Plus, since all the arguments in suggest_int were positional arguments before this PR, I would like to simply change the variable name and let the convert_positional_args stay as it does.
If necessary, I can also make another PR for this.
How do you like it? (@not522)

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.

Oh, I'm sorry, it's my mistake. I wanted to say "make only step and log keyword-only".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Certainly!
Then I will change the code so that inappropriate args usages will be warned!

@HideakiImamura
Copy link
Copy Markdown
Member

@nabenabe0928 The checks CI fails. Could you fix them?

@nabenabe0928
Copy link
Copy Markdown
Contributor Author

@nabenabe0928 The checks CI fails. Could you fix them?

Done!

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.

I found unnecessary warnings, for example, suggest_int("x", 0, 9) outputs FutureWarning. I think that the check for the length in converter_wrapper is not correct.

@nabenabe0928 nabenabe0928 force-pushed the code-fix/change-args-to-kwargs-in-suggest-int branch from 56fe8a9 to 070cc61 Compare October 27, 2023 06:15
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

I found unnecessary warnings, for example, suggest_int("x", 0, 9) outputs FutureWarning. I think that the check for the length in converter_wrapper is not correct.

Now I changed so that the FutureWarning happens only if keyword-only is specified as arguments.
Also the new implementation warns only once rather than raising warnings for each argument.

f"{func.__name__}(): Please give all values as keyword arguments."
f"{func.__name__}() got {kwargs_expected} as positional arguments "
"but they were expected to be given as keyword arguments."
" See https://github.com/optuna/optuna/issues/3324 for details.",
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.

It's almost LGTM. How about removing this line because this error is not only related to #3324?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, because of the last change, #3324 does not relate to this line anymore. (while it still makes sense to use kwargs only, the future warning for it is not the default Python design, so I removed it and stick to your suggestion.)

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 removed their assignment Nov 1, 2023
@not522 not522 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Nov 1, 2023
Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

LGTM.

@HideakiImamura HideakiImamura merged commit 4694831 into optuna:master Nov 2, 2023
@HideakiImamura HideakiImamura added this to the v3.5.0 milestone Nov 2, 2023
@nabenabe0928 nabenabe0928 deleted the code-fix/change-args-to-kwargs-in-suggest-int branch November 8, 2023 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. optuna.trial Related to the `optuna.trial` submodule. This is automatically labeled by github-actions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants