Skip to content

Add arguments of versions to convert_positional_args#6009

Merged
nabenabe0928 merged 8 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/add_argument_to_convert_positional_args
Apr 10, 2025
Merged

Add arguments of versions to convert_positional_args#6009
nabenabe0928 merged 8 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/add_argument_to_convert_positional_args

Conversation

@fusawa-yugo
Copy link
Copy Markdown
Contributor

Motivation

This PR is from #5485

Description of the changes

I added arguments and warning for deprecated_version and removed_version, referring to deprecated_func
The format of warning is:

"Positional arguments in {name} have been deprecated since v{d_ver}. "
"They will be removed in v{r_ver}. "
"Please use keyword arguments instead. "
"See https://github.com/optuna/optuna/releases/tag/v{d_ver} for details."

@nabenabe0928 nabenabe0928 changed the title Add arguments of versions to convert_positional_args Add arguments of versions to convert_positional_args Mar 14, 2025
@c-bata
Copy link
Copy Markdown
Member

c-bata commented Mar 17, 2025

@not522 @porink0424 Could you review this PR?

@c-bata c-bata added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Mar 17, 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.

Thank you for your PR! Could you check my comments?

Comment on lines +64 to +65
deprecated_version: str | None = None,
removed_version: str | None = None,
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.

Let me confirm whether we will make the arguments optional. I recommend it is temporarily optional, and we will make it required on follow-up. How about that?

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.

I agree with you.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 Apr 1, 2025

Choose a reason for hiding this comment

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

Although I have no preference, I think it would be better to make the input typings stricter based on LSP.

The reason behind this comes from the fact that looser input typings implicitly assume that the function have a routine where we handle the possible input typings.
Namely, we have the processing for removed_version=None in this case and everywhere this function is called may use removed_version=None, which is a bit annoying to update later.
As we have the guidelines for deprecation, I think it would be better to not have optional for now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Btw, is removed_version actually replaced_version?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, now I noticed that most of them have not specified version, so we need to make it optional for now, probably.

Copy link
Copy Markdown
Member

@porink0424 porink0424 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 your PR, I've left some comments.

@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

Thank you for reviews!!
I revised the warnings.
Warning is displayed like below.

Test function

@convert_positional_args(
    previous_positional_arg_names=["a", "b"],
    deprecated_version="3.0.0",
    removed_version="4.0.0",
)
def example_function(a: int, b: int, c: int = 3, d: int = 4) -> None:
    print(f"a: {a}, b: {b}, c: {c}, d: {d}")

example_function(1, 2) 

Warning

FutureWarning: Positional arguments ['a', 'b'] in example_function() have been deprecated since v3.0.0. They will be removed in v4.0.0. Please use keyword arguments instead. See https://github.com/optuna/optuna/releases/tag/v3.0.0 for details.
  example_function(1, 2)
a: 1, b: 2, c: 3, d: 4

positional_arg_names = _get_positional_arg_names(func)
inferred_kwargs = _infer_kwargs(previous_positional_arg_names, *args)

if inferred_kwargs and (deprecated_version or removed_version):
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.

inferred_kwargs include ordinal positional arguments. So we should check the lengths of them.

Suggested change
if inferred_kwargs and (deprecated_version or removed_version):
if len(inferred_kwargs) > len(positional_arg_names) and (deprecated_version or removed_version):

@not522
Copy link
Copy Markdown
Member

not522 commented Mar 28, 2025

Thank you for your update! It's almost OK.

How about swap the order of warning messages? For example, the following outputs the versions first, but I think It's better to first output what users should do (i.e., example_function() got {'b', 'a'} as ...).

from optuna._convert_positional_args import convert_positional_args

@convert_positional_args(
    previous_positional_arg_names=["a", "b", "c", "d"],
    deprecated_version="3.0.0",
    removed_version="4.0.0",
)
def example_function(*, a: int, b: int, c: int = 3, d: int = 4) -> None:
    print(f"a: {a}, b: {b}, c: {c}, d: {d}")

example_function(1, 2)
/Users/naotomizuno/optuna/tmp_convert.py:11: FutureWarning: Positional arguments ['a', 'b', 'c', 'd'] in example_function() have been deprecated since v3.0.0. They will be removed in v4.0.0. Please use keyword arguments instead. See https://github.com/optuna/optuna/releases/tag/v3.0.0 for details.
example_function() got {'b', 'a'} as positional arguments but they were expected to be given as keyword arguments.

@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

Thank you for your comments again!!
I updated the code.
Please review it again!

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 Mar 28, 2025
@c-bata
Copy link
Copy Markdown
Member

c-bata commented Mar 28, 2025

@HideakiImamura I unassigned @porink0424 since he is busy for a while. Could you assign an additional reviewer?

@nabenabe0928 nabenabe0928 self-assigned this Mar 31, 2025
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, i left some comments:)

Comment on lines +64 to +65
deprecated_version: str | None = None,
removed_version: str | None = None,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, now I noticed that most of them have not specified version, so we need to make it optional for now, probably.

Comment on lines +67 to +69
if deprecated_version is None and removed_version is None:
pass
else:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if deprecated_version is None and removed_version is None:
pass
else:
if deprecated_version is not None or removed_version is not None:

@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

Thank you for review!!
Could you check it again?

"Positional arguments {deprecated_positional_arg_names} in {func_name}() "
"have been deprecated since v{d_ver}. "
"They will be replaced with the corresponding keyword arguments in v{r_ver}, "
"so please use keyword specification instead. "
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"so please use keyword specification instead. "
"so please use the keyword specification instead. "

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the changes, almost LGTM!

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the update, LGTM!

@nabenabe0928 nabenabe0928 merged commit 11ff024 into optuna:master Apr 10, 2025
14 checks passed
@nabenabe0928 nabenabe0928 removed their assignment Apr 10, 2025
@nabenabe0928 nabenabe0928 added this to the v4.3.0 milestone Apr 10, 2025
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants